在 HTML 电子邮件中嵌入图像

Embedding images in HTML email

所以我已经为此苦苦挣扎了很长一段时间,并最终得到了某种程度的工作。在某些电子邮件客户端上,这段代码显示正确嵌入的三个图像,一些将它们作为附件附加,而另一些则不显示它们。我做错了什么,我该如何解决。

电子邮件使用 php 的 mail() 功能发送,如下所示:mail($_POST['email'],'Signature',$body,$header);

此外,在有人向我开枪之前,我知道这不是正确的编程习惯,但这个脚本是一个本地脚本,只有我们可以访问它来简单地生成电子邮件签名。这就是为什么它根本不进行任何输入验证或清理的原因。

$header = 'From:webmaster@nobelis.co.za' . "\r\n";
            $header .= 'MIME-Version: 1.0' . "\r\n";
            $header .= 'Content-type:multipart/mixed; boundary="B_3504178637_3566343"' . "\r\n";
            $body = '> This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--B_3504178637_3566343
Content-type: multipart/alternative;
    boundary="B_3504178637_3589184"


--B_3504178637_3589184
Content-type: text/plain;
    charset="US-ASCII"
Content-transfer-encoding: 7bit


Regards,

details





--B_3504178637_3589184
Content-type: text/html;
    charset="US-ASCII"
Content-transfer-encoding: quoted-printable

<table cellpadding="0" cellspacing="0" width="300">
<tr>
<td style="padding-right: 10px; border-bottom: 1px solid rgb(255, 255, 255);">
<img src="cid:E3FFCD1F-2201-4790-81B5-5F4F9B82A413" type="image/png" width="51" style="width:51px">
</td>
<td style="text-align: center; border-bottom: 1px solid rgb(221, 221, 221);">
<span style="color: rgb(31, 130, 197); font-family: Helvetica; font-size: 13px; line-height: 20px;">' . strtoupper($_POST['name']) . '</span><br>
<span style="color: rgb(102, 102, 102); font-family: Helvetica; font-size: 13px;">' . strtoupper($_POST['designation']) . '</span>
</td>
</tr>
<tr>
<td style="padding-right: 10px; border-bottom: 1px solid rgb(255, 255, 255); border-top: 1px solid rgb(255, 255, 255);">
<img src="cid:060EED13-21A7-4FF2-BDCE-D5A87BD2165B" type="image/png" width="51" style="width:51px">
</td>
<td style="text-align: center; border-bottom: 1px solid rgb(221, 221, 221);">
<span style="color: rgb(31, 130, 197); font-family: Helvetica; font-size: 13px; line-height: 20px;">CONTACT ME</span><br>
<span style="color: rgb(102, 102, 102); font-family: Helvetica; font-size: 10px;">T: ' . $_POST['telephone'] . ' | ' . ($_POST['fax'] != "" ? "F: " . $_POST['fax'] : ($_POST['cellphone'] != "" ? "C: " . $_POST['cellphone'] : "F:  086 694 0819")) . '</span>
</td>
</tr>
<tr>
<td style="padding-right: 10px; border-top: 1px solid rgb(255, 255, 255);">
<img src="cid:CA0B3207-4728-46DE-8BC7-72723443E97A" type="image/png" width="51" style="width:51px"></td>
<td style="text-align: center;">
<span style="color: rgb(31, 130, 197); font-family: Helvetica; font-size: 13px; line-height: 20px;">company name</span><br>
<span style="color: rgb(102, 102, 102); font-family: Helvetica; font-size: 10px;"><i>address</i></span>
</td>
</tr>
</table>

--B_3504178637_3589184--


--B_3504178637_3566343
Content-type: image/png; name="E3FFCD1F-2201-4790-81B5-5F4F9B82A413.png"
Content-ID: <E3FFCD1F-2201-4790-81B5-5F4F9B82A413>
Content-disposition: inline;
    filename="E3FFCD1F-2201-4790-81B5-5F4F9B82A413.png"
Content-transfer-encoding: base64

//base64 encoded image
--B_3504178637_3566343
Content-type: image/png; name="060EED13-21A7-4FF2-BDCE-D5A87BD2165B.png"
Content-ID: <060EED13-21A7-4FF2-BDCE-D5A87BD2165B>
Content-disposition: inline;
    filename="060EED13-21A7-4FF2-BDCE-D5A87BD2165B.png"
Content-transfer-encoding: base64


//base64 encoded image
--B_3504178637_3566343
Content-type: image/png; name="CA0B3207-4728-46DE-8BC7-72723443E97A.png"
Content-ID: <CA0B3207-4728-46DE-8BC7-72723443E97A>
Content-disposition: inline;
    filename="CA0B3207-4728-46DE-8BC7-72723443E97A.png"
Content-transfer-encoding: base64


//base64 encoded image
--B_3504178637_3566343--' . "\r\n";

对于主要消息的内容类型,您应该使用 multipart/related 而不是 multipart/mixed。

使用 PHPMailer class 而不是 mail() 函数。它既简单又强大