image
  • base64
  • php
  • 2012-06-25 41 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实施中还存在很多问题)。

    相关问题