سؤال

I have a very weird problem when sending mail using Pear's Mail_Mime class. When mail is sent, empty lines are inserted between these these lines (between the boundary and the content-transfer-encoding and content-type):

--=_976a455a037a6f66f542025a19c22f9f

Content-Transfer-Encoding: quoted-printable

Content-Type: text/plain; charset=ISO-8859-1


Hello world

--=_976a455a037a6f66f542025a19c22f9f

Content-Transfer-Encoding: quoted-printable

Content-Type: text/html; charset=ISO-8859-1



<html>

  <head>

...

This causes the mail to be unreadable by most e-mail clients. For instance, in Thunderbird this mail remains completely empty:

Mail unreadable

This is my code:

require_once 'Mail.php';
require_once 'Mail/mime.php';

$to = 'my@example.com';

$headers = array(
  'From'    => 'noreply@example.com',
  'Subject' => 'Test mail',
);

$html = 
  '<html>
  <head>
  <style type="text/css">
    body{font-family: Arial, sans-serif;}
  </style>
  </head>
    <body>
      <h1>Test mail</h1>
      <p>Hello world</p>
    </body>
  </html>';

$text = "Hello world";

$mime = new Mail_mime();
$mime->setTXTBody($text);

$mime->setHTMLBody($html);
$body = $mime->get();
$hdrs = $mime->headers($headers);
$mail =& Mail::factory('mail');
$mail->send($to, $hdrs, $body);

I found out that if I manually remove the whitelines, that it works just fine. So I take out a few whitelines like this:

--=_976a455a037a6f66f542025a19c22f9f    
Content-Transfer-Encoding: quoted-printable    
Content-Type: text/plain; charset=ISO-8859-1


Hello world

--=_976a455a037a6f66f542025a19c22f9f    
Content-Transfer-Encoding: quoted-printable    
Content-Type: text/html; charset=ISO-8859-1



<html>

  <head>

...

And then the mail would open just fine:

enter image description here

I have upgraded Mail_Mime and Mail to the latest versions, and am at a loss what could cause this weird issue. Do note that whitelines are ALSO inserted throughout the message (so each HTML line will have an extra whiteline inserted). But this does not affect the display of the e-mail.

هل كانت مفيدة؟

المحلول

You have to initialize Mail_Mime correctly:

$mime = new \Mail_mime(
    array(
       'eol' => "\n",
       'head_charset' => 'utf-8',
       'text_charset' => 'utf-8',
       'html_charset' => 'utf-8',
    )
);

This is kinda explained in the manual:

Normally, it is not necessary to set parameters. But, if you want to send the generated MIME message using Mail then you have to set eol to "\n".

I don't know why this isn't default, but I think it will be backwards compatibility to some early version.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top