簡體   English   中英

電子郵件附件和HTML內容錯誤

[英]Email Attachment and HTML Content Error

我正在用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中完美顯示:

可以幫忙解決這個問題。

更好的方法(更簡便且可以正常工作)是使用像PHPMailer這樣的mailer類。

但是要回答您,您可以在這里找到一些幫助。

您可以在這里找到另一個有益的例子

<!-- 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM