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


Send html and plain text email simultaneously with PHP Mailer

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

//specify the email address you are sending to, and the email subject
$email = 'email@example.com';
$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);

见下:

$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("reply@thehost.com", "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);
}

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

在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();

从HTML字符串创建消息。自动修改内联图像和背景并通过转换HTML创建纯文本版本。重写$this->Body和$this->AltBody

中已有的值

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

试试这个:

$mail->SMTPDebug = 2;
<<p> 调试水平/strong>

PHPMailer->SMTPDebug属性设置为这些数字或常量(在SMTP类中定义)会导致不同的输出量:

  • SMTP::DEBUG_OFF (0):关闭调试(也可以不打开)完全输出,默认为0)。
  • SMTP::DEBUG_CLIENT (1):客户端发送的输出消息。
  • SMTP::DEBUG_SERVER (2):作为1,加上从(这是最有用的设置)。
  • SMTP::DEBUG_CONNECTION (3):作为2,加上更多关于初始连接-此级别可以帮助诊断STARTTLS故障。
  • SMTP::DEBUG_LOWLEVEL (4):为3,甚至更低信息,非常冗长,不要用于调试SMTP,只低级问题。

你不需要使用高于2的级别,除非你在连接上有困难——它只会使输出更冗长,更难以阅读。

请注意,在调用send()之前不会得到任何输出,因为在调用send()之前没有SMTP会话发生。

: https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging