I have recently tried to use the php mail function to send out confirmation emails and I have been successful in doing so. However, when I added a few things to my script, something doesn't seem to work.
The code below is the code that I got to work. Everything that I need for the email to contain appears.
$to = 'Myemail';
$subject = 'Confirmation';
$message = 'This is a test';
$headers = 'MIME-Version: 1.0' . "
" .
'Content-type: text/plain; charset=iso-8859-1' . "
" .
'Content-Transfer_Encoding: 7bit' . "
" .
'From: fromemail'."
" .
'Reply-To: replyemail' . "
" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
However, when I transfer the same headers to another script (Below), the mail delivers but there are a few issues.
1) My mail says that the mail is from nobody. 2) Instead of the headers appearing in the info area, it appears as text in the mail From: from email Reply-To: reply email X-Mailer: PHP/5.2.9
The script below is being included in another program i wrote, so i am wondering if that is the problem. I don't think its syntax because its the same headers I used above. I have attached a picture of the mail I get. http://imgur.com/weNkr
Your help is greatly appreciated!!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<HEAD>
</HEAD>
<body>
<?php
$message = $_POST['message'];
$subject = $_POST['subject'];
if ($message != null) {
include("connect.php");
$extract = mysql_query("SELECT * FROM `contact` ORDER BY `id`") or die("Error");
$counter = 0;
while ($row = mysql_fetch_assoc($extract)) {
$email[$counter] = $row['email'];
$counter++;
}
for ($x = 0; $x < $counter; $x++) {
$to = $email[$x];
$subject = $subject;
$message = $message;
$headers = 'MIME-Version: 1.0' . "
" .
'Content-type: text/plain; charset=iso-8859-1' . "
" .
'Content-Transfer_Encoding: 7bit' . "
" .
'From: fromemail' . "
" .
'Reply-To: replyemail' . "
" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo "EMAIL WAS SENT TO: ";
echo $email[$x];
echo "<BR>";
}
}
?>
</body>
</html>
Your problem is with this line:
'Content-Transfer_Encoding: 7bit' . "
" .
//---------------------------------^^^^^^^^^^^
// Two line breaks ends the header block
// These remaining headers are seen as part of the message body
'From: fromemail'."
" .
'Reply-To: replyemail' . "
" .
'X-Mailer: PHP/' . phpversion();
You have an extra line break here, which completes the headers portion of the message before the From
and subsequent headers. Remove the extra .
As a side note, you may need to set the -f
switch to set the envelope from address in order to resolve the From nobody error for some email clients, like so:
$from = 'email@example.com';
mail($to, $subject, $message, $headers, '-f' . $from);
This is of course in addition to the header error that Michael has corrected.