9 Replies - 1462 Views - Last Post: 30 May 2008 - 01:25 AM Rate Topic: -----

#1 morcomm  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 28-March 08

PHP mail form - from address

Post icon  Posted 30 May 2008 - 12:16 AM

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

<?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@*****.***

Is This A Good Question/Topic? 0
  • +

Replies To: PHP mail form - from address

#2 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5068
  • View blog
  • Posts: 26,450
  • Joined: 10-May 07

Re: PHP mail form - from address

Posted 30 May 2008 - 12:24 AM

What mail server are you sending this through?

I don't see any settings for your mail server in the code.
Was This Post Helpful? 0
  • +
  • -

#3 joeyadms  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 178
  • Joined: 04-May 08

Re: PHP mail form - from address

Posted 30 May 2008 - 12:36 AM

The From field is in the email headers. Use this


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

mail("$sendto", "$subject", "$message",$headers);


Was This Post Helpful? 0
  • +
  • -

#4 morcomm  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 28-March 08

Re: PHP mail form - from address

Posted 30 May 2008 - 12:42 AM

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: 30 May 2008 - 12:44 AM

Was This Post Helpful? 0
  • +
  • -

#5 joeyadms  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 178
  • Joined: 04-May 08

Re: PHP mail form - from address

Posted 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.
Was This Post Helpful? 1
  • +
  • -

#6 morcomm  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 28-March 08

Re: PHP mail form - from address

Posted 30 May 2008 - 01:00 AM

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:

<?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 - 01:04 AM

Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5068
  • View blog
  • Posts: 26,450
  • Joined: 10-May 07

Re: PHP mail form - from address

Posted 30 May 2008 - 01:08 AM

View Postmorcomm, on 30 May, 2008 - 03:42 AM, said:

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().

<?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.
Was This Post Helpful? 1
  • +
  • -

#8 joeyadms  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 178
  • Joined: 04-May 08

Re: PHP mail form - from address

Posted 30 May 2008 - 01:16 AM

View Postjoeyadms, on 30 May, 2008 - 12:49 AM, said:

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
Was This Post Helpful? 0
  • +
  • -

#9 morcomm  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 28-March 08

Re: PHP mail form - from address

Posted 30 May 2008 - 01:20 AM

Thanks man, that worked. It is now sending emails from the right address
Was This Post Helpful? 0
  • +
  • -

#10 joeyadms  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 178
  • Joined: 04-May 08

Re: PHP mail form - from address

Posted 30 May 2008 - 01:25 AM

awesome
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1