如何使用phpmail在电子邮件正文上添加动态图像


How to Add dynamic image on body of Email using phpmail

>我正在创建一个电子邮件服务,它使用phpmail将不同的图像发送给不同的人。我可以根据需要发送邮件和附件,但是在邮件正文中动态添加嵌入图像时。我将无法取得成功。

<?php
require_once('class.phpmailer.php');
// multiple recipients
$to = $arr['Contact.Email']; // note the comma
$mail = new PHPMailer(true); // the true param means it will throw exceptions on     errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try{
  $mail->SetFrom($arr['Contact.Email'], 'First Last');
  $mail->AddAddress($arr['Contact.Email'], 'John Doe');
  $mail->Subject = 'PHPMailer Test';
  $mail->AddEmbeddedImage($arr['ContactId'].'.png', 'my-attach');
  $mail->Body = 'Your <b>HTML</b> with an embedded Image: <img src=cid:my-attach''> Here is an image!';
  $mail->AddAttachment($arr['ContactId'].".png"); // this is a regular attachment (Not inline)
  $mail->Send();
  echo "Message Sent OK<p></p>'n";
} 
catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
catch (Exception $e) {
    echo $e->getMessage(); 
}
?>

我可以嵌入一个图像,但是当我尝试为不同的用户发送不同的图像时,出现错误。我到处寻找,但没有得到任何令人满意的答案。任何帮助将不胜感激。

您在以下位置出现错误:

$mail->Body = 'Your <b>HTML</b> with an embedded Image: <img src=cid:my-attach''> Here is an image!';

您需要在附加后跳过 '''

您还需要通过添加以下内容将消息设置为 html 格式:

$mail->IsHTML(true);

在此处阅读有关在文档中嵌入图像的更多信息:http://phpmailer.worxware.com/?pg=tutorial