使用 gmail smtp 发送邮件、换行符和发件人 header
Mail Sending, Line break & and From header using gmail smtp
我用 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显示为字符并且没有换行符,我做错了什么?
我遇到的另一个问题是表单方法 dosent 工作,我发送电子邮件尝试 nati323@gmail.com,然后我使用 nati323@gmail.com 帐户连接到 smtp 服务器,然后我总是从我自己那里得到消息,我定义的表格显示....
调用PHP函数nl2br()
将换行符转换为HTML换行符。然后,将 PHPMailer 设置为使用 $mail->IsHTML(TRUE);
作为 html 发送电子邮件
您在邮件中使用了单引号。这将按字面意思获取其中包含的所有内容,这就是为什么您会得到文字字符串 \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";`
我用 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显示为字符并且没有换行符,我做错了什么?
我遇到的另一个问题是表单方法 dosent 工作,我发送电子邮件尝试 nati323@gmail.com,然后我使用 nati323@gmail.com 帐户连接到 smtp 服务器,然后我总是从我自己那里得到消息,我定义的表格显示....
调用PHP函数nl2br()
将换行符转换为HTML换行符。然后,将 PHPMailer 设置为使用 $mail->IsHTML(TRUE);
您在邮件中使用了单引号。这将按字面意思获取其中包含的所有内容,这就是为什么您会得到文字字符串 \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";`