I'm trying to use the Mail_mime class in order to send an e-mail with an attached image. Everything works except for the image. The e-mail recipient doesn't get any attached file.
I started from this point to write my code:
pear.php.net/manual/en/package.mail.mail-mime.example.php
(I couldn't write the whole URL)
And this is what I wrote:
PHP Code:
<?php
// Use of PEAR libraries
// Store raw post data obtained from flash
if (isset($HTTP_RAW_POST_DATA)) {
$inputData = fopen('php://input','rb');
$image = stream_get_contents($inputData);
fclose($inputData);
}
require('Mail.php');
require('Mail/mime.php');
$text = 'Hello dude! Here\'s your image!';
$html = '<html><body>Hello dude! Here\'s your image!</body></html>';
$crlf = "\n";
$hdrs = array(
'From' => 'myself@hotmail.com',
'To' => 'myself@hotmail.com',
'Subject' => 'MIME message test with attached image'
);
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($image, 'application/octet-stream');
//do not ever try to call these lines in reverse order
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
$mail->send('info@receiver.com', $hdrs, $body);
?>
In comparison to the example in the PEAR web, the only difference is that I have to get raw post data sent from flash (the image bytes) and use them in the addAttachment() method instead of just a plain text.
The recipient e-mail client receives this e-mail, with the correct "From:" and "Reply-To" info, the correct subject and message body...., but there is no image.
What am I doing wrong?
P.S.: I also tried adding $HTTP_RAW_POST_DATA to the 1st parameter of the addAttachment() method, like this:
addAttachment($HTTP_RAW_POST_DATA, 'application/octet-stream');