从 PHP 发送 HTML 电子邮件 [英] Sending HTML email from PHP
问题描述
我正在尝试从 PHP 发送一个简单的 HTML 电子邮件.下面的代码只会在 GMail 中生成一封空白电子邮件.它还有一个名为noname"的空附件,这根本不是我想要的;虽然这可能只是它不起作用的症状.
I am trying to send a simple HTML e-mail from PHP. The code below simply results in a blank e-mail in GMail. It also has an empty attachment called 'noname', which is not at all what I want; though that might just be a symptom of it not working.
我使用的代码是:
<?php
//define the receiver of the email
$to = 'morrillkevin@gmail.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with
$headers = "From: webmaster@example.com
Reply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "
Content-Type: multipart/alternative; boundary="PHP-alt-".$random_hash.""";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
MIME-Version: 1.0
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b>formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
推荐答案
原来关键是编码类型.而不是:
It turns out the key is the encoding type. Instead of:
Content-Type: text/plain; charset="iso-8859-1"
我需要使用:
Content-Type: text/plain; charset=us-ascii
这可能取决于您如何在自己的文本编辑器中保存 PHP 文件等详细信息.我没有研究过,但 PHP 中的 iconv 函数可能也给我带来了一些乐趣.所以我觉得这部分真的很敏感.
It might depend on stuff as detailed as how you save the PHP file in your own text editor. I haven't looked into it, but the iconv function in PHP may have brought me some joy too. So I think this part is really sensitive.
这是一个更好的示例代码片段,它端到端地显示了整个事情:
Here is a better snippet of sample code that shows the whole thing end-to-end:
$notice_text = "This is a multi-part message in MIME format.";
$plain_text = "This is a plain text email.
It is very cool.";
$html_text = "<html><body>This is an <b style='color:purple'>HTML</b> text email.
It 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 <foo@gmail.com>";
$from = "Me.com <me@me.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 . "
" .
"MIME-Version: 1.0
" .
"Content-Type: multipart/alternative;
" .
" boundary=" . $mime_boundary_header))
echo "Email sent successfully.";
else
echo "Email NOT sent successfully!";
exit;
-凯文
这篇关于从 PHP 发送 HTML 电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!