【发布时间】: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 开头,然后我得到原始文本。
【问题讨论】:
-
您将标题放在正文中。看看stackoverflow.com/questions/16242489/…
标签: php html email mime-types email-attachments