电子邮件中的 Base64 图像,在 google 邮件中不显示

问题描述 投票:0回答:3

我创建了一个 Base64 图像,将通过电子邮件发送给移动设备上的客户 它可以在电子邮件应用程序中使用,但 Gmail 应用程序显示丢失的图像

这只是谷歌邮件为了安全而屏蔽数据图像吗? 也没有显示图像的按钮

代码:

 $headers =“发件人:[电子邮件受保护]
”;
    $headers .=“回复:[电子邮件受保护]
”;
    $headers .=“MIME 版本:1.0
”;
    $headers .=“内容类型:text/html;字符集=ISO-8859-1
“;

$imagedata = file_get_contents("http://www.barcodesinc.com/generator/image.php? code=12345&style=68&type=C128B&width=180&height=70&xres=1&font=1"); $base64 = base64_encode($imagedata); $message ="<img src='data:image/png;base64,$base64'>"; mail($to,$subject,$message,$headers);</pre>
email gmail base64
3个回答
5
投票

这是谷歌屏蔽图片。谷歌剔除了大多数内联CSS和其他“可能”对最终用户系统造成损害的漏洞。不幸的是,情况确实如此。不过,我仍然认为您永远不应该将图像嵌入电子邮件中,因为您有很多使用数据计划的移动用户。好像有点不礼貌。



0
投票

在尝试发送带有嵌入图像的电子邮件时,我遇到了许多挑战,特别是当这些图像采用 Base64 格式时。许多电子邮件客户端(包括 Gmail)不直接支持 Base64 图像。经过大量研究和实验,我终于找到了一个非常有效的解决方案(将 base64 转换为 blob 并使用它):

https://www.labnol.org/gmail-base64-images-231026

这是一个通用的 PHP 解决方案,使用 PHPMailer 将 base64 图像转换为 blob 并将其嵌入到电子邮件中: 使用 PHPMailer 的通用 PHP 解决方案

php代码:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

// Your email setup
$mail = new PHPMailer(true);

try {
    // SMTP configuration
    $mail->isSMTP();
    $mail->Host = 'smtp.example.com';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'your-email-password';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port = 465;

    // Recipients
    $mail->setFrom('[email protected]', 'Your Name');
    $mail->addAddress('[email protected]');

    // Subject
    $mail->Subject = 'Your Subject';

    // HTML content with base64 image
    $htmlContent = '<html><body><p>Hello,</p><img src="data:image/png;base64,YOUR_BASE64_IMAGE_HERE" /></body></html>';

    // Convert base64 images to blobs and embed them as inline images
    $htmlBody = $htmlContent;
    $inlineImages = [];
    preg_match_all('/<img src="data:image\/(png|jpeg|gif);base64,([^"]+)"[^>]*>/i', $htmlBody, $matches);
    foreach ($matches[0] as $index => $base64ImageTag) {
        $format = $matches[1][$index];
        $base64Data = $matches[2][$index];
        $imageByte = base64_decode($base64Data);
        $imageName = uniqid();
        $imageBlob = new \stdClass();
        $imageBlob->data = $imageByte;
        $imageBlob->type = "image/$format";
        $newImageTag = str_replace("src=\"data:image/{$format};base64,{$base64Data}\"", "src=\"cid:$imageName\"", $base64ImageTag);
        $htmlBody = str_replace($base64ImageTag, $newImageTag, $htmlBody);
        $inlineImages[$imageName] = $imageBlob;
    }

    $mail->Body = $htmlBody;

    foreach ($inlineImages as $imageName => $imageBlob) {
        $mail->addStringEmbeddedImage($imageBlob->data, $imageName, $imageName, 'base64', $imageBlob->type);
    }

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

关键步骤:

识别 Base64 图像: 使用正则表达式查找 HTML 内容中的所有 Base64 编码图像。

将 Base64 转换为 Blob: 将 Base64 图像数据解码为二进制格式 (blob)。 为每个图像生成唯一标识符。

嵌入图像: 将 HTML 内容中的 base64 图像标签替换为 cid 标签。 使用 addStringEmbeddedImage 将二进制图像作为内联附件添加到电子邮件中。

适应: 该方法可以适用于其他编程语言和电子邮件库(如Java的JavaMail、Python的smtplib等),通过使用类似的步骤解码base64图像,将其转换为二进制格式,并将它们作为内联图像嵌入到电子邮件内容中。

© www.soinside.com 2019 - 2024. All rights reserved.