PHPMailer允许mime边界可见

时间:2019-02-23 08:19:24

标签: smtp phpmailer

我正在使用PHPMailer通过smtp-relay.gmail.com发送电子邮件-see previous post通过G-Suite建立帐户后,我的凭据已被接受,但是当电子邮件发送完毕后,我可以看到纯文本版本以及html版本,以及其他一些字符:

    ------example.com----250cd4bbb8be52d828379181e485c269 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Order Received

...

 ------example.com----250cd4bbb8be52d828379181e485c269 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

后跟html版本,其结尾为:

------example.com----250cd4bbb8be52d828379181e485c269-- 

当我使用香草版本时,它工作得很好,没有看到纯文本或mime边界数据:

$send = mail($recpEmails, $subject, $htmlMessage, $headers);

变量$ htmlMessage仍然具有与以前相同的信息,但是现在PHPMailer通过以下行发送该信息:

$mail->Body = $htmlMessage;

我看不到纯文本或其他带有破折号的行。为什么通过PHPMailer> smtp-relay.gmail.com发送会更改结果?

是因为我添加了以下行吗?

$mail->IsHTML(true);

是由于下一行吗?如果是的话,我应该将其设置为什么?

$mail->SMTPDebug = 3;

我需要更改G-Suite> email>高级设置中的配置吗?

这是上一篇文章的更新代码:

  $mail = new PHPMailer(true);

  $mail->isSMTP();
  $mail->SMTPDebug = 3; 
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = "tls";
  $mail->Host = "smtp-relay.gmail.com";
  $mail->Port = "587";
  $mail->Username = "info@example.com";
  $mail->Password = "somePassword";
  $mail->setFrom("info@example.com");
  $mail->isHTML(true);
  $mail->Subject = $subject;
  $mail->Body = $htmlMessage;
  $mail->addAddress($recpEmails);
  $mail->Send();

预先感谢

1 个答案:

答案 0 :(得分:0)

结果是答案在以下stackoverflow post

我以前通过以下方式通过PHP的邮件在纯文本和html版本之间建立mime边界:

创建文本消息-这应该与您的html完全相同,以避免被标记为垃圾邮件:

$plain_text = ""; // same words that will appear in the html

开始创建mime边界:

# -=-=-=- MIME BOUNDARY
$mime_boundary = "----example.com----".md5(time());
# -=-=-=- MAIL HEADERS

创建主题:

$subject = "example.com\n";

创建标题,其中将包含您之前开始的mime边界。以及在标头中-在第一个mime边界之间添加纯文本消息

$headers = "From: example.com <cs@example.com>\n";
$headers .= "Reply-To: example.com <cs@example.com>\n";
$headers .= "BCC: example.com <info@example.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";

$htmlMessage = "--$mime_boundary\n";
$htmlMessage .= "Content-Type: text/plain; charset=us-ascii\n";
$htmlMessage .= "Content-Transfer-Encoding: 7bit\n";
$htmlMessage .= "$plain_text\n";

$htmlMessage .= "--$mime_boundary\n";
$htmlMessage .= "Content-Type: text/html; charset=UTF-8\n";
$htmlMessage .= "Content-Transfer-Encoding: 8bit\n\n";
$htmlMessage .= "<html>\n";
$htmlMessage .= "<body>\n";
$htmlMessage .= "" // has to be the same words as the plain text to avoid being marked as spam
$htmlMessage .= "</body>\n";
$htmlMessage .= "</html>\n";

完成mime边界:

# -=-=-=- FINAL BOUNDARY
$htmlMessage .= "--$mime_boundary--\n\n";
# -=-=-=- SEND MAIL

但是,使用PHPMailer,使用以下两行代码不再需要所有这些代码:

  $mail->AltBody = $plain_text; 
  $mail->Body = $htmlMessage;

以下是我测试和使用的内容:

  $mail = new PHPMailer(true);

  $mail->isSMTP();
  $mail->SMTPDebug = 3; 
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = "tls";
  $mail->Host = "smtp-relay.gmail.com";
  $mail->Port = "587";
  $mail->Username = "cs@example.com";
  $mail->Password = "somePassword";
  $mail->setFrom("cs@example.com");
  $mail->isHTML(true);
  $mail->Subject = $subject;
  $mail->AltBody = $plain_text; 
  $mail->Body = $htmlMessage;
  $mail->addAddress($recpEmails);
  $mail->Send();

几乎忘了,但这是我添加密件抄送的方式:

  $mail->addBCC("info@example.com");