编辑 #4
以下是另一个不错的版本,看起来更容易使用:
<?php
$to = 'email@example.com';
$subject = 'Test HTML email';
$mime_boundary = '_x'.sha1(time()).'x';
$headers = <<<HEADERS
From: Test <email@example.com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="PHP-alt$mime_boundary"
HEADERS;
$message = <<<MESSAGE
--PHP-alt$mime_boundary
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
This Is a Plain Text Email
This message has no HTML. http://www.google.com
--PHP-alt$mime_boundary
Content-type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 7bit
<html>
<body>
<h1>This Is an HTML Email</h1>
<p>
This message is composed in <a href="http://www.google.com">GOOGLE, click here</a>.
</p>
</body>
</html>
--PHP-alt$mime_boundary--
MESSAGE;
if(!mail($to, $subject, $message, $headers))
{
echo "Something went wrong!";
}
else
{
echo "Message sent successfully. Check your email!";
}
?>
编辑3
另一种经过测试的方法:(迄今为止是更好的方法)
<?php
function send_email(
$to='email@example.com',
$from='email@example.com',
$subject='test',
$html_content='<b>HELLO in HTML bold</b>',
$text_content='Hi there in plain text',
$headers='')
{
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$body = "This is a multi-part message in mime format.\n\n";
$body .= "--$mime_boundary\n";
$body .= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n\n";
$body .= $text_content;
$body .= "\n\n";
$body .= "--$mime_boundary\n";
$body .= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n\n";
$body .= $html_content;
$body .= "\n\n";
$body .= "--$mime_boundary--\n";
$headers .= "From: $from\r\n";
$headers .= "X-Sender-IP: $_SERVER[SERVER_ADDR]\r\n";
$headers .= 'Date: '.date('n/d/Y g:i A')."\r\n";
return mail($to, $subject, $body, $headers);
}
send_email();
?>
编辑 2
这个方法起作用了,在Gmail中只显示了HTML格式,因此纯文本理论上也应该可以。
<?php
$notice_text = "This is a multi-part message in MIME format.";
$plain_text = "This is a plain text email.\r\nIt is very cool.";
$html_text = "<html><body>This is an <b style='color:purple'>HTML</b>" .
"text email.\r\nIt is very cool.</body></html>";
$semi_rand = md5(time());
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);
$to = "Me <email@example.com>";
$from = "Him <another@example.com>";
$subject = "My Email";
$body = "$notice_text
--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
$plain_text
--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
$html_text
--$mime_boundary--";
if (@mail($to, $subject, $body,
"From: " . $from . "\n" .
"bcc: " . $bcc . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/alternative;\n" .
" boundary=" . $mime_boundary_header))
echo "Email sent successfully.";
else
echo "Email NOT sent successfully!";
?>
编辑1
尝试使用此版本:(在任何其他地方均未测试附加代码)
<?php
$to = "someone@gmail.com";
$subject = "Test HTML E-mail";
$headers = "From: admin@yellowcas.com" . "\n" . "Reply-To: admin@yellowcas.com" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$boundary = uniqid("HTMLDEMO");
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
$body = "--$boundary\r\n" .
"Content-Type: text/plain; charset=ISO-8859-1\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode("
Hello, Tom!!!
This is simple text email message.
"));
$body .= "--$boundary\r\n" .
"Content-Type: text/html; charset=ISO-8859-1\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode("
<!doctype html>
<head>
<title>Untitled Document</title>
</head>
<body>
<h2>Hello, Tom!</h2>
<p>This is something with <strong>HTML</strong> formatting.</p>
</body>
</html>
"));
ob_start();
?>
<?php
$message = ob_get_clean();
$mail_sent = mail( $to, $subject, $body, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
您需要在初始标题中使用以下内容:
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
below:
$headers .= "MIME-Version: 1.0" . "\n";
根据
PHP.net手册
,已经进行了测试(成功以HTML格式发送到我的Gmail,而非代码)。
<?php
$to = "someone@gmail.com";
$subject = "Test HTML E-mail";
$random_hash = md5(date("r", time()));
$mID = md5($to);
$headers = "From: admin@yellowcas.com" . "\n" . "Reply-To: admin@yellowcas.com" . "\n";
$headers .= "Errors-To: someone@yahoo.com" . "\n";
$headers .= "MIME-Version: 1.0" . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=". $random_hash ." ; Message-ID: <" . $mID . ">" . "\n";
ob_start();
?>
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
,就像http://php.net/mail中所述,在`$headers .= "MIME-Version: 1.0" . "\n";`下面。 - Funk Forty Niner\r
字符。我在所有的脚本中使用\r\n
,所有的电子邮件客户端包括 Gmail 都能正常接收。我甚至用它测试了下面的答案$headers .= "MIME-Version: 1.0" . "\r\n";
,结果也很好。 - Funk Forty Niner