views:

8

answers:

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?