Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 电子邮件正文为空,MIME多部分(文本和html)_Php_Email_Smtp - Fatal编程技术网

Php 电子邮件正文为空,MIME多部分(文本和html)

Php 电子邮件正文为空,MIME多部分(文本和html),php,email,smtp,Php,Email,Smtp,我可以只发送文本,只发送html,但是当我将两者结合在一起时,电子邮件会发送,但正文是空的 $to = "some@email.com"; $subject = "Welcome"; $boundary = md5(date('U')); $headers = "From: Company <noreply@email.com>" . "\n". "X-Mailer: PHP/".phpversion() ."\n".

我可以只发送文本,只发送html,但是当我将两者结合在一起时,电子邮件会发送,但正文是空的

$to = "some@email.com";
$subject = "Welcome";
$boundary = md5(date('U'));

$headers = "From: Company <noreply@email.com>" . "\n".
                   "X-Mailer: PHP/".phpversion() ."\n".
                   "MIME-Version: 1.0" . "\n".
                   "Content-Type: multipart/alternative; boundary=".$boundary. "\n";
                   "Content-Transfer-Encoding: 7bit". "\n";

// TEXT EMAIL PART
$text = "Congratulations";

// HTML EMAIL PART
$html = "<html><body>\n";
$html .= "<div>Congratulations</div>";
$html .= "</body></html>\n";

$message = "Multipart Message coming up" . "\n\n".
           "--".$boundary.
           "Content-Type: text/plain; charset=\"iso-8859-1\"" .
           "Content-Transfer-Encoding: 7bit".
           $text. 
           "--".$boundary. 
           "Content-Type: text/html; charset=\"iso-8859-1\"". 
           "Content-Transfer-Encoding: 7bit". 
           $html.
           "--".$boundary."--";

mail($to, $subject, $message, $headers);    
$to=”some@email.com";
$subject=“欢迎”;
$boundary=md5(日期('U');
$headers=“From:Company”。“\n”。
“X-Mailer:PHP/”.phpversion()。“\n”。
“MIME版本:1.0”。“\n”。
“内容类型:多部分/可选;边界=”.$boundary。“\n”;
“内容传输编码:7bit”。“\n”;
//文本电子邮件部分
$text=“恭喜”;
//HTML电子邮件部分
$html=“\n”;
$html.=“祝贺您”;
$html.=“\n”;
$message=“即将出现多部分消息”。“\n\n”。
“-”$边界。
“内容类型:文本/普通;字符集=\“iso-8859-1\”。
“内容传输编码:7bit”。
$text。
“-”$边界。
“内容类型:text/html;字符集=\“iso-8859-1\”。
“内容传输编码:7bit”。
$html。
“——”$boundary。”——”;
邮件($to、$subject、$message、$headers);
$boundary
如果未定义边界,则在任何边界之前或之后都没有换行符,并且在每个零件的标题和主体之间也没有空行(或在任何这些标题之后有换行符)


不要手摇哑剧。我相信有一个不错的PHP库可以为您提供帮助。

您忘记了内容类型和内容传输编码行之后的换行符,因此正文内容的开头与标题混合在一起:

       "--".$boundary.
                    ^^^--- missing \n
       "Content-Transfer-Encoding: 7bit".
                                        ^^^--- missing \n\n
       $text. 

如上所述,请使用或来执行此操作。他们会处理所有琐碎的细节,让你用更少的代码行发送整个邮件。

我假设你已经为某个东西设置了$boundary?另外,看看你可以为你做很多艰苦的工作是的,现在就到了。我剪切粘贴时没有添加它。边界设置在第三行:md5(日期('U');不是我写答案的时候。看看对这个问题的评论,真不敢相信事情竟如此简单。我将确保查看SwiftMailer,以便生成这样的多行文本,同时查看HEREDOCs。比重复串接容易得多。