我想在电子邮件中同时发送html和纯文本。我找到this pretty straightforward tutorial但它使用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);
答案 0 :(得分:20)
见下文:
$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使用对您来说可能是可选的。
答案 1 :(得分: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-&gt; Body和$ this-&gt; AltBody
中的任何现有值
在Github上查看完整功能的代码: https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php#L3299
答案 2 :(得分:0)
试试这个:
$mail->SMTPDebug = 2;
调试级别
将 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() 之前不会得到任何输出,因为在调用之前不会发生 SMTP 会话。
链接:https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging