0

My php mail script is sending mail, but even with the headers telling it to be html, it ignores it and sends the mail as text. All the '\r\n' breaks end up being ignored and the $headers content pasted below the $body in the email. Below is the code.

//mail out copy
$to = $email;
$subject = 'Thank you for signing up to Learn to Play';
$headers = 'From: donotreply@xxx.com' . '\r\n';

Always set content-type when sending HTML email
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$body = 'Thank you for registering with us for your Learn to Play Table Games Lesson. Your registration has been confirmed for ';
$body .= $date_requested;
$body .= ' for a group of '; 
$body .= $guests;

$body .= '.  Please check in at the Guest Services Desk at 6:45PM where a member of our Table Games Team will meet you for your session. ';
$body .= 'Should there be any conflict with your reservation a member of our team will contact you to re-schedule your lesson.';

mail($to, $subject, $headers, $body) or die('Error sending mail');

}

Xero1
  • 329
  • 4
  • 18
  • Do yourself a huge favor and stop doing this the hard way. Use a popular, battle-tested class like https://swiftmailer.symfony.com/ to do all this for you. You don't need to be making raw headers and the `mail()` function. – ceejayoz Oct 11 '17 at 23:52

2 Answers2

1

You do it the in the wrong order:

mail($to, $subject, $body, $headers) or die('Error sending mail');

You also have to used double quotes "\r\n" instate of single quotes '\r\n'.

Example

for Text Mail:

$subject = 'Thank you for signing up to Learn to Play';

$body    = 'Thank you for registering with us for your Learn to Play Table Games Lesson. Your registration has been confirmed for '
         . $date_requested
         . ' for a group of '
         . $guests
         . '.  Please check in at the Guest Services Desk at 6:45PM where a member of our Table Games Team will meet you for your session. '
         . 'Should there be any conflict with your reservation a member of our team will contact you to re-schedule your lesson.'
;

$headers = "From: donotreply@example.com\r\n"
         . "Reply-To: donotreply@example.com\r\n"
         . 'X-Mailer: PHP/' . phpversion()
;

mail($email, $subject, $body, $headers) or die('Error sending mail');

for HTML Mail:

$subject = 'Thank you for signing up to Learn to Play';

$body    = 
'<html>
    <head>
        <title>' . $subject . '</title>
    </head> 
    <body>
        <h1>Thank you for registering with us for your Learn to Play Table Games Lesson.</h1>
        <p>Your registration has been confirmed for '
        . $date_requested
        . ' for a group of '
        . $guest
        . '. Please check in at the Guest Services Desk at 6:45PM where a member of our Table Games Team will meet you for your session. Should there be any conflict with your reservation a member of our team will contact you to re-schedule your lesson.
        </p>
    </body>
</html>'
;

$headers = "MIME-Version: 1.0\r\n"
         . "Content-type: text/html; charset=iso-8859-1\r\n"
         . "From: donotreply@example.com\r\n"
         . "Reply-To: donotreply@example.com\r\n"
         . 'X-Mailer: PHP/' . phpversion()
;

mail($email, $subject, $body, $headers) or die('Error sending mail');

http://php.net/manual/en/function.mail.php

Webdesigner
  • 1,954
  • 1
  • 9
  • 16
  • I changed the $headers to be the last parameter, and now my email looks like this: – Xero1 Oct 13 '17 at 16:08
  • Learn to PlayThank you for registering with XXX for your Learn to Play Table Games Lesson. Your registration has been confirmed for Monday October 16, 2017 for a group of 1. Please check in at the Guest Services Desk at 6:45PM where a member of our Table Games Team will meet you for your session.
    Should there be any conflict with your reservation a member of our team will contact you to re-schedule your lesson.
    – Xero1 Oct 13 '17 at 16:10
  • and the header content regarding MIM version is in the from section of the email – Xero1 Oct 13 '17 at 16:10
  • You have to do it right. You have to used double quotes `"\r\n"` instate of single quotes `'\r\n'` . And you should use ether HTML or Plain Text. See New Examples. – Webdesigner Oct 13 '17 at 16:52
0

Here:

mail($to, $subject, $headers, $body) or die('Error sending mail');

Change that to:

mail($to, $subject, $body, $headers) or die('Error sending mail');

Because $headers should be the last parameter.

Zeke
  • 1,281
  • 1
  • 18
  • 26