【问题标题】:phpmailer auto include local imagesphpmailer 自动包含本地图片
【发布时间】:2013-11-28 21:40:07
【问题描述】:

我目前正忙于使用 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 替换它吗? 因为它们是本地图像,所以我没有网络 src 链接或其他任何东西的麻烦......

【问题讨论】:

    标签: php image email embed phpmailer


    【解决方案1】:

    为什么不直接将图片嵌入到 base64 的 HTML 中?

    您只需将图像转换为 base64,然后像这样包含它们:

    <img src="data:image/jpg;base64,---base64_data---" />
    <img src="data:image/png;base64,---base64_data---" />
    

    我不知道这是否与您的情况相关,但我希望它有所帮助。

    【讨论】:

    • 我不知道 phpmailer 是否知道如何处理这些东西。当我只是发送带有正文的邮件(提供 mime 类型)时,它不会显示为图片。这就是我选择 phpmailer 的原因
    • 最新版 Gmail 不支持 data:images 这样的。我个人喜欢它,但使用 AddEmbeddedImage 可能会更好
    【解决方案2】:

    你的方法是对的

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

    【讨论】:

      【解决方案3】:
      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) {
              $src = $matches[1][$index];
      
              if (preg_match("/\.jpg/", $src)) {
                  $dataType = "image/jpg";
              } elseif (preg_match("/\.png/", $src)) {
                  $dataType = "image/jpg";
              } elseif (preg_match("/\.gif/", $src)) {
                  $dataType = "image/gif";
              } else {
                  // use the oldfashion way
                  $id = 'img' . $index;            
                  $mailer->AddEmbeddedImage($src, $id);
                  $body = str_replace($src, 'cid:' . $id, $body);
              }
      
              if($dataType) { 
                  $urlContent = file_get_contents($src);            
                  $body = str_replace($src, 'data:'. $dataType .';base64,' . base64_encode($urlContent), $body);
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-15
        • 2017-02-28
        • 2018-09-06
        • 2013-01-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多