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

如何使用PHP在邮件正文上发送base64图像?,php,image,email,base64,Php,Image,Email,Base64,我试图用下面的代码发送一封邮件,邮件正文上有一个base64格式的图片,但是图片从来没有出现过。。。如果我更改为URL,它会工作,但它不会与base64。。。我在一个新页面上仅使用测试了base64,并且工作正常。。。我错过了什么 <?php // recipients $to = $_POST['email']; // subject $subject = 'Test'; // message $message = '

我试图用下面的代码发送一封邮件,邮件正文上有一个base64格式的图片,但是图片从来没有出现过。。。如果我更改为URL,它会工作,但它不会与base64。。。我在一个新页面上仅使用
测试了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图像示例:


你的帖子需要以
数据开始:image/png;base64,

但是,即使您解决了这个问题,您查看邮件的电子邮件客户端也很可能根本不支持该方法<代码>数据:URI是一种相对较新的现象——至少在电子邮件客户端的发展史上是如此,大多数客户端仍在发现一种称为CSS的技术。对于电子邮件中的图像,经过尝试和测试的方法是将图像作为内联附件嵌入

以下是一些不依赖于库的方法:


然而,使用像Swiftmailer这样的库可能会减轻很多痛苦

尝试测试$\u POST['imageFromOtherPage']是否包含base64,可能代码对于一篇文章来说太长了,您需要使用。

我尝试了不同的方法,找到的唯一方法是上传图像并获取URL,我从以下链接获得:

这很简单:

<?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,因此,我不使用
,而是使用生成的URL。工作得很好

我也有同样的问题,最后我解决了。这可能对您有所帮助: 您可以使用SwiftMailer,但必须添加新的图像文本标题“内容位置”。 代码如下:

$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);
$message=\Swift\u message::newInstance()->setSubject($subject)->setFrom($frommail,'Name')->setTo($toEmail)->setbc($bccEmails);
/*从cid获取uniqueID:uniqueID*/
$imageID=explode(“:”,$message->embed(\Swift\u Image::fromPath('paththeimage')->setContentType('Image/png'))[1];
/*将内容位置添加到图像标题*/
/**@var\Swift\u Image$Image*/
$image=$message->getChildren()[0];
$image->getHeaders()->addTextHeader('Content-Location',$imageID);
$message->setBody('在这里您将看到一些带有''text/html'的html);
$mailer->send($message);

获取所有图像url的函数:

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;
}
函数getImagesFromMsg($msg,$tmpFolderPath) { $arrSrc=array(); 如果(!empty($msg)) {
preg_match_all('/I测试时,我将内容写在

标签上,并显示正确的base64内容…它只是不“翻译”作为一张图片…你确定它显示了所有的数据吗?img的大小如何?如果它是1mb或更大,帖子可以剪切base64 Down的一部分。我使用的是一个修改过的版本,我检查了模板的主体,使用$message->embed from并替换src=“X”的内容使用此函数创建的文件的cid..非常有效!谢谢!

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;
}