如何在gmail中显示base64图像?

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

我正在将 Markdown 时事通讯中的

base64
编码图像从
rich text editor
发送到不同的电子邮件服务。除了
gmail
之外,每个服务都能正确渲染图像。相反,它显示
base64 string
:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAACLQAAAxOCAYAAAD0p7d8AA...

关于此问题的主要SO线程没有提供解决方案,正如在已接受答案的评论中可以看到的那样。

如何在 Gmail 中显示来自

data string
的图像?是否可以插入一个转换层以使其工作? (我不敢相信 6 年后 Gmail 还不支持这个)

image email base64 gmail markdown
2个回答
5
投票

Gmail 不支持嵌入的 Base 64 图像(请参阅我可以发送电子邮件吗)。我相信这是出于一般安全原因。您要么需要生成图像服务器端。或者将其作为附件发送(就像您提到的其他帖子中一样)。


0
投票

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

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

关键步骤:

**Identify Base64 Images:**
    Use a regular expression to find all base64-encoded images in the HTML content.

**Convert Base64 to Blob:**
    Decode the base64 image data into binary format (blob).
    Generate a unique identifier for each image.

**Embed Images:**
    Replace the base64 image tags in the HTML content with cid tags.
    Add the binary images to the email as inline attachments using addStringEmbeddedImage.

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

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