Send html and plain text email simultaneously with PHP Mailer

做~自己de王妃 提交于 2019-12-17 18:32:18

问题


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

//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);

回答1:


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("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);
}

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




回答2:


There is a function for that in 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

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



来源:https://stackoverflow.com/questions/22507176/send-html-and-plain-text-email-simultaneously-with-php-mailer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!