Отправлять html и текстовое сообщение одновременно с 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