简体   繁体   English

使用PHP邮件将基本64位字符串作为图像嵌入到电子邮件中

[英]Embed base 64 string as image into email using php mail

How do I embed Base64 Image Data into email using php mail? 如何使用php邮件将Base64图像数据嵌入到电子邮件中?

<?php
$aImg = $_POST['aImage'];

$to = "abc@hotmail.com";
$subject = "Sending png image to email";

$message = "<html><head></head><body>";
$message .= '<img src="'.$aImg.'" alt="This is an Image" /></body></html>';

$headers = "Content-type: text/html";

if (mail($to, $subject, $message, $headers)) {
    echo("<p>Message successfully sent!</p>");
} else {
    echo("<p>Message delivery failed...</p>");
}
?>

After I run the codes, it shows "message successfully sent" but the image is not displayed out. 运行代码后,它显示“消息已成功发送”,但图像未显示出来。 It shows a small red cross image instead. 而是显示一个小的红十字图像。 I have been debugging for hours but still not able to get my image out. 我已经调试了几个小时,但仍然无法取出我的图像。 Currently, I'm sending the email to localhost for testing. 目前,我正在将电子邮件发送到localhost进行测试。

This is a little sloppy. 这有点草率。 I wrote it / mashed it together a long time ago from code I found online, and might have broken it sitting here pulling out private info without my glasses on. 我很早以前就从网上找到的代码编写/混搭了它,可能坐在这里弄坏了它,没戴眼镜就拿出了私人信息。 :-) Things I learned doing this way back then: chunk_split, concatenation (.), The use of a random separator. :-)那时我学会了这样做的方法:chunk_split,串联(。),使用随机分隔符。

<?
$to="x"; // For the file to be sent.
$from="xx"; // For the from line on the received email
$name="name.ext";
$type="application/x-gzip";
            $subject="subj"
            $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
         // open the file for a binary read
            $file = fopen(**xxxxxxxxxx filepath xxxxxxxxxxxx**,'rb');
         // read the file content into a variable
         //   $data = chunk_split(base64_encode(fread($file,filesize($file))));

         // now we encode it and split it into acceptable length lines
            //ALREADY DONE, MOVE UP A FEW LINES
         // message body
            $message = "Here's that File I promised you";
         // build headers
            $headers = "From: ".$from." \r\n" .
            "MIME-Version: 1.0\r\n" .
            "Content-Type: multipart/mixed;\r\n" .
            " boundary=\"{$mime_boundary}\"";
         // put message body in mime boundries
            $message = "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" .
            $message . "\n\n";
         // attachment with mime babble
             $message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
            //"Content-Disposition: attachment;\n" .
            //" filename=\"{$backfile}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
            chunk_split(base64_encode(fread($file,filesize(**xxxxxxxxxx filepath xxxxxxxxxxxx**)))) . "\n\n" .
            "--{$mime_boundary}--\n";
// close the file
fclose($file);
         // send mail
            mail($to, $subject, $message, $headers)
             ?>

If you had tried 'view source' it would have given you your first clue as to what happenned here. 如果您尝试过“查看源代码”,它将为您提供有关此处发生情况的第一条线索。

This is not trivial, and there's multiple ways to solve the problem. 这不是小事,有多种方法可以解决问题。 But you've got a long journey ahead of you. 但是,您还有很长的路要走。

Assuming you want to include the image directly in the email rather than as a HTTP URL or attachment, then you'll need to include it as data url - which will only work in very recent browsers / mail agents. 假设您希望将图像直接包含在电子邮件中,而不是作为HTTP URL或附件,那么您将需要将其作为数据URL包含-仅在最近的浏览器/邮件代理中有效。

Alternatively you can simply add it as an attachment to any email - but creating mime messages is not trivial - use one of the off-the-shelf packages such as swiftmailer or phpmailer. 或者,您可以简单地将其添加为任何电子邮件的附件-但创建mime消息并非易事-使用现成的软件包之一,例如swiftmailer或phpmailer。

A 3rd approach is to create an encapsulated MIME http web archive file , but I'm not aware of any off-the-shelf package fro creating such files in PHP. 第三种方法是创建一个封装的MIME http Web存档文件 ,但是我不知道在PHP中创建此类文件时是否有任何现成的软件包。 Also, I think these files are only supported by MSOutlook, MSIE and Opera (and even then there are lots of issues with the MS implementation). 此外,我认为这些文件仅受MSOutlook,MSIE和Opera支持(即使如此,MS实施也存在很多问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM