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' . "\r\n" .
'Content-type: text/plain; charset=iso-8859-1' . "\r\n" .
'Content-Transfer_Encoding: 7bit' . "\r\n\r\n" .
'From: fromemail'."\r\n" .
'Reply-To: replyemail' . "\r\n" .
'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. https://i.stack.imgur.com/eSDyo.jpg
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' . "\r\n" .
'Content-type: text/plain; charset=iso-8859-1' . "\r\n" .
'Content-Transfer_Encoding: 7bit' . "\r\n\r\n" .
'From: fromemail' . "\r\n" .
'Reply-To: replyemail' . "\r\n" .
'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:
You have an extra line break here, which completes the headers portion of the message before the
Fromand subsequent headers. Remove the extra\r\n.