我目前正忙于 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/