2014-01-25 97 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'); 

之後,你把它。完成。

相關問題