从PHP发送HTML电子邮件 [英] Sending HTML email from PHP
问题描述
我正在尝试从PHP发送一个简单的HTML电子邮件。下面的代码简单地导致了GMail中的一封空白电子邮件。它还有一个空的附件,叫做noname,这根本不是我想要的;虽然这可能只是一个不起作用的症状。
我使用的代码是:
<?php
//定义电子邮件的接收者
$ to ='morrillkevin@gmail.com';
//定义电子邮件的主题
$ subject ='测试HTML电子邮件';
//创建边界字符串。它必须是唯一的
//所以我们使用MD5算法来生成随机哈希
$ random_hash = md5(date('r',time()));
//定义要传递的标题。请注意,它们与\\\
$ headers =From:webmaster@example.com\r\\\
Reply-To:webmaster@example.com分开;
//添加边界字符串和mime类型规范
$ headers。=\r\\\
Content-Type:multipart / alternative; boundary = \PHP-alt - 。$ random_hash。 \ ;
//定义消息的正文。
ob_start(); //打开输出缓冲
?>
--PHP-alt-<?php echo $ random_hash; ?>
MIME版本:1.0
内容类型:text / plain; charset =iso-8859-1
Content-Transfer-Encoding:7bit
Hello World!
这是简单的文本电子邮件。
--PHP-alt-<?php echo $ random_hash; ?>
MIME版本:1.0
内容类型:text / html; charset =iso-8859-1
Content-Transfer-Encoding:7bit
< h2> Hello World!< / h2>
< p>这是< b> HTML< / b>格式化的东西< / p>
--PHP-alt-<?php echo $ random_hash; ?> -
< ;?
//将当前缓冲区内容复制到$ message变量中并删除当前输出缓冲区
$ message = ob_get_clean();
//发送电子邮件
$ mail_sent = @mail($ to,$ subject,$ message,$ headers);
//如果邮件发送成功打印邮件发送。否则打印邮件失败
echo $ mail_sent? 邮件发送:邮件失败;
原来,关键是编码类型。而不是:
Content-Type:text / plain;我需要使用: Content-Type:text / plain; charset = us-ascii
它可能依赖于如何将您的PHP文件保存在您的自己的文本编辑器我没有看过,但PHP中的 iconv 函数也可能给我带来了一些喜悦。所以我认为这部分是非常敏感的。
这是一个更好的示例代码片段,用于展示端到端的整体:
$ notice_text =这是MIME格式的多部分消息。
$ plain_text =这是一个纯文本的电子邮件。\\\\,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
$ html_text =< html>< body>这是< b style ='color:purple'> HTML< / b>文本电子邮件。 ; /体>< / 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 =我的电子邮件;
$ body =$ notice_text
- $ mime_boundary
内容类型:text / plain; charset = us-ascii
内容转移-Encoding:7bit
$ plain_text
- $ mime_boundary
内容类型:text / html; charset = us-ascii
内容转移-Encoding:7bit
$ html_text
- $ mime_boundary--;
if(@mail($ to,$ subject,$ body,
From:。$ from。\\\
MIME-Version:1.0
内容类型:multipart / alternative; \\\
boundary =$ mime_boundary_header))
echo电子邮件已成功发送。
else
echo电子邮件不成功发送;
退出;
-Kevin
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.
The code I am using is:
<?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 \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-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"
I needed to use:
Content-Type: text/plain; charset=us-ascii
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.\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 <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 . "\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!";
exit;
-Kevin
这篇关于从PHP发送HTML电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!