如何将phpmailer与utf8与外部主机一起使用 [英] How to use phpmailer with utf8 with external host

查看:78
本文介绍了如何将phpmailer与utf8与外部主机一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以肯定你们大多数人已经做过类似的事情,但是即使我尝试过一些杂乱的事情,我也无法以某种方式在phpmailer的代码上为HTML格式的主题使用正确的字符集.你们能抓住下面的代码可能出什么问题吗?

I am sure most of you have already done with kind of thing but i am unable to use correct charset for subject with html format on this code for phpmailer somehow eventhough i've tried hundereds of way. Could you guys catch what could be wrong with the code below?

如果我将"Content-Type:text/html"替换为"Content-Type:text/plain",则utf8仅适用于邮件正文,但不适用于主题仍然会收到错误的标题警告,此外邮件似乎不是html格式任何一个.我应该怎么写才能在此代码中使用外部邮件服务器?

If i replace "Content-Type: text/html" to "Content-Type: text/plain" then utf8 works fine for only message body but not for subject still gets bad header warning besides mail does not seem as html format either. And what should i write to use an external mail server into this code ?

$message =  "Hi $uname,\r\n\r\n\r\n";
$message .= "$passwordLink\r\n\r\n\r\n";
$message .= "you just hit password recovery request\r\n;
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "From: Password Service <service@domain.com> \n";
$headers .= "To-Sender: \n";
$headers .= "X-Mailer: PHP\n"; 
$headers .= "Reply-To: noreply@domain.com\n"; 
$headers .= "Return-Path: noreply@domain.com\n"; 
$headers .= "Content-Type: text/html; charset=utf-8";
$host     = "mailgw.domain.com";
$subject = "Pass reminder service";
@mail($email,$subject,$message,$headers);
return str_replace("\r\n","<br/>",$message);

预先感谢

推荐答案

通过此函数要使用外部服务器,我建议使用PHPMailer类 https://github.com/PHPMailer/PHPMailer,因此您可以执行以下操作:

To use external server I would recommend to use PHPMailer class https://github.com/PHPMailer/PHPMailer, so you could do something like this:

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;  

...

这篇关于如何将phpmailer与utf8与外部主机一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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