Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,694 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,310 people online right now. Registration is fast and FREE... Join Now!




PHP mail form - from address

 
Reply to this topicStart new topic

PHP mail form - from address

morcomm
post 29 May, 2008 - 11:16 PM
Post #1


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 49


My Contributions


Hi, I have implemented a email for on my site. This is the code for it.

CODE
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$body = $_POST['body'];
if ($name != "" AND $email != "" AND $body != "") {
//PUT YOUE EMAIL ADDRESS -----
$sendto = "my email";
//PUT YOUR EMAIL ADDRESS -----
$subject = "Email from site!";

$message = "This is a message from your site
From: $name
Email: $email
Message: $body";
// send the mail
mail("$sendto", "$subject", "$message");
echo "Your message was sent";
}
?><form method="post" name="mailform" action="<?=$_SERVER['PHP_SELF'] ?>">

<table width="200"  border="0" cellspacing="0">
  <tr>
    <td width="62">Name:</td>
    <td width="134">
      <input name="name" type="text" id="name">
   </td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input name="email" type="text" id="email"></td>
  </tr>
  <tr>
    <td valign="top">Message:</td>
    <td><textarea name="body" cols="45" rows="10"></textarea></td>
  </tr>
</table>
  <br>
  <input type="submit" name="Submit" value="Submit">
  <input type="reset" name="Submit2" value="Reset">
</form>


The problem that I am having with this is that every time I send a email from this form I get an email from Apache. This is the email address: apache@webdev.jgi.co.za

The address of the testing server that I am using is http://webdev.jgi.co.za.

Can anyone explain what I am doing wrong. I want this email to come from something like nonreply@*****.***
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 29 May, 2008 - 11:24 PM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


What mail server are you sending this through?

I don't see any settings for your mail server in the code.
User is offlineProfile CardPM

Go to the top of the page

joeyadms
post 29 May, 2008 - 11:36 PM
Post #3


D.I.C Head

Group Icon
Joined: 4 May, 2008
Posts: 145



Thanked 6 times

Dream Kudos: 600

Expert In: PHP, Web Security

My Contributions


The From field is in the email headers. Use this

CODE


$fromEmail = 'noreply@mysite.com';
$headers = "From: $fromEmail";

mail("$sendto", "$subject", "$message",$headers);
User is offlineProfile CardPM

Go to the top of the page

morcomm
post 29 May, 2008 - 11:42 PM
Post #4


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 49


My Contributions


Where are the email headers? I know this sounds like a stupid question, but is this a file somewhere in my site?

This post has been edited by morcomm: 29 May, 2008 - 11:44 PM
User is offlineProfile CardPM

Go to the top of the page

joeyadms
post 29 May, 2008 - 11:49 PM
Post #5


D.I.C Head

Group Icon
Joined: 4 May, 2008
Posts: 145



Thanked 6 times

Dream Kudos: 600

Expert In: PHP, Web Security

My Contributions


Nah man, I showed you in the script above.
PHP's mail() function accepts 4 arguments

To, Subject, Message, Additional headers

All you have to do is set a string $headers = "From: some@email.com"; And pass that as the fourth argument.

Take a look at my script above, and just modify yours with it, ie. change your mail function and add the variables.
User is offlineProfile CardPM

Go to the top of the page

morcomm
post 30 May, 2008 - 12:00 AM
Post #6


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 49


My Contributions


OK, I think I understand. It is in the php script that runs the form. I have altered the code, but know it generates a blank page when I view it in a browser. I think this happens because the testing server does not have error pages. Code Below:

CODE
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$body = $_POST['body'];
if ($name != "" AND $email != "" AND $body != "") {
//PUT YOUE EMAIL ADDRESS -----
$sendto = "morcomm@jgi.co.za";
//PUT YOUR EMAIL ADDRESS -----
$subject = "Email from site!";

$message = "This is a message from your site
From: $name
Email: $email
Message: $body";

$headers = "From: email";
// send the mail
mail("$sendto", "$subject", "$message" "$header");
echo "Your message was sent";
}
?><form method="post" name="mailform" action="<?=$_SERVER['PHP_SELF'] ?>">

<table width="200"  border="0" cellspacing="0">
  <tr>
    <td width="62">Name:</td>
    <td width="134">
      <input name="name" type="text" id="name">
   </td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input name="email" type="text" id="email"></td>
  </tr>
  <tr>
    <td valign="top">Message:</td>
    <td><textarea name="body" cols="45" rows="10"></textarea></td>
  </tr>
</table>
  <br>
  <input type="submit" name="Submit" value="Submit">
  <input type="reset" name="Submit2" value="Reset">
</form>


This post has been edited by morcomm: 30 May, 2008 - 12:04 AM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 30 May, 2008 - 12:08 AM
Post #7


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(morcomm @ 30 May, 2008 - 03:42 AM) *

Where are the email headers? I know this sounds like a stupid question, but is this a file somewhere in my site?

Using joeyadms's code you are creating the mail header on the fly, & then sending it as an argument to mail().

CODE

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$body = $_POST['body'];
if ($name != "" AND $email != "" AND $body != "") {
//PUT YOUE EMAIL ADDRESS -----
$sendto = "morcomm@jgi.co.za";
//PUT YOUR EMAIL ADDRESS -----
$subject = "Email from site!";

$message = "This is a message from your site
From: $name
Email: $email
Message: $body";

$fromEmail = 'noreply@mysite.com';
$headers = "From: $fromEmail";

// send the mail
mail("$sendto", "$subject", "$message",$headers);

echo "Your message was sent";
}
?><form method="post" name="mailform" action="<?=$_SERVER['PHP_SELF'] ?>">

<table width="200"  border="0" cellspacing="0">
  <tr>
    <td width="62">Name:</td>
    <td width="134">
      <input name="name" type="text" id="name">
   </td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input name="email" type="text" id="email"></td>
  </tr>
  <tr>
    <td valign="top">Message:</td>
    <td><textarea name="body" cols="45" rows="10"></textarea></td>
  </tr>
</table>
  <br>
  <input type="submit" name="Submit" value="Submit">
  <input type="reset" name="Submit2" value="Reset">
</form>



You had $header in the mail function. & your variable just said email, it did not reference the email variable.
User is offlineProfile CardPM

Go to the top of the page

joeyadms
post 30 May, 2008 - 12:16 AM
Post #8


D.I.C Head

Group Icon
Joined: 4 May, 2008
Posts: 145



Thanked 6 times

Dream Kudos: 600

Expert In: PHP, Web Security

My Contributions


QUOTE(joeyadms @ 30 May, 2008 - 12:49 AM) *

Nah man, I showed you in the script above.
PHP's mail() function accepts 4 arguments

To, Subject, Message, Additional headers

All you have to do is set a string $headers = "From: some@email.com"; And pass that as the fourth argument.

Take a look at my script above, and just modify yours with it, ie. change your mail function and add the variables.


You left out a comma in your mail function, next to $headers
User is offlineProfile CardPM

Go to the top of the page

morcomm
post 30 May, 2008 - 12:20 AM
Post #9


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 49


My Contributions


Thanks man, that worked. It is now sending emails from the right address
User is offlineProfile CardPM

Go to the top of the page

joeyadms
post 30 May, 2008 - 12:25 AM
Post #10


D.I.C Head

Group Icon
Joined: 4 May, 2008
Posts: 145



Thanked 6 times

Dream Kudos: 600

Expert In: PHP, Web Security

My Contributions


awesome
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:48AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month