image
  • base64
  • php
  • 2012-06-25 40 views 2 likes 
    2

    如何使用PHP郵件將Base64圖像數據嵌入到電子郵件中?使用PHP郵件將基礎64字符串嵌入到電子郵件中

    <?php 
    $aImg = $_POST['aImage']; 
    
    $to = "[email protected]"; 
    $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>"); 
    } 
    ?> 
    

    我運行代碼後,顯示「消息發送成功」,但圖像沒有顯示出來。它會顯示一個小紅十字圖像。我一直在調試幾個小時,但仍然無法讓我的圖像出來。目前,我正在將電子郵件發送到本地主機進行測試。

    +0

    如何構建圖像數據?你看過你電子郵件的html src,它是否與http://en.wikipedia.org/wiki/Data_URI_scheme相匹配? – archil

    +0

    我通過將畫布轉換爲PNG圖像來構建。 var aImage = canvas [0] .toDataURL(「image/png」); – user1407415

    回答

    1

    這是一點馬虎。我很早以前就從我在網上找到的代碼寫了它/將它們混合在一起,並且可能已經打破了它坐在這裏拉出隱私信息而不戴我的眼鏡。 :-)我學到的事情是這樣做的:chunk_split,concatenation(。),使用隨機分隔符。

    <? 
    $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) 
          ?> 
    
    0

    如果您已經嘗試過「查看源代碼」,它會給出您關於此處發生的事情的第一條線索。

    這不是微不足道的,有多種方法可以解決問題。但是你有一段漫長的旅程。

    假設你想直接在電子郵件中,而不是作爲一個HTTP URL或附件的圖像,那麼你就需要將它作爲data url - 這隻會工作在非常近的瀏覽器/郵件代理。

    或者,您可以簡單地將它作爲附件添加到任何電子郵件 - 但創建MIME消息不是微不足道的 - 使用諸如swiftmailer或phpmailer之類的現成軟件包之一。

    第三種方法是創建封裝的MIME http web archive file,但我不知道任何現成的包在PHP中創建這樣的文件。另外,我認爲這些文件僅被MSOutlook,MSIE和Opera支持(甚至在MS實施中還存在很多問題)。

    相關問題