【问题标题】:Mail Sending, Line break & and From header using gmail smtp使用 gmail smtp 的邮件发送、换行符和 From 标头
【发布时间】:2015-07-30 13:52:29
【问题描述】:

我用 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 帐户连接到 smtp 服务器,我总是从我的自我,以及我定义的表单显示......

【问题讨论】:

  • 你能提供你设置 $msg 的行吗?
  • @JonStirling 请看我的编辑

标签: php email phpmailer


【解决方案1】:

您在消息中使用了单引号。这将从字面上理解其中包含的所有内容,这就是为什么您会得到文字字符串 \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";`

【讨论】:

【解决方案2】:

调用 PHP 函数 nl2br() 将换行符转换为 HTML 换行符。然后,将 PHPMailer 设置为使用 $mail->IsHTML(TRUE); 以 html 格式发送电子邮件

【讨论】:

    猜你喜欢
    • 2017-11-04
    • 1970-01-01
    • 2017-11-14
    • 2021-01-12
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 2013-02-03
    相关资源
    最近更新 更多