douai2499 2013-09-27 02:15 采纳率: 100%
浏览 440
已采纳

如何使用PHP在邮件正文上发送base64图像?

I'm trying to send an email with an image in base64 on the body with PHP using the code below, but the image never appears... If I change to an URL it works, but it doesn't with the base64... I tested the base64 on a new page only with <img src=base64> and worked too... What am I missing??

<?php
    // recipients
    $to  = $_POST['email'];

    // subject
    $subject = 'Test';

    // message
    $message = '
        <html>
        <head>
         <title>Test</title>
        </head>
        <body>

            <img src="'.$_POST['imageFromOtherPage'].'"/>

        </body>
        </html>
        ';

    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "
";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";

    // Mail it
    mail($to, $subject, $message, $headers);
 ?>

Here is my base64 image example: http://jsfiddle.net/28nP4/

  • 写回答

5条回答 默认 最新

  • dongluanjie8678 2013-09-29 16:34
    关注

    I tried different things and the only way I found was uploading the image and getting the URL, I got that from this link: http://j-query.blogspot.in/2011/02/save-base64-encoded-canvas-image-to-png.html

    It is very simple:

    <?php
        // requires php5
        define('UPLOAD_DIR', 'images/');
        $img = $_POST['img'];
        $img = str_replace('data:image/png;base64,', '', $img);
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);
        $file = UPLOAD_DIR . uniqid() . '.png';
        $success = file_put_contents($file, $data);
        print $success ? $file : 'Unable to save the file.';
    ?>
    

    And this generates an URL, so, instead of using <img src="'.$_POST['imageFromOtherPage'].'"/>, I use the generated URL. Worked perfectly!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大