Is it possible to send mixed txt/html mails with the php mail function?

五迷三道 提交于 2019-12-24 23:29:55

问题


I'm doingsome tests, I tried two solutions so far:

The first one sends the message inside the headers (message parameter of the mail() function is empty) []

    $boundary = "nextPart";
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "From: ".$from_name." <".$from.">\r\n";
    $headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n";
    //Html
    $headers .= "\n--$boundary\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= $html;
    //Text
    $headers .= "\n--$boundary\n";
    $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
    $headers .= $text;

The second one is this one: http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment (set headers and sends them inside the message)

Non of them works properly (the first one doesn't work at all, the second works in gmail but it's not a properly formatted email, and some client can't handle it). Dissecting the code of php mailer (http://phpmailer.worxware.com/index.php?pg=phpmailer) i saw that it doesn't even try to send multipart emails.

So I'm wondering is it possible to send PROPERLY formatted multipart email with the php mail function.

Thank you

p.s. I know and use pear mail, I just want to understand this thing.


回答1:


yes, it is possible. PHP's mail(); function is nothing more than a handy function to deliver your 'raw' message to the underlying operating system's mail handler (sendmail for example).

So if your message headers- and message body combined are a correct MIME encoded message, things will work fine.

To figure out the details, find a simple mail client and analyse it's emails. Combine this with the MIME specifications and you should get there.




回答2:


From what i know, mail() just sends what it's given, so yes, it's perfectly possible to send whatever you want, including multipart mimes. In fact, this is exactly what mail classes (pear, phpmailer) do behind the scenes, although they can be configured to use other transports as well.

In other words, a mail class compares to "mail()" like a template engine compares to "echo".



来源:https://stackoverflow.com/questions/3675605/is-it-possible-to-send-mixed-txt-html-mails-with-the-php-mail-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!