2014-01-25 61 views
2

我试图在电子邮件正文中嵌入带有png图标的base64图像,但是无论是获取空图像,还是获取原始代码而不是html。我基于Stackoverflow中的其他内容玩过不同的内容类型,我认为它应该是第一个多部分/替代,然后是多部分/混合,但是我不确定哪部分应该进入电子邮件的标题和什么去了电子邮件正文。对此,我无法获得任何好的教程。也许有人知道一些工具,将普通的HTML文档转换为嵌入式图像?在电子邮件正文中嵌入base64图像

$headers = "MIME-Version: 1.0" . "\r\n"; 
    $headers .= "Content-type: multipart/alternative; boundary='boundary1'" . "\r\n"; 
    $headers .= "From: StudentOpen <[email protected]>" . "\r\n"; 
    $headers .= "Reply-To: [email protected]" . "\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开始,然后我得到原始文本。

+1

您正在将标题放入正文中。看看https://stackoverflow.com/questions/16242489/send-a-base64-image-in-html-email –

回答

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('[email protected]' => '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'); 

之后,你把它。完成。

相关问题