PHP:附加 base64 图像的 mail() 以文本形式显示

发布于 2024-11-29 04:01:15 字数 1422 浏览 1 评论 0原文

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

$boundary1 = 'bound1';
$boundary2 = 'bound2';
$to = '[email protected]';  
$subject = 'Test Image attachment'; 
$headers = 'From: "Me" <[email protected]>'."\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");
    }
}

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

I have been trying to encode an image as base64 then sent it as an email attachment using php mail(), but all I am getting is the base64 text in my email. Here is what I am using:

$boundary1 = 'bound1';
$boundary2 = 'bound2';
$to = '[email protected]';  
$subject = 'Test Image attachment'; 
$headers = 'From: "Me" <[email protected]>'."\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");
    }
}

Does anyone see anything wrong with this code? Really not sure why I am just see RAW text when I recieve the email.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰ツ都不明白 2024-12-06 04:01:15

这是发送带有内联 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 <[email protected]>";
$header[] = "Reply-To: John Doe <[email protected]>";
$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 ,所以,在附件中,你必须定义content ID header:

Content-ID: <logo>

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

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

This is the right way for sending HTML email with inline base64 image:

<?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 <[email protected]>";
$header[] = "Reply-To: John Doe <[email protected]>";
$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));

The important part here is the content id reference, so, first, you must define your image id, in this case, the id is logo, so, in the attachment, you must define the content ID header:

Content-ID: <logo>

Then, in the HTML content, you may call or attach inline this image by the content ID, as shown below:

<img src="cid:logo" alt="Logo">
别理我 2024-12-06 04:01:15

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

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

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

You are mixing \r\n and \n\n in your message headers and MIME attachment part headers respectively. Try changing them all to \r\n.

Another possibility - if you are trying to attach the image, rather than display inline, use Content-disposition: attachment;

$message .= "Content-Disposition: attachment; filename=\"$file_name\"\n\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文