簡體   English   中英

使用PHP郵件將基本64位字符串作為圖像嵌入到電子郵件中

[英]Embed base 64 string as image 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>");
}
?>

運行代碼后,它顯示“消息已成功發送”,但圖像未顯示出來。 而是顯示一個小的紅十字圖像。 我已經調試了幾個小時,但仍然無法取出我的圖像。 目前,我正在將電子郵件發送到localhost進行測試。

這有點草率。 我很早以前就從網上找到的代碼編寫/混搭了它,可能坐在這里弄壞了它,沒戴眼鏡就拿出了私人信息。 :-)那時我學會了這樣做的方法: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)
             ?>

如果您嘗試過“查看源代碼”,它將為您提供有關此處發生情況的第一條線索。

這不是小事,有多種方法可以解決問題。 但是,您還有很長的路要走。

假設您希望將圖像直接包含在電子郵件中,而不是作為HTTP URL或附件,那么您將需要將其作為數據URL包含-僅在最近的瀏覽器/郵件代理中有效。

或者,您可以簡單地將其添加為任何電子郵件的附件-但創建mime消息並非易事-使用現成的軟件包之一,例如swiftmailer或phpmailer。

第三種方法是創建一個封裝的MIME http Web存檔文件 ,但是我不知道在PHP中創建此類文件時是否有任何現成的軟件包。 此外,我認為這些文件僅受MSOutlook,MSIE和Opera支持(即使如此,MS實施也存在很多問題)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM