在SO上有很多这样的问题,但问题是他们每个人都建议使用某种第三方库。这不是我的选择,因为我们使用内部排队系统,电子邮件将被发送到我们的数据库,直到它被发送。
如何在不使用第三方软件的情况下将图像嵌入电子邮件中?
答案 0 :(得分:0)
通常,如果您使用以下MIME标头将图像附加到电子邮件中,则text/html
可将其作为图像使用:
Content-Type: image/jpeg
Attachment-Disposition: inline; filename=MYIMAGE.JPG
然后在邮件正文中:
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
<html>
<!-- HTML tags -->
<img src="MYIMAGE.JPG" />
<!-- more HTML tags -->
</html>
答案 1 :(得分:0)
我使用vanilla PHP发送带有嵌入式嵌入图像的电子邮件。代码的核心如下所示:
$from = "$from_name <$from_email>";
$reply = "$replyto_name <$replyto_email>";
$to = "$to_name <$to_email>";
$main_boundary = substr( sha1(rand()), 0, 20 );
$part_boundary = substr( sha1(rand()), 0, 20 );
$headers = "From: $from\n";
$headers .= "Reply-To: $reply\n";
$headers .= "X-Mailer: PHP script mailer\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= 'Content-Type: multipart/alternative;'."\n".' boundary="'.$main_boundary.'"'."\n";
$message=
'This is a multi-part message in MIME format.
--'.$main_boundary.'
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
'.$msgTXT.'
--'.$main_boundary.'
Content-Type: multipart/related;
boundary="'.$part_boundary.'"
--'.$part_boundary.'
Content-Type: text/html; UTF-8
Content-Transfer-Encoding: 7bit
'.$msgHTML.'
--'.$part_boundary.'
Content-Type: image/png; name="logo.png"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="logo.png"
Content-ID: <part1.logo.111@example.com>
<base64 encoded image here>
--'.$part_boundary.'
Content-Type: image/gif; name="logo2.gif"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="logo2.gif"
Content-ID: <part2.logo.222@example.com>
<base64 encoded image here>
--'.$part_boundary.'--
--'.$main_boundary.'--
';
$mailsent = mail ($to, $subject, $message, $headers);
重要的是: