How can i attach an image using Phpmailer?

假如想象 提交于 2019-11-27 16:21:10

问题


I am using phphmailer and have attached an image, it shows only the image like an icon rather than image itself here is my code could you help.

$mail->AddEmbeddedImage('2.jpg', '2img', '2.jpg');
$mail->Subject  =  "Order Form:  Contact form submitted";
$mail->Body     =  $body . 'img src="../../photo/2img" ;

note: i have dropped html tag befor img as I get error sending this Q.


回答1:


Per PHPMailer Manual, you sould use the method AddEmbeddedImage

$mail->AddEmbeddedImage(filename, cid, name);
By using this function with this example's value above, results in this code:
$mail->AddEmbeddedImage('my-photo.jpg', 'my-photo', 'my-photo.jpg '); 

like this:

$mail->AddEmbeddedImage("rocks.png", "my-attach", "rocks.png");
$mail->Body = 'Embedded Image: <img alt="PHPMailer" src="cid:my-attach"> Here is an image!';

so cid:my-attach will be replaced with the inline-source of the image that's inside the email body




回答2:


Using the AddEmbeddedImage() function works well in showing the embedded image in the web based emails. However, Yahoo always adds it as an attachment too. To overcome this problem, you can safely disregard the AddEmbeddedImage() and link to the full path of the image on your server and PHPMailer has the capacity of converting it to CID and It will correctly show as embedded image and Yahoo won't add it as an attachment anymore.

In the html body of the message, add it like usual:

<img src="http://PATH-TO-IMAGE" alt='THIS IS THE IMAGE" />



来源:https://stackoverflow.com/questions/6095285/how-can-i-attach-an-image-using-phpmailer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!