2014-03-19 72 views
5

我想在電子郵件中同時發送html和純文本。我發現this pretty straightforward tutorial,但它使用mail()函數發送電子郵件。 如何將此代碼轉換爲PHP Mailer類發送電子郵件?用PHP同時發送html和純文本郵件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); 

回答

16

見下文:

$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); 
} 

感謝http://snippets.aktagon.com/snippets/129-how-to-send-both-html-and-text-emails-with-php-and-phpmailer和谷歌。 SMTP的使用對你來說可能是可選的。

+1

精彩!我GOOGLE了這一點,但無法找到任何關於PHP Mailers AltBody屬性。像魅力一樣工作,我的電子郵件剛剛在http://www.mail-tester.com上收到了9.9而不是8.3! – erdomester

+0

任何我需要照顧的東西,即純文本身體看起來應該如何?我的新行總是被刪除......'$ message ['plaintext'] = $ PHPMailer-> html2text($ message ['html']);' – user2966991

+0

嘗試'$ PHPMailer-> html2text(nl2br($ message ['html' ]))' –

3

有一個在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->車身$這個 - > AltBody

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