How to send message with images
Sending mail with attachment and sending HTML-mail with attachment are different procedures. Of course, both are in the body of the message, encoded in base64, but used headers are different. In that part we’ll talk about how to send HTML file with interstitial (unlike attached) images, using class Mime_mail.
<?php
include("Mail.php");
include("Mail/mime.php");
$text = "Text version of email";
$html = "<html><body>HTML version of email<img src="image.jpg"></body></html>";
$file = "/tmp/image.jpg";
$crlf = "\r\n";
$hdrs = array(
"From" => "you@yourdomain.com",
"Subject" => "Test mime message"
);
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->addHTMLImage ($file, "image/jpeg");
$mime->setHTMLBody($html);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory("mail");
$mail->send("postmaster@localhost", $hdrs, $body);
?>
The main difference is using addHTMLImage function. It has following parameters:
- string $data
- string $c_type
- string $name
- boolean $isfile
Full way to the attached image on the server or its content. Obligatory parameter.
content-type header value that will be sent. Non-obligatory parameter. Its default value is application/octet-stream
Name of the attached image. It will be used only in the case if first parameter ($data) is a file content.
Defines whether first parameter is a way to the image. Non-obligatory parameter. Its default value is true.
There are two ways of calling that method. As first parameter we can indicate the way to the image or binary data of the image. In the second case third and fourth parameters are obligatory.
The peculiarity of that method is that every image can be associated with the help of Content-ID: <;8820c4185>; header with the unique key. After that all links on attached image are replaced by the links on its key. As a result, in the received mail there will be line of <IMG SRC="cid: 8820c4185"> type, that will be analyzed by mail client, all content will be extracted from the corresponding section and there will appear image.



