使用wp_mail显示内嵌图像附件 [英] Display inline image attachments with wp_mail
问题描述
我有问题.
我想将图像附加到电子邮件中,并与其他一些php生成的内容一起内联显示.问题是我没有丝毫想法如何使用内联wp_mail用于附加的文件附件数组.
I would like to attach an image to an email and also display it inline, with some other php-generated content. The problem is I don't have the slightest ideea how to use inline a file attachment array used by wp_mail to attach.
我的解决方案是在base64中对图像进行编码,然后将它们放入HTML内联中,如下所示:
My solution was to encode the images in base64 and put them inline the HTML like this:
<img alt="The Alt" src="data:image/png;base64,*etc*etc*etc" />
但是问题是Gmail/Outlook从图像中删除了src数据.所以它降落为
But the problem is that Gmail / Outlook remove the src data from the image. So it lands as
<img alt="The Alt" />
是否有任何线索可以修改(标题可以与base64一起使用)或如何使用附件将它们嵌入到行内?
Any clues what to modify (headers to work with base64) or how to use attachment to embed them inline?
推荐答案
wp_mail
使用 PHPMailer
类.此类具有内联附件所需的所有功能.要在wp_mail()发送电子邮件之前更改phpmailer对象,可以使用过滤器 phpmailer_init
.
wp_mail
uses the PHPMailer
class. This class has all the functionality needed for inline attachments.
To change the phpmailer object before wp_mail() sends the email you could use the filter phpmailer_init
.
$body = '
Hello John,
checkout my new cool picture.
<img src="cid:my-cool-picture-uid" width="300" height="400">
Thanks, hope you like it ;)';
这是如何在电子邮件正文中插入图片的示例.
That was an example of how to insert the picture in you email body.
$file = '/path/to/file.jpg'; //phpmailer will load this file
$uid = 'my-cool-picture-uid'; //will map it to this UID
$name = 'file.jpg'; //this will be the file name for the attachment
global $phpmailer;
add_action( 'phpmailer_init', function(&$phpmailer)use($file,$uid,$name){
$phpmailer->SMTPKeepAlive = true;
$phpmailer->AddEmbeddedImage($file, $uid, $name);
});
//now just call wp_mail()
wp_mail('test@example.com','Hi John',$body);
仅此而已.
这篇关于使用wp_mail显示内嵌图像附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!