PHP:附加 base64 图像的 mail() 以文本形式显示
我一直在尝试将图像编码为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是发送带有内联 Base64 图像的 HTML 电子邮件的正确方法:
这里重要的部分是内容 id 引用,因此,首先,您必须定义图像 id,在本例中,id 是 logo ,所以,在附件中,你必须定义content ID header:
然后,在HTML内容中,你可以通过content ID调用或附加内联这张图片,如下所示:
This is the right way for sending HTML email with inline base64 image:
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:
Then, in the HTML content, you may call or attach inline this image by the content ID, as shown below:
您分别在邮件标头和 MIME 附件部分标头中混合
\r\n
和\n\n
。尝试将它们全部更改为\r\n
。另一种可能性 - 如果您尝试附加图像,而不是内联显示,请使用 Content-disposition:attachment;
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;