如何使用phpmail在电子邮件正文上添加动态图像(How to Add dynamic image on body of Email using phpmail)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
如何使用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(); } ?>

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

I am creating a email service which send different image to different person using phpmail.I can send mail and attachment as i require but when it comes to dynamically adding embedded image in body of mail. I wont be able to achieve success.

<?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(); } ?>

I can embed one image but when i try to send different image for different user i get an error.I searched everywhere but i did not get any satisfactorily answer. any help will be appreciated.

最满意答案

你有一个错误:

$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

You have an error at:

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

You need to skip the \' after attach.

You also need to set the message to html format by adding:

$mail->IsHTML(true);

Read more about embedding images in the docs here: http://phpmailer.worxware.com/?pg=tutorial

更多推荐

本文发布于:2023-04-20 18:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/9f2f5d4d9826b8d59943268ebd254746.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   图像   电子邮件   动态   正文

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!