通用发送电子邮件html或纯文本php

时间:2014-02-27 13:43:05

标签: php email

我已经可以创建一个发送HTML邮件模板的脚本了,并且如果用户有一个无法读取HTML的客户端,并且纯文本列表可能是接收HTML电子邮件。

请提供一些关于如何做的普遍建议。我经历了整个互联网,我真的不知道。

非常感谢你的建议。

2 个答案:

答案 0 :(得分:1)

您应该在同一个电子邮件正文中添加名为multipart/alternative的电子邮件的html和纯文本版本:

<?php

        $from = "a asd"; 
        $email = "sd@dfsds.com";
        $email = "sdsad@sdfsdf.com";

                $semi_rand = md5(time());
                $mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
                $mime_boundary_header = chr(34) . $mime_boundary . chr(34);
                $boundary = "nextPart";
                $headers = "From: \"".$from."\" <".$email.">\n";

                $headers .=  "To: ". $new_to ."\n";

                $headers .= "Reply-To: \"". $from. "\" <" . $email . ">\r\n";
                $headers .= "Return-Path: <". $email .">\r\n";
                $headers .= "MIME-Version: 1.0\r\n";
                $headers .= "Content-Type: multipart/alternative;\n boundary=" . $mime_boundary_header ;
                $headers .= "\n--$boundary\n"; // beginning \n added to separate previous content
                $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
                $headers .= "\n--$boundary\n";
                $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
                $headers .= "Content-Transfer-Encoding:base64\r\n";
$body = "

--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

". strip_tags($messageBody) ."

--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding:base64

". chunk_split(base64_encode($messageBody)) ."

--$mime_boundary--";

mail(null,$sub,$body,$headers,"-f".$email); 

答案 1 :(得分:0)

为什么不使用MailChimp呢?它似乎具有您想要的所有功能。