通过PHPMailer从URL发送带有附件的电子邮件


Sending e-mail via PHPMailer with attachments from URL

我正在使用PHPMailer。我必须发送两个附件,两个都是链接(一个是base64图像,第二个是url)。

// Variables with images
$croped_img = 'data:image/jpg;base64,/extreme_long_code_here';
$photo_thumbnail_url = 'http://t2.ftcdn.net/jpg/00/76/44/75/400_F_76447580_gOWAV0P8APW0iC51OsW5huD6qoiEsh7O.jpg';
// PHPMailer
$mail->addAttachment('$photo_thumbnail_url', 'selected.jpg');
$mail->addAttachment('$croped_img', 'croped_img.jpg');

PHPMailer之所以有效,是因为如果我以这种形式发送电子邮件,附件就不会出现。但如果我链接本地图像,例如'images/abc.jpg',那么它们就来了。

我应该使用哪些php函数将变量作为电子邮件附件发送?

一如既往,确保您使用的是PHPMailer的最新版本。

您需要使用addStringAttachment,此处的文档:

$photo_thumbnail_url = 'http://t2.ftcdn.net/jpg/00/76/44/75/400_F_76447580_gOWAV0P8APW0iC51OsW5huD6qoiEsh7O.jpg';
$mail->addStringAttachment(file_get_contents($photo_thumbnail_url), 'selected.jpg');

msgHTML函数自动处理data URL,将它们转换为具有自动cid值的嵌入图像,因此,如果您的图像已经嵌入到HTML中,则无需为其工作做任何操作。

如果你想自己处理数据URL转换,你可以在这里看到它是如何工作的。

确保您的PHPMailer库是最新的!

然后试试这个

$mail->AddEmbeddedImage($photo_thumbnail_url, 'selected.jpg');
$mail->AddEmbeddedImage($croped_img, 'croped_img.jpg');