我正在用PHP编写电子邮件附件脚本。我已经成功完成了电子邮件的图像附件,但我遇到了html的问题。
电子邮件中的两件重要事项:
电子邮件内容,这是一些设计的HTML。
电子邮件附加图片。
如果我使用以下标题,我可以看到设计HTML的电子邮件。但附件不起作用。
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
如果我使用以下标题,我可以成功附加图像。但是html刚刚出现,因为它没有显示它是如何设计的......
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
此电子邮件也应在Outlook中完美显示:
有人可以帮助解决问题。
答案 0 :(得分:1)
更好的方式(更简单,也更好)将使用像PHPMailer这样的邮件程序...
但要回答你,你可以找到一些帮助here。
您可以找到另一个有帮助的示例here
<!-- language: lang-php -->
// Prepare by setting a timezone, mail() uses this.
date_default_timezone_set('America/New_York');
// Save some values to send an email, these might have come from any source:
$to = 'example@eliw.com';
$subject = 'A sample email - Dual Format';
// Create a boundary string. It needs to be unique (not in the text) so ...
// We are going to use the sha1 algorithm to generate a 40 character string:
$sep = sha1(date('r', time()));
// Define the headers we want passed. Note that they are separated by \r\n
$headers = "From: php@example.com\r\nX-Mailer: Custom PHP Script";
// Add in our content boundary, and mime type specification:
$headers .=
"\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-{$sep}\"";
// The body of the message. Use the separator with -- in front of it to
// mark the beginning of each section, and then provide the content type.
// A blank line beneath that will define the beginning of the content.
// At the end finish with the separator again, but this time with a --
// after it as well.
$body =<<<EOBODY
--PHP-alt-{$sep}
Content-Type: text/plain
This is our sample email message
Hello World!
That's it for now
--PHP-alt-{$sep}
Content-Type: text/html
<p>This is our sample email message</p>
<h2>Hello World!</h2>
<p>That's it for now.</p>
--PHP-alt-{$sep}--
EOBODY;
// Finally, send the email
mail($to, $subject, $body, $headers);