Failed to send HTML mails using PHP mail()
Strange things happening to me. I'm trying to send a HTML mail, using the php mail()
function, but no luck here. Even when I copy/paste a piece of code literally, it doesn't work. What am I doing wrong? Here is the piece of code I use:
$message = "<html><body>";
$message .= "<table rules='all' style='border-color: #666;' cellpadding='10'>";
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$to = 'me@gmail.com';
$subject = 'Website Change Reqest';
$headers = "From: " . $email . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($to, $subject, $message, $headers)) {
echo 'Your message has been sent.';
} else {
echo 'There was a problem sending the email.';
}
And this is what my e-mail looks like...:
Reply-To: me@gmail.com
MIME-Version: 1.0
Content-Type: text/ht开发者_运维技巧ml; charset=ISO-8859-1
Message-Id: <20110703234551.A9D6153DAB@apache10.hostbasket.com>
Date: Mon, 4 Jul 2011 01:45:51 +0200 (CEST)
<html><body><table rules="all" style="border-color: #666;" cellpadding="10"><tr style='background: #eee;'><td><strong>Name:</strong> </td></tr><tr><td><strong>Email:</strong> </td></tr><tr><td><strong>Type of Change:</strong> </td></tr><tr><td><strong>Urgency:</strong> </td></tr><tr><td><strong>URL To Change (main):</strong> </td></tr><tr><td><strong>NEW Content:</strong> </td></tr></table></body></html>
What do I do wrong?
We can't see where the variable $email
was set, but I'm guessing that there might be an extra line break at the end of your $email
variable. This would have the effect of putting in two linebreaks after the From:
header and before the Reply-to:
, which signals the start of the body of the message and the completion of the headers.
Try:
$email = trim($email);
before constructing the message. Since there appears to be an extra line break after the Reply-to
header as well, my case is even stronger for an extra break in $email
.
UPDATE
Also try changing the linebreaks to PHP's native format on the system where the code will run. This is done by replacing \r\n
with PHP_EOL
$headers = "From: " . $email . PHP_EOL;
$headers .= "Reply-To: ". $email . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-Type: text/html; charset=ISO-8859-1" . PHP_EOL;
Here is a useful link to sending html emails in php
http://blog.twigahost.com/how-to-send-html-email-with-php/
I recommend you to use a mailer class. They give you the possibility to use smtp auth, so every mail will be passed through. A few examples:
- http://phpmailer.worxware.com/
- http://swiftmailer.org/
- http://framework.zend.com/manual/en/zend.mail.html
精彩评论