使用wp_mail显示内联图像附件



我有一个问题。

我想附加一个图像到电子邮件,也显示它内联,与其他一些php生成的内容。问题是我不知道如何使用内联wp_mail使用的文件附件数组来附加。

我的解决方案是在base64编码的图像,并把它们内联的HTML,像这样:

<img alt="The Alt" src="data:image/png;base64,*etc*etc*etc" />

但是问题是Gmail/Outlook会从图像中删除src数据。所以它的位置是

<img alt="The Alt" />

任何线索修改(头与base64工作)或如何使用附件嵌入它们内联?

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 ;)';

这是一个如何在邮件正文中插入图片的例子。

$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('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');

第一个wp_mail将不带附件。第二个wp_mail将包含嵌入的图像。第三个wp_mail将不带附件。

目前运行正常😎

如果你得到一个意想不到的T_FUNCTION错误,这是由于PHP版本<5.3. 在这种情况下,创建一个函数以更传统的方式完成:

function attachInlineImage() {  
  global $phpmailer;  
  $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  
  if (is_file($file)) {  
    $phpmailer->AddEmbeddedImage($file, $uid, $name);  
  }  
}  
add_action('phpmailer_init','attachInlineImage');  

我创建这个类是为了管理在邮件正文中添加图像并清理图像。

如果你定义了SENDGRID_PASSWORD,它将使用Sendgrid而不是你的服务器发送电子邮件

这篇文章是一步一步地指导如何使用wordpress

在邮件正文中嵌入图像。https://codewriteups.com/embed-images-in-email-body-using-wp_mail-and-phpmailer

<?php
/*
 * Send HTML Emails with inline images
 */
class Custom_Mailer
{
    public $email_attachments = [];
    public function send($to, $subject, $body, $headers, $attachments)
    {
        /* Used by "phpmailer_init" hook to add attachments directly to PHPMailer  */
        $this->email_attachments = $attachments;
        /* Setup Before send email */
        add_action('phpmailer_init', [$this, 'add_attachments_to_php_mailer']);
        add_filter('wp_mail_content_type', [$this, 'set_content_type']);
        add_filter('wp_mail_from', [$this, 'set_wp_mail_from']);
        add_filter('wp_mail_from_name', [$this, 'wp_mail_from_name']);
        
        /* Send Email */
        $is_sent = wp_mail($to, $subject, $body, $headers);
        
        /* Cleanup after send email */
        $this->email_attachments = [];
        remove_action('phpmailer_init', [$this, 'add_attachments_to_php_mailer']);
        remove_filter('wp_mail_content_type', [$this, 'set_content_type']);
        remove_filter('wp_mail_from', [$this, 'set_wp_mail_from']);
        remove_filter('wp_mail_from_name', [$this, 'wp_mail_from_name']);
        return $is_sent;
    }
    public function add_attachments_to_php_mailer(&$phpmailer)
    {
        $phpmailer->SMTPKeepAlive=true;
        
        /* Sendgrid */
        if (defined('SENDGRID_PASSWORD')) {
            $phpmailer->IsSMTP();
            $phpmailer->Host="smtp.sendgrid.net";
            $phpmailer->Port = 587;
            $phpmailer->SMTPAuth = true;
            $phpmailer->SMTPSecure = 'tls';
            $phpmailer->Username="apikey";
            $phpmailer->Password = SENDGRID_PASSWORD;   /* api key from sendgrid */
        }
        /* Add attachments to mail */
        foreach ($this->email_attachments as $attachment) {
            if (file_exists($attachment['path'])) {
                $phpmailer->AddEmbeddedImage($attachment['path'], $attachment['cid']);
            }
        }
    }
    public function set_content_type()
    {
        return "text/html";
    }
    
    public function set_wp_mail_from($email)
    {
        //Make sure the email is from the same domain
        //as your website to avoid being marked as spam.
        return strip_tags(get_option('admin_email'));
    }
    public function wp_mail_from_name($name)
    {
        return get_bloginfo('name');
    }
}

用法:

/* Set mail parameters */
$to = 'test@example.com';
$subject = 'Inline Img';
$body = '<h1>Image:</h1> <img src="cid:my_img_cid"/>';
$headers = "";
$my_attachments = [
    [
        "cid" => "my_img_cid", /* used in email body */
        "path" => plugin_dir_path(__FILE__) . '/my_img.png',
    ],
];
$custom_mailer = new Custom_Mailer();
$custom_mailer->send($to, $subject, $body, $headers, $my_attachments);

AddEmbeddedImage只接受两个参数,因此请确保不像示例中那样包含$name参数。

将以下代码添加到functions.php中,以将您的图像包含在所有电子邮件中(如签名徽标)。

function attachInlineLogo() {
    global $phpmailer;
    if (!( $phpmailer instanceof PHPMailer )) {
        require_once ABSPATH . WPINC . '/class-phpmailer.php';
        require_once ABSPATH . WPINC . '/class-smtp.php';
        $phpmailer = new PHPMailer(true);
    }
    $strFilePath = 'ABSOLUTE LOGO PATH';
    $strFileUID  = 'UNIQUE-UID'; //UID TO USE IN HTML TEMPLATE
    $strFileName = 'LOGO NAME';
    if (is_file($strFilePath)) {
        $phpmailer->SMTPKeepAlive = true;
        $phpmailer->AddEmbeddedImage($strFilePath, $strFileUID, $strFileName);
    }
}
add_action('phpmailer_init', 'attachInlineLogo');

在你的HTML代码

<img src='cid:UNIQUE-UID' />

相关内容

  • 没有找到相关文章

最新更新