使用 PHP Mailer 同时发送 html 和纯文本电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22507176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:58:15  来源:igfitidea点击:

Send html and plain text email simultaneously with PHP Mailer

php

提问by erdomester

I would like to send both html and plain text simultaneously in an email. I found this pretty straightforward tutorialbut it used the mail() function to send the email. How do I transform this code send an email with the PHP Mailer class?

我想在电子邮件中同时发送 html 和纯文本。我发现这个教程非常简单,但它使用了 mail() 函数来发送电子邮件。如何转换此代码使用 PHP Mailer 类发送电子邮件?

//specify the email address you are sending to, and the email subject
$email = '[email protected]';
$subject = 'Email Subject';

//create a boundary for the email. This 
$boundary = uniqid('np');

//headers - specify your from email address and name here
//and specify the boundary for the email
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Your Name \r\n";
$headers .= "To: ".$email."\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

//here is the content body
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";

//Plain text body
$message .= "Hello,\nThis is a text email, the text/plain version.
\n\nRegards,\nYour Name";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";

//Html body
$message .= "
 Hello,
This is a text email, the html version.

Regards,
Your Name";
$message .= "\r\n\r\n--" . $boundary . "--";

//invoke the PHP mail function
mail('', $subject, $message, $headers);

回答by Half Crazed

See below:

见下文:

$mail = new PHPMailer();

$mail->IsHTML(true);
$mail->CharSet = "text/html; charset=UTF-8;";
$mail->IsSMTP();

$mail->WordWrap = 80;
$mail->Host = "smtp.thehost.com"; 
$mail->SMTPAuth = false;

$mail->From = $from;
$mail->FromName = $from; // First name, last name
$mail->AddAddress($to, "First name last name");
#$mail->AddReplyTo("[email protected]", "Reply to address");

$mail->Subject =  $subject;
$mail->Body =  $htmlMessage; 
$mail->AltBody  =  $textMessage;    # This automatically sets the email to multipart/alternative. This body can be read by mail clients that do not have HTML email capability such as mutt.

if(!$mail->Send())
{
  throw new Exception("Mailer Error: " . $mail->ErrorInfo);
}

Thanks to http://snippets.aktagon.com/snippets/129-how-to-send-both-html-and-text-emails-with-php-and-phpmailerand Google. SMTP use might be optional for you.

感谢http://snippets.aktagon.com/snippets/129-how-to-send-both-html-and-text-emails-with-php-and-phpmailer和谷歌。SMTP 的使用对您来说可能是可选的。

回答by Fran?ois J.

There is a function for that in PHPMailer: msgHTML()

PHPMailer 中有一个函数: msgHTML()

# Minimum code to send an email:
$phpmailer = new PHPMailer();

$phpmailer->From = $from;
$phpmailer->FromName = $from_name;

$phpmailer->AddAddress( $to, $to_name );

$phpmailer->Subject = $subject;

$phpmailer->CharSet = 'UTF-8';

$phpmailer->msgHTML( $htmlMessage ); # <- right here!

# Use PHP's mail() instead of SMTP:
$phpmailer->IsMail();

$phpmailer->Send();

Create a message from an HTML string. Automatically makes modifications for inline images and backgrounds and creates a plain-text version by converting the HTML. Overwrites any existing values in $this->Body and $this->AltBody

从 HTML 字符串创建消息。自动对内嵌图像和背景进行修改,并通过转换 HTML 创建纯文本版本。覆盖 $this->Body 和 $this->AltBody 中的任何现有值

See full function's code on Github: https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php#L3299

在 Github 上查看完整的函数代码:https: //github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php#L3299