0

I have some trouble with PEAR when I'm using the Mail_mime class to send out HTML/text mail with embedded images.

What I need script to do, is to provide an email with both a text and HTML version of the content. The content will be somewhat different.

  1. The text version will contain some text, and an image attachment.
  2. The HTML version will have a layout with some links and an embedded image. This image is the same as the attached image in the text version.

What I've got so far, is a script that send a plain text version and an HTML version. The text version is in fact not the text version which I'm telling it to send, but a stripped down version of the HTML email.

After some investigation, I found out that the plain text version actually gets sent in the email, but the email clients only show the stripped HTML version for some strange reason. It also seems like it's the addHTMLImage() method that breaks it. Without the embedded image the

What my code looks like, at the moment:

<?php

require 'Mail.php';
require 'Mail/mime.php';

$to = '[email protected]';
$additional_headers = array(
    'Subject' => 'Email subject',
    'From' => '[email protected]'
);

$text_body = <<<TEXT
This is the plain text version.
TEXT;

$html_body = <<<HTML
<p>This is the HTML version</p>
<p><img src="image.jpg" alt="" /></p>
HTML;

$mime = new Mail_mime();
$mime->setTxtBody($text_body);
$mime->setHTMLBody($html_body);
$mime->addHTMLImage(file_get_contents('default.jpg'), 'image/jpeg', 'image.jpg', FALSE);

$body = $mime->get();
$headers = $mime->headers($additional_headers);
$mailer =& Mail::factory('smtp', array(
    'host' => 'my.mailserver.net',
    'port' => 0,
    'auth' => TRUE,
    'username' => 'myusername',
    'password' => 'mypassword'
));
$res = $mailer->send($to, $headers, $body);

if (PEAR::isError($res)) {
    echo 'Couldn\'t send message: '.$res->getMessage();
}

?>

As far as I know, there doesn't seem to be anyone else with this problem. Is there something wrong with my code, or with my PEAR installation?

1
  • I'm having the same issue, though I'm not sure the title of your question is accurate to what you're asking. Anyways, I've opened up a bug for the Mime_mail module. Hopefully, something will come of it. pear.php.net/bugs/bug.php?id=18567 May 26, 2011 at 16:17

2 Answers 2

0

Pass in the end of line character to the constructor of the Mail_mime class.

Windows in "\n\r" - or the other way around, can't remember

0

We ran into the same issue today and have done extensive research in to it. What we've uncovered is that Mozilla Thunderbird chokes on ABNF strings that don't follow the Content-Type capitalization pattern. In all other email clients we've tested, the text alternative worked as expected.

So, you're problem is likely your client. Hope this helps point you in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.