邮件发送,换行符&和使用gmail smtp的头文件

时间:2015-05-18 22:23:02

标签: php email phpmailer

我用PHPMailer发送电子邮件,我的代码是:

$mail = new PHPMailer(true);
$mail->Mailer       = "smtp";
$mail->IsSMTP();  // telling the class to use SMTP
$mail->SMTPDebug    = 0; 
$mail->SMTPSecure = 'tls';
$mail->Host     = "smtp.gmail.com"; // SMTP server
$mail->Username   = "nati323@gmail.com";
$mail->Password   = "****";
$mail->addCustomHeader('Content-Type: text/plain; charset="utf-8"');
$mail->SMTPAuth   = true;       
$mail->Port       = 587;
$mail->From     = $from;
$mail->AddAddress($to);
$mail->Subject  = $subject;
$mail->Body     = $msg;
return $mail->Send();

$ msg变量是一个函数参数,我这样使用它:

function sendMail ($to, $subject, $msg, $from, $add = null)

我这样使用它:

sendMail($_POST['mail'], $text['EMAIL_RECOVER_MSG_SUBJECT'], $text['EMAIL_RECOVER_MSG'], $from)

变量包含:

$text['EMAIL_RECOVER_MSG_SUBJECT'] = 'Password Recover: From dmworld24.com';
$text['EMAIL_RECOVER_MSG'] = 'You Forget Your Password To Recover it Please Go Into The Next Link:\r\n http://dmworld24.com/login.php?act=rec&token={TOKEN}&mail={MAIL} \r\n If You Didnt Ask For Password Recovering Just Ignore This Message';
$from = 'System@dmworld24.com';

现在我尝试在消息中添加换行符,例如:

You Forget Your Password To Recover it Please Go Into The Next Link:\r\n http://dmworld24.com/login.php?act=rec&token=7054343021*****353214&mail=nati323@gmail.com \r\n If You Didnt Ask For Password Recovering Just Ignore This Message

我收到的邮件是一样的,\ r \ n显示为字符,没有换行符,我做错了什么?

我有另一个问题,表格方法有效,我发送电子邮件尝试nati323@gmail.com,我用nati323 @gmail.com acount连接到smtp服务器,我总是收到来自我的消息自我,以及我定义的表格剂量表示......

2 个答案:

答案 0 :(得分:2)

您在邮件中使用单引号。这将把字面上包含的所有东西都包含在内,这就是为什么你得到文字字符串\ r \ n。

用双引号替换单引号以正确输出新行。

$text['EMAIL_RECOVER_MSG'] = "You Forget Your Password To Recover it Please Go Into The Next Link:\r\n http://dmworld24.com/login.php?act=rec&token={TOKEN}&mail={MAIL} \r\n If You Didnt Ask For Password Recovering Just Ignore This Message";`

答案 1 :(得分:0)

调用PHP函数nl2br()将换行符转换为HTML换行符。然后,设置PHPMailer使用$mail->IsHTML(TRUE);

将电子邮件发送为html