通用发送电子邮件html或纯文本PHP [英] Universal Send email html or plain text php

查看:190
本文介绍了通用发送电子邮件html或纯文本PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Been there for days wraps over the fact that I can create a script that sends HTML mail template, and in case the user has a client who can not read HTML and plain text listing is possibly the receiving HTML email off.

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

Please some universal advice on how to do it. I went through the whole internet and I really do not know.

非常感谢您的建议。

推荐答案

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

You should add html and plain text version of email called multipart/alternative in same email body:

<?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); 

这篇关于通用发送电子邮件html或纯文本PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆