【发布时间】: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