Php 在电子邮件正文中嵌入base64图像
我试图在电子邮件正文中嵌入带有png图标的base64图像,但我要么得到空图像,要么得到原始代码而不是html。我根据Stackoverflow中的其他帖子使用了不同的内容类型,我认为应该先使用多部分/可选,然后使用多部分/混合,但是我不确定应该将哪些部分放入电子邮件的标题中,哪些部分放入电子邮件的正文中。我找不到关于这方面的好教程。也许有人知道一些将常规html文档转换为嵌入图像的工具Php 在电子邮件正文中嵌入base64图像,php,html,email,mime-types,email-attachments,Php,Html,Email,Mime Types,Email Attachments,我试图在电子邮件正文中嵌入带有png图标的base64图像,但我要么得到空图像,要么得到原始代码而不是html。我根据Stackoverflow中的其他帖子使用了不同的内容类型,我认为应该先使用多部分/可选,然后使用多部分/混合,但是我不确定应该将哪些部分放入电子邮件的标题中,哪些部分放入电子邮件的正文中。我找不到关于这方面的好教程。也许有人知道一些将常规html文档转换为嵌入图像的工具 $headers = "MIME-Version: 1.0" . "\r\n"; $head
$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);
$headers=“MIME版本:1.0”。“\r\n”;
$headers.=“内容类型:多部分/可选;边界='boundary1'”。“\r\n”;
$headers.=“From:StudentOpen”。“\r\n”;
$headers.=“回复:web@mymail.com" . “\r\n”;
$subject=$GLOBALS['tpl']['win\u mail\u subject'];
$emailText='';
$emailText.=“内容类型:多部分/混合;边界='boundary1'”。“\r\n”;
$emailText.=“内容类型:多部分/可选;边界='boundary2'”。“\r\n”;
$emailText.='--boundry1'。“\r\n”;
$emailText.=“--boundry2”。“\r\n”;
$emailText.=“内容类型:文本/html;字符集=UTF-8”。“\r\n”;
$emailText.=“内容传输编码:7bit”。“\r\n”;
$emailText.=“”;
$emailText.=“”;
$emailText.='';
$emailText.=“”;
$emailText.=“”;
$emailText.=$GLOBALS['tpl']['howdy'].'.$tmp_成员_用户名',';
$emailText.='
';
$emailText.=$GLOBALS['tpl']['win\u mail\u description1'];
$emailText.='';
$emailText.='';
$emailText.='';
//facebook.png
$emailText.=“--boundry2”。“\r\n”;
$emailText.=“内容类型:图像/png;”。“\r\n”;
$emailText.=“name='facebook.png'”。“\r\n”;
$emailText.=“内容传输编码:base64”。“\r\n”;
$emailText.=“内容ID:”。“\r\n”;
$emailText.=“内容配置:内联;”。“\r\n”;
$emailText.=“filename='facebook.png'”。“\r\n”;
$emailText.=“ivborw0kggoaaa…AAElFTkSuQmCC”。“\r\n”;//base64字符串
$emailText.=“--boundry2”。“\r\n”;
$emailText.=“--boundry1”。“\r\n”;
$body=$emailText;
邮件($user\u email、$subject、$body、$headers);
我做错了什么?我收到的邮件是乱七八糟的。从--boundry1开始,然后我得到原始文本。您不应该自己尝试创建有效的MIME电子邮件消息。虽然这样做是可能的,但是使用一个专门的库更容易,它正确地实现了所有的边缘情况和缺陷,并且您所需要做的就是调用几个方法来发送电子邮件。如果有需要更改的内容,这也更容易维护 本例中提到的两个库是和。使用其中一种,代码如下: (改编自Swiftmailer示例代码)
require_once'lib/swift_required.php';
$message=Swift_message::newInstance();
//给信息一个主题
$message->setSubject($GLOBALS['tpl']['win\u mail\u subject']);
//使用关联数组设置发件人地址
$message->setFrom(数组('s)web@mymail.com“=>”学生开放“);
//使用关联数组将设置为地址
$message->setTo(数组($user_email));
$emailText='…此处省略。。。
嵌入(Swift_Image::fromPath('facebook.png'))。'“alt=”Facebook“/>
';
//给它一具尸体
$message->setBody($emailText,'text/html');
在那之后,您发送它。完成。您正在将标题放入正文中。请查看
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');