Eek*_*Eek 4 email wordpress base64 attachment
我有个问题.
我想将图像附加到电子邮件中,并将其内联显示,以及其他一些php生成的内容.问题是我没有丝毫的想法如何使用内联wp_mail使用的文件附件数组来附加.
我的解决方案是在base64中对图像进行编码,并将它们内联为html,如下所示:
<img alt="The Alt" src="data:image/png;base64,*etc*etc*etc" />
Run Code Online (Sandbox Code Playgroud)
但问题是Gmail/Outlook会从图像中删除src数据.所以它落地了
<img alt="The Alt" />
Run Code Online (Sandbox Code Playgroud)
有什么线索要修改(使用base64的头文件)或如何使用附件将它们嵌入内联?
谢谢,拉杜.
Con*_*tin 19
wp_mail使用PHPMailer该类.此类具有内联附件所需的所有功能.要在wp_mail()发送电子邮件之前更改phpmailer对象,您可以使用过滤器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 ;)';
Run Code Online (Sandbox Code Playgroud)
这是如何在您的电子邮件正文中插入图片的示例.
$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);
Run Code Online (Sandbox Code Playgroud)
就这样.
我需要以一种更好的方式做到这一点,因为我一步发送多封邮件,并且并非所有邮件都应该具有相同的嵌入图像。所以我使用康斯坦丁的这个解决方案,但进行了修改:-)
wp_mail('test@example.org', 'First mail without attachments', 'Test 1');
$phpmailerInitAction = function(&$phpmailer) {
$phpmailer->AddEmbeddedImage(__DIR__ . '/img/header.jpg', 'header');
$phpmailer->AddEmbeddedImage(__DIR__ . '/img/footer.png', 'footer');
};
add_action('phpmailer_init', $phpmailerInitAction);
wp_mail('test@example.org', 'Mail with embedded images', 'Example <img src="cid:header" /><br /><img src="cid:footer" />', [
'Content-Type: text/html; charset=UTF-8'
], [
__DIR__ . '/files/terms.pdf'
]);
remove_action('phpmailer_init', $phpmailerInitAction);
wp_mail('test@example.org', 'Second mail without attachments', 'Test 2');
Run Code Online (Sandbox Code Playgroud)
第一个wp_mail将没有附件。第二个wp_mail将包含嵌入图像。第三个wp_mail将没有附件。
目前运行良好
| 归档时间: |
|
| 查看次数: |
8817 次 |
| 最近记录: |