使用phpMailer和isHTML时出错

时间:2016-06-13 07:30:01

标签: php html phpmailer

我使用phpMailer发送附件和HTML格式的图像。但该页面显示我无效。

include 'PHPMailer-master/class.phpmailer.php';
$mail = new PHPMailer();
$mail->From      = 'someone@some.com';
$mail->FromName  = 'someone';

$mail->AddAddress( 'someone@some2.com' );
$mail->Subject   = 'Message Subject';

$mail->Body      = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
$mail->AltBody="This is text only alternative body.";
$mail->IsHTML(true);

$file_to_attach = '../images/logo.png';
$mail->AddAttachment( $file_to_attach , 'logo.png"' );
if(!$mail->Send())
    echo "Error sending: " . $mail->ErrorInfo;;
else
    echo "Letter is sent";

使用此代码非常适合我发送带有HTML和附件的邮件。

<?php
require_once 'PHPMailer-master/class.phpmailer.php';

$mail = new PHPMailer(true);
try {
$mail->AddAddress('someone@some.com', 'a name');
$mail->SetFrom('someone2@some2.com', 'another name');

$mail->AddReplyTo('someone3@some3.com', 'another name');
$mail->Subject = 'PHPMailer Test';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->IsHTML(true);
$mail->Body      = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
$mail->AddAttachment('../images/logo.png');
$mail->Send(); echo "Message Sent OK\n";
}
catch (phpmailerException $e) {
    echo $e->errorMessage();
}
catch (Exception $e) {
    echo $e->getMessage();
}
?>

2 个答案:

答案 0 :(得分:0)

您正在使用$ email变量到新对象PHPMailer,但在第8,9和14,15行中您正在使用$ mail。

$mail->AltBody="This is text only alternative body."; $mail->IsHTML(true); if(!$mail->Send()) echo "Error sending: " . $mail->ErrorInfo;

只需使用$ email。

答案 1 :(得分:-1)

我看到你正在使用

$file_to_attach = '../images/logo.png'; 

获取图像文件的名称。但是文件名已设置为:

$email->AddAttachment( $file_to_attach , 'logo.png"' );

尝试将整个路径放在$email->AddAttachment();这样的$email->AddAttachment('../images/Logo.pgn');