我正在通过本地 smtp 服务器通过 PHP 服务器发送电子邮件。此 HTML 电子邮件应包含应嵌入的图像。我已经设置了 Pear-Extensions Mail、Mail_Mime 和 Net_SMTP。所有电子邮件都可以正常发送,但没有附加图像。
我已经找到了这个解决方案:PHP Attaching an image to an email,尽管我一步一步地跟着它,图像仍然没有附加到电子邮件中。我已经尝试通过 附加图像$mime->addAttachment("/path/to/image/smile.gif", "image/gif");
,但我仍然没有收到任何附件。
$userGivenName = 'John';
$userSurname = 'Doe';
$crlf = "\r\n";
$mime = new Mail_mime($crlf);
$cid = "image001.png";
$mime->addHTMLImage("/filehost/img/emailLogo.png", "image/png", "", true, $cid);
$mailtext = '
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<meta name=Generator content="Microsoft Word 15 (filtered medium)">
</head>
<body lang=DE link="#0563C1" vlink="#954F72">
<div class=WordSection1>
<p class=MsoNormal>
<span style=\'mso-fareast-language:DE\'>
Hello '.$userGivenName.' '.$userSurname.',
<o:p>
</o:p>
</span>
</p>
<p class=MsoNormal>
<span style=\'mso-fareast-language:DE\'>
<o:p>
</o:p>
</span>
</p>
<p class=MsoNormal>
<span style=\'mso-fareast-language:DE\'>
some text
<o:p>
</o:p>
</span>
</p>
<p class=MsoNormal>
<span style=\'mso-fareast-language:DE\'>
<o:p>
</o:p>
</span>
</p>
<p class=MsoNormal>
<span style=\'mso-fareast-language:DE\'>
Regards
<o:p>
</o:p>
</span>
</p>
<p class=MsoNormal>
<span style=\'mso-fareast-language:DE\'>
<o:p>
</o:p>
</span>
</p>
<p class=MsoNormal>
<b>
<span style=\'font-size:10.0pt;font-family:"Arial",sans-serif;color:#E30613;mso-fareast-language:DE\'>
Max Mustermann
</span>
</b>
<span style=\'font-size:12.0pt;font-family:"Times New Roman",serif;color:#1F497D;mso-fareast-language:DE\'>
</span>
<span style=\'font-size:10.0pt;font-family:"Arial",sans-serif;color:#575756;mso-fareast-language:DE\'>
<br>
<b>
Department XY
</b>
<br>
................................................................
</span>
<span style=\'font-size:12.0pt;font-family:"Times New Roman",serif;color:#1F497D;mso-fareast-language:DE\'>
<o:p>
</o:p>
</span>
</p>
<p class=MsoNormal>
<span style=\'font-size:10.0pt;mso-fareast-language:DE\'>
<o:p>
</o:p>
</span>
</p>
<p class=MsoNormal>
<span style=\'mso-fareast-language:DE\'>
<img border=0 width=82 height=70 style=\'width:.8541in;height:.7291in\' id="Picture_x0020_1" src="cid:'.$cid.'" alt="Company Logo">
</span>
<span style=\'font-size:12.0pt;font-family:"Times New Roman",serif;color:#1F497D;mso-fareast-language:DE\'>
<o:p>
</o:p>
</span>
</p>
<p class=MsoNormal>
<span style=\'mso-fareast-language:DE\'>
<o:p>
</o:p>
</span>
</p>
<p class=MsoNormal>
<o:p>
</o:p>
</p>
</div>
</body>
</html>
';
$hdrs = array(
'From' => 'no-reply@mycompany.de',
'Subject' => 'mysubject'
);
$mime->addAttachment("/filehost/img/emailLogo.png", "image/png");
$msg_text = 'Plaintext';
$mime->setTXTBody($msg_text);
$mime->setHTMLBody($mailtext);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail = Mail::factory('smtp', array('host' => 'smtp.mycompany.local', 'port' => '25', 'auth' => false));
$mail->send($_POST['username'], $hdrs, $body);