簡體   English   中英

在電子郵件正文中嵌入base64圖像

[英]embedding base64 images in email body

我正在嘗試在電子郵件正文中嵌入帶有png圖標的base64圖像,但是我得到的是空圖像,或者得到的是原始代碼而不是html。 我根據Stackoverflow中的其他帖子嘗試了不同的內容類型,我認為應該首先是多部分/替代,然后是多部分/混合,但是我不確定哪部分應該放入電子郵件的標題中,什么應該進入電子郵件正文。 我對此沒有任何好的指導。 也許有人知道某種可以將常規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開頭,然后得到原始文本。

您不應該自己嘗試創建有效的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');

之后,將其發送。 完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM