【问题标题】:embedding base64 images in email body在电子邮件正文中嵌入 base64 图像
【发布时间】:2014-02-16 12:47:50
【问题描述】:

我正在尝试在电子邮件正文中嵌入带有 png 图标的 base64 图像,但是我得到的是空图像,或者我得到的是原始代码而不是 html。我根据 Stackoverflow 中的其他帖子尝试了不同的内容类型,我认为它应该首先是 multipart/alternative,然后是 multipart/mixed,但是我不确定哪些部分应该进入电子邮件的标题以及哪些部分进入电子邮件正文。我找不到任何好的教程。也许有人知道一些将常规 html 文档转换为嵌入图像的工具?

    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: multipart/alternative; boundary='boundary1'" . "\r\n";
    $headers .= "From: StudentOpen <web@mymail.com>" . "\r\n";
    $headers .= "Reply-To: web@mymail.com" . "\r\n";

    $subject = $GLOBALS['tpl']['win_mail_subject'];

    $emailText = '';

    $emailText .= "Content-type:multipart/mixed; boundary='boundary1'" . "\r\n";
    $emailText .= "Content-type:multipart/alternative; boundary='boundary2'" . "\r\n";

    $emailText .= '--boundry1' . "\r\n";
    $emailText .= "--boundry2" . "\r\n";
    $emailText .= "Content-Type: text/html; charset=UTF-8" . "\r\n";
    $emailText .= "Content-Transfer-Encoding: 7bit" . "\r\n";

    $emailText .= "<html>";
    $emailText .= "<head>";
    $emailText .= '<meta http-equiv="content-type" content="text/html; charset=UTF-8">';
    $emailText .= "</head>";
    $emailText .= "<body>";

    $emailText .= $GLOBALS['tpl']['howdy'].' '.$tmp_member_username.',';
    $emailText .= '<br/>';
    $emailText .= $GLOBALS['tpl']['win_mail_description1'];        

    $emailText .= '<img src="cid:facebook.png" alt="Facebook"/>';

    $emailText .= '</body>';
    $emailText .= '</html>';

    // facebook.png

    $emailText .= "--boundry2" . "\r\n";
    $emailText .= "Content-Type: image/png;" . "\r\n";
    $emailText .= "name='facebook.png'" . "\r\n";
    $emailText .= "Content-Transfer-Encoding: base64" . "\r\n";
    $emailText .= "Content-ID: <facebook.png>" . "\r\n";
    $emailText .= "Content-Disposition: inline;" . "\r\n";
    $emailText .= "filename='facebook.png'" . "\r\n";

    $emailText .= "iVBORw0KGgoAAAA...AAElFTkSuQmCC" . "\r\n"; // base64 string

    $emailText .= "--boundry2" . "\r\n";
    $emailText .= "--boundry1" . "\r\n";

    $body = $emailText;

    mail($user_email, $subject, $body, $headers);

我做错了什么?我收到的电子邮件消息很混乱。以 --boundry1 开头,然后我得到原始文本。

【问题讨论】:

标签: php html email mime-types email-attachments


【解决方案1】:

您不应尝试自己创建有效的 MIME 电子邮件。虽然可以这样做,但使用专用库更容易,它正确实现了所有边缘情况和陷阱,您需要做的就是调用几个方法来发送电子邮件。如果有什么要改变的话,这也更容易维护。

本例中提到的两个库是PHPMailerSwiftmailer。使用其中任何一个,代码如下:

(改编自 Swiftmailer 示例代码)

require_once 'lib/swift_required.php';

$message = Swift_Message::newInstance();

// Give the message a subject
$message->setSubject($GLOBALS['tpl']['win_mail_subject']);

// Set the From address with an associative array
$message->setFrom(array('web@mymail.com' => 'StudentOpen'));

// Set the To addresses with an associative array
$message->setTo(array($user_email));

$emailText = '<html>...omitted here ... 
    <img src="' . $message->embed(Swift_Image::fromPath('facebook.png')) . '" alt="Facebook" />
</html>';

// Give it a body
$message->setBody($emailText, 'text/html');

然后,您发送它。完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-05
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 2013-09-26
    • 2019-12-12
    相关资源
    最近更新 更多