【问题标题】:PHP: mail() attaching base64 image comes out as textPHP:附加base64图像的mail()以文本形式出现
【发布时间】:2011-08-11 18:30:41
【问题描述】:

我一直在尝试将图像编码为 base64,然后使用 php mail() 将其作为电子邮件附件发送,但我得到的只是电子邮件中的 base64 文本。这是我正在使用的:

$boundary1 = 'bound1';
$boundary2 = 'bound2';
$to = 'test@me.com';  
$subject = 'Test Image attachment'; 
$headers = 'From: "Me" <me@myemail.com>'."\r\n"; 
//add boundary string and mime type specification 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary1\""; 
//define the body of the message. 
$message = 'Content-Type: image/png; name="'.$file_name.'"'."\n"; 
$message .= "Content-Transfer-Encoding: base64\n"; 
$message .= "Content-Disposition: inline; filename=\"$file_name\"\n\n";
$message .= base64_encode_image("signature_files/".$file_name, 'png')."\n";
$message .= "--" . $boundary2 . "\n";
//send the email 
mail( $to, $subject, $message, $headers);

// Function to encode an image
function base64_encode_image($filename=string,$filetype=string) {
    if ($filename) {
        $imgbinary = fread(fopen($filename, "r"), filesize($filename));
        return 'data:image/' . $filetype . ';base64,' . chunk_split(base64_encode($imgbinary), 64, "\n");
    }
}

有人发现这段代码有什么问题吗?真的不知道为什么我在收到电子邮件时只看到 RAW 文本。

【问题讨论】:

  • 不要建立自己的 mime 电子邮件。使用SwiftmailerPHPMailer 并为自己省去所有这些麻烦。他们都可以轻松地做附件和内嵌图片,
  • @Marc - 这是个好主意,但我没有这个项目的奢侈。

标签: png base64 php


【解决方案1】:

您在邮件标题和 MIME 附件部分标题中分别混合了 \r\n\n\n。尝试将它们全部更改为\r\n

另一种可能性 - 如果您尝试附加图像,而不是内联显示,请使用 Content-disposition: attachment;

$message .= "Content-Disposition: attachment; filename=\"$file_name\"\n\n";

【讨论】:

  • 修复了这些问题,但问题仍然存在。
  • @Nic 你确定这不是你的base64_encode_image() 函数吗?尝试使用普通的base64_encode() 来返回原始图像。
  • 我其实以为可能是这样,所以我按照你的建议做了,还是不行。
【解决方案2】:

这是发送带有内嵌 base64 图像的 HTML 电子邮件的正确方法:

<?php
$boundary = md5(uniqid(time()));

$header[] = "MIME-Version: 1.0";
$header[] = "Content-Type: Multipart/Mixed; Boundary=\"$boundary\"";
$header[] = "Content-Transfer-Encoding: 7bit";
$header[] = "From: John Doe <john.doe@pbx.com>";
$header[] = "Reply-To: John Doe <john.doe@pbx.com>";
$header[] = "X-Mailer: PHP/".phpversion();

$msg[] = "";
$msg[] = "--{$boundary}";
$msg[] = "Content-Type: text/html; charset=ISO-8859-1";
$msg[] = "";
$msg[] = "<p>YOUR HTML CONTENT</p> <img src=\"cid:logo\" alt=\"Logo\">";
$msg[] = "";
//HERE YOU MUST ATTACH THE IMAGE
//===============================================
$image = chunk_split(YOUR_IMAGE_ENCODED_IN_BASE64);
$msg[] = "--{$boundary}";
$msg[] = "Content-Type: image/png; name=\"logo.png\"";
$msg[] = "Content-ID: <logo>";
$msg[] = "Content-Description: Attachment";
$msg[] = "Content-Transfer-Encoding: base64";
$msg[] = "Content-Disposition: inline; filename=\"logo.png\"";
$msg[] = "";
$msg[] = $image;
$msg[] = "";

$msg[] = "--{$boundary}--";
mail($mailto, $subject, implode("\r\n", $msg), implode("\r\n", $header));

这里重要的部分是内容id引用,所以,首先,你必须定义你的图片id,在这种情况下,id是logo,所以,在附件中,你必须定义内容 ID 标头:

Content-ID: <logo>

然后,在 HTML 内容中,你可以通过内容 ID 调用或附加内联这张图片,如下所示:

<img src="cid:logo" alt="Logo">

【讨论】:

    猜你喜欢
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2016-02-24
    相关资源
    最近更新 更多