【问题标题】:Email Attachment and HTML Content Error电子邮件附件和 HTML 内容错误
【发布时间】:2013-11-03 12:14:13
【问题描述】:

我正在用 PHP 编写电子邮件附件脚本。我已成功完成电子邮件的图像附件,但我遇到了 html 问题。

电子邮件中的两个重要内容:

  1. 带有某些设计的 HTML 电子邮件内容。

  2. 电子邮件附件图片。

如果我使用以下标题,我可以看到带有 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 中完美显示:

谁能帮忙解决这个问题。

【问题讨论】:

  • <img src='http://link/to/my/image.jpg' />这样的图片使用绝对链接是不可想象的(只是一个快速的答案)
  • 不要建立自己的 mime 电子邮件。使用 PHPMailer 或 Swiftmailer。他们会更容易、更可靠地做到这一点。

标签: php html-email email-attachments email


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    相关资源
    最近更新 更多