3

我正在尝试使用下面的代码使用 PHP 在正文上发送带有 base64 图像的电子邮件,但图像永远不会出现...如果我更改为 URL 它可以工作,但它不适用于 base64 .. .我只在新页面上测试了base64<img src=base64>并且也工作过......我错过了什么?

<?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' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

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

这是我的 base64 图像示例:http: //jsfiddle.net/28nP4/

4

5 回答 5

7

我尝试了不同的方法,我发现的唯一方法是上传图像并获取 URL,我从这个链接中得到了它:http: //j-query.blogspot.in/2011/02/save-base64-encoded-canvas-图像到png.html

这很简单:

<?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.';
?>

这会生成一个 URL,所以我不使用 ,而是<img src="'.$_POST['imageFromOtherPage'].'"/>使用生成的 URL。工作完美!

于 2013-09-29T16:34:37.733 回答
1

我有同样的问题,最后我解决了。它可能对您有帮助:您可以使用 SwiftMailer,但您必须添加新的 Image TextHeader 'Content-Location'。这是代码:

$message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($fromEmail, 'Name')->setTo($toEmail)->setBcc($bccEmails);
/*get uniqueID from cid:uniqueID */
$imageID = explode(':', $message->embed(\Swift_Image::fromPath('pathToTheImage')->setContentType('image/png')))[1];

/*Add Content-Location to image header*/
/** @var \Swift_Image $image */
$image = $message->getChildren()[0];
$image->getHeaders()->addTextHeader('Content-Location', $imageID);
$message->setBody('here you will have some html with <img src=$imageID alt="Embed Image">', 'text/html');

$mailer->send($message);
于 2014-12-31T00:42:32.510 回答
1

获取所有图片网址的功能:

function getImagesFromMsg($msg, $tmpFolderPath)
{
    $arrSrc = array();
    if (!empty($msg))
    {
        preg_match_all('/<img[^>]+>/i', stripcslashes($msg), $imgTags);

        //All img tags
        for ($i=0; $i < count($imgTags[0]); $i++)
        {
            preg_match('/src="([^"]+)/i', $imgTags[0][$i], $withSrc);
            //Remove src
            $withoutSrc = str_ireplace('src="', '', $withSrc[0]);

            //data:image/png;base64,
            if (strpos($withoutSrc, ";base64,"))
            {
                //data:image/png;base64,.....
                list($type, $data) = explode(";base64,", $withoutSrc);
                //data:image/png
                list($part, $ext) = explode("/", $type);
                //Paste in temp file
                $withoutSrc = $tmpFolderPath."/".uniqid("temp_").".".$ext;
                @file_put_contents($withoutSrc, base64_decode($data));
            }

            //Set to array
            $arrSrc[] = $withoutSrc;
        }
    }
    return $arrSrc;
}
于 2015-12-24T09:13:05.777 回答
0
<img src="'.$_POST['imageFromOtherPage'].'"/>

您的 POST 需要以data:image/png;base64, 形成 base64 编码的data:URI开头。

但是即使您解决了这个问题,您正在查看邮件的电子邮件客户端也很可能根本不支持该方法。data:URI 是一个相对较新的现象——至少在电子邮件客户端的年表中,其中大多数仍处于发现有一种称为 CSS 的技术的过程中。电子邮件中图像的久经考验的方法是将图像嵌入为内联附件。

以下是一些不依赖库的方法:让 PHP 发送带有内联图像附件的电子邮件

然而,使用像 Swiftmailer 这样的库可能会消除很多痛苦。这是相应的 Swiftmailer 文档。

于 2013-09-27T02:21:30.530 回答
0

尝试测试 $_POST['imageFromOtherPage'] 是否包含 base64 ,可能代码对于 Post 来说太长,您需要使用 php://input

于 2013-09-27T02:22:31.607 回答