我正在使用PHPMailer发送HTML电子邮件。我首先在调用ob_start
后使用HTML和PHP变量设置电子邮件的HTML正文。
这是我的代码:
<?php
ob_start();
?>
..... a bunch of VALID HTML
<?
$body = ob_get_contents();
require_once('class.phpmailer.php');
// Send to Me
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSendmail(); // telling the class to use SendMail transport
try {
$mail->AddReplyTo('info@example.com', 'Company Name');
$mail->AddAddress('me@example.com', 'My Name');
$mail->SetFrom('info@example.com', 'Company Name');
$mail->Subject = "Contact Form Confirmation";
$mail->AltBody = "This is a contact form submitted through the main website.";
$mail->WordWrap = 50; // set word wrap
$mail->Body = $body;
$mail->IsHTML(true); // send as HTML
$mail->Send(); // Try to send email
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
// end try
出于某种原因,这一行:
$mail->Body = $body;
导致电子邮件无法发送。如果我将其替换为:
$mail->Body = "This is a test email.";
电子邮件发送完美。
如果$body
中包含的HTML只是一个有头脑的CSS的有效HTML页面,而且没有Javascript或其他内容,为什么电子邮件不会发送?
还有其他办法吗?请帮忙!!