phpmailer自动包含本地图像
我目前正忙于phpmailer,想知道如何使用脚本在我的电子邮件中自动嵌入本地图像。我的想法是上传一个html文件和一个包含图像的文件夹,然后用脚本替换phpmailer自动包含本地图像,php,image,email,embed,phpmailer,Php,Image,Email,Embed,Phpmailer,我目前正忙于phpmailer,想知道如何使用脚本在我的电子邮件中自动嵌入本地图像。我的想法是上传一个html文件和一个包含图像的文件夹,然后用脚本替换 现在我得到的是: $mail = new PHPMailer(true); $mail->IsSendmail(); $mail->IsHTML(true); try { $mail->SetFrom($from); $mail->AddAddress($to); $mail->Subj
现在我得到的是:
$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);
}
函数嵌入图像(&$body,$mailer)
{
//获取所有img标签
preg_match_all(“/”,$body);
}
我不确定我应该在这里做什么,您是否检索src并用cid:$id替换它?
由于它们是本地图像,我没有web src链接之类的问题。为什么不将图像直接嵌入base64中的HTML中
您只需在base64中转换图像,然后将其包括在内,如下所示:
<img src="data:image/jpg;base64,---base64_data---" />
<img src="data:image/png;base64,---base64_data---" />
我不知道这是否与您的情况相关,但我希望它能有所帮助。您找到了正确的方法
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);
}
}
函数嵌入图像(&$body,$mailer){
//获取所有img标签
预匹配全部('/$img)
{
//制造cid
$id='img'.$index;
$src=$matches[1][$index];
//现在?????
$mailer->AddEmbeddedImage($src,$id);
//这种替换可能会得到改进
//因为它可能会取代你不想取代的东西
$body=str_replace($src,'cid:'.$id,$body);
}
}
函数嵌入图像(&$body,$mailer){
//获取所有img标签
预匹配全部('/$img){
$src=$matches[1][$index];
if(预匹配(“/\.jpg/”,$src)){
$dataType=“image/jpg”;
}elseif(预匹配(“/\.png/”,$src)){
$dataType=“image/jpg”;
}elseif(预匹配(“/\.gif/”,$src)){
$dataType=“image/gif”;
}否则{
//老一套
$id='img'.$index;
$mailer->AddEmbeddedImage($src,$id);
$body=str_replace($src,'cid:'。$id,$body);
}
如果($dataType){
$urlContent=file\u get\u contents($src);
$body=str_replace($src,'data:'.$dataType.';base64'.base64_encode($urlContent),$body);
}
}
}
我不知道phpmailer是否知道如何处理这些东西。当我只发送一封邮件时,邮件的正文中有一个mime类型,它不会显示为图片。。这就是为什么我选择phpmailer最新的Gmail不支持数据:像这样的图片。我个人喜欢它,但使用AddEmbeddedImage可能更好
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);
}
}
}