phpmailer 自动包含本 map 片

标签 php image email embed phpmailer

我目前正忙于 phpmailer,想知道如何使用脚本自动将本地镜像嵌入到我的电子邮件中。我的想法是上传一个 html 文件和一个包含图像的文件夹,然后让脚本替换 <img src 标签到 cid 标签。

现在我得到的是:

$mail = new PHPMailer(true);
$mail->IsSendmail();
$mail->IsHTML(true);

try 
{
    $mail->SetFrom($from);
    $mail->AddAddress($to);
    $mail->Subject = $subject;

    $mail->Body = embed_images($message, $mail);
    $mail->Send();
}

现在我得到了这个扫描 html 正文并替换 src 标签的不完整函数:

function embed_images(&$body, $mailer)
{
    // get all img tags
    preg_match_all('/<img.*?>/', $body, $matches);
    if (!isset($matches[0])) return;

    $i = 1;
    foreach ($matches[0] as $img)
    {
        // make cid
        $id = 'img'.($i++);
        // now ?????
    }
    $mailer->AddEmbeddedImage('i guess something with the src here');
    $body = str_replace($img, '<img alt="" src="cid:'.$id.'" style="border: none;" />', $body);
}

我不确定我应该在这里做什么,你检索 src 并将其替换为 cid:$id 吗? 因为它们是本地镜像,所以我没有 web src 链接或其他任何问题......

最佳答案

你的方法是对的

function embed_images(&$body,$mailer){
    // get all img tags
    preg_match_all('/<img[^>]*src="([^"]*)"/i', $body, $matches);
    if (!isset($matches[0])) return;

    foreach ($matches[0] as $index=>$img)
    {
        // make cid
        $id = 'img'.$index;
        $src = $matches[1][$index];
        // now ?????
        $mailer->AddEmbeddedImage($src,$id);
        //this replace might be improved 
        //as it could potentially replace stuff you dont want to replace
        $body = str_replace($src,'cid:'.$id, $body);
    }
}

关于phpmailer 自动包含本 map 片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20002039/

相关文章:

php - 在 php 中, "echo ` 命令` "做什么?(带单引号)

html - 在 DIV 中居中文本和图像的组合

java - 利用继承和多态来绘制图像?

java - 使用javax.mail发送大电子邮件,某些电子邮件发送失败

email - Win8/WinRT-如何在电子邮件正文中添加换行符

javascript - 不读取最后一个选项值

php - 如何在单击按钮时触发 PHP/jQuery

javascript - php post动态重定向并防止表单重新提交

html - CSS图片重叠定位

java - Java/Mysql服务器程序中邮箱账号密码存储