2013-10-24 4 views
0

Я пишу сценарий вложения электронной почты в PHP. Я успешно сделал прикрепление изображения к электронной почте, но я столкнулся с проблемами с html.Ошибка вложений электронной почты и содержимого HTML

Два Важно, что в Email:

  1. Email Content, который является HTML с некоторыми проектами.

  2. Email Прикрепить фотографии.

Если я использую следующий заголовок, я могу видеть электронную почту с разработанным 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

Это немыслимо, чтобы использовать абсолютные ссылки для изображений, как '' (только быстрый ответ) – JoDev

+1

Не создавайте свою собственную мимическую электронную почту. Используйте PHPMailer или Swiftmailer. Они сделают это намного проще и надежнее. –

ответ

1

Лучший способ (проще и работает отлично) будет использовать класс мэйлера как PHPMailer ...

Но чтобы ответить, вы можете найти некоторую помощь here.

Вы можете найти другой пример helful 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 = '[email protected]'; 
$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: [email protected]\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); 
Смежные вопросы