Here is a example that is working! try it. I added some comments to the code!
if you have any question, don't hesitate to ask me.
Sergio Sanchez
function send_email($to, $from, $subject, $body) {
require_once "Mail.php";
require_once "Mail/mime.php";
//Definition of image added to html message
$image = "background.jpg";
$imageType = "image/jpg";
//SMTP server of the email
$host = "smtp.example.com";
// header of the message: from, to and subject
$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject
);
//Creating the object of the Mail_mime
$mime = new Mail_mime();
//setting the body as html message
$mime->setHTMLBody($body);
//Adding the html image to the message
$mime->addHTMLImage($image, $imageType, $image, true);
//Setting the encoding of the text of the message
$mimeparams = array ('head_encoding' => 'base64',
'text_encoding' => '8bit',
'head_charset' => 'UTF-8',
'text_charset' => 'UTF-8',
'html_charset' => 'UTF-8');
// Applying the params to the html text message
$body = $mime->get($mimeparams);
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', array ('host' => $host));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
return false;
} else {
echo("<p>Message successfully sent!</p>");
return true;
} // End if
} // End Function send_mail()
//Message in html
$html = "<span style='color:#0000CC'>this message is in blue color</span>
<br>
<img src='background.jpg' width='731' height='136' alt='Background image' longdesc='Background image' />";
//Using the function send_mail
send_email('John Doe <john@example.com>', 'My Website <no-reply@example.com>', 'Test HTML message', $html);