PHP Send UTF-8 mail without PEAR::Mail PEAR::Mail_Mime

浪尽此生 提交于 2019-12-01 09:13:54

问题


I would like to be able to send e-mails with PHP mail() containing the 8-bit characters åäö. They will be used in the subject, the message and in the "From:"-header. How can I do this without using the PEAR packages?


回答1:


Simplest solution if you don't mind encoding even words that don't need it is to put everything in a base64 RFC 2047 encoded-word:

$subject= "=?utf-8?b?".base64_encode($subject)."?=";
$body= "blah blah $utf8text blah";
$headers= "MIME-Version: 1.0\r\n";
$headers.= "From: =?utf-8?b?".base64_encode($fromname)."?= <$fromaddress>\r\n";
$headers.= "Content-Type: text/plain;charset=utf-8";

mail($toaddress, $subject, $body, $headers);



回答2:


Use the swiftmailer library. http://swiftmailer.org/




回答3:


$headers = array('From' => $from,
    'To' => "<$to>",
    'Subject' => $subject);

if ($is_html) {
    $headers['Content-type'] = "text/html; charset=UTF-8";
} else {
    $headers['Content-type'] = "text/plain; charset=UTF-8";
}

This works for me



来源:https://stackoverflow.com/questions/1500690/php-send-utf-8-mail-without-pearmail-pearmail-mime

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