views:

2073

answers:

6

Is there a way to attach an image to an html formatted email message created in PHP?

We need to ensure that a corporate logo is on emails sent to clients who may not have access to the internet whilst reading their email (They will obviously have it to download the files).

+6  A: 

Try the PEAR Mail_Mime package, which can embed images for you.

When I answered this question, I posted a quick sample, but found that using the API as written produced a message with an image attachment, but wasn't rendered inline. A little nastiness is required to make it work with this class.

Specifically, you need the content-ID for the attached image, and Mail_mime won't give this to you directly. Right after calling addHTMLImage, you need to fish it out of Mail_mime's internal structures. Not great.

Here then is a butt-ugly way to do with Mail_mime...

include('Mail.php');
include "Mail/mime.php";


$crlf = "\r\n";
$hdrs = array( 
        'From' => '[email protected]', 
        'Subject' => 'Mail_mime test message' 
        ); 

$mime = new Mail_mime($crlf); 

$mime->addHTMLImage("/path/to/myimage.gif", "image/gif");

//here's the butt-ugly bit where we grab the content id
$cid=$mime->_html_images[count($mime->_html_images)-1]['cid'];

//now we can use the content id in our message
$html = '<html><body><img src="cid:'.$cid.'"></body></html>';
$text = 'Plain text version of email';

$mime->setTXTBody($text);
$mime->setHTMLBody($html); 

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send('[email protected]', $hdrs, $body);
Paul Dixon
Damn, you were 20 seconds faster ;-)
Joonas Pulakka
+1  A: 

It's probably easiest to use some library that can deal with email attachments. For example, PEAR's Mail_Mime.

Joonas Pulakka
A: 

Are you rolling your own, or using a prefab class? I recommend PHP Mailer[0] myself, and there's also PEAR::Mail_Mime[1] among others that Google would be happy to help you find. I've been using PHP Mailer to send messages with embedded images[2] for years without a hitch, though bear in mind that each image increases the email's bandwidth weight hugely, so generally it should not be used for anything in bulk. And to echo Bill, do make use of the text-only alternative too.

[0] http://phpmailer.sourceforge.net/

[1] http://pear.php.net/manual/en/package.mail.mail-mime.php

[2] http://phpmailer.sourceforge.net/docs/PHPMailer/PHPMailer.html#AddEmbeddedImage

taken from http://lists.evolt.org/archive/Week-of-Mon-20060612/183029.html

Natrium
A: 

PEAR's Mail_Mime package is what you're after here.

Once you've set your message up, adding an attachment is as simple as:

$mime = new Mail_mime("\n");

$mime->setTXTBody($msg_text);
$mime->setHTMLbody($msg_html);

// Add gif as attachment to mail
$mime->addAttachment("/path/to/image/smile.gif", "image/gif");

$body = $mime->get();
$headers = $mime->headers($headers);
$mail->send("[email protected]", $headers, $body);

If you're looking for your logo to display in a particular place in the email - rather than solely as an attachment - you can do the following:

// In your message html:
<img src='logo.gif' alt='Our logo' />

// PHP:
$mime->addHTMLImage('/path/to/image/logo.gif');

This approach can have mixed results depending on your user's mail client, so before sending it out try testing your format on dummy gmail, yahoo and hotmail accounts.

ConroyP
A: 

There are more than enough answers here that should help fix your specific problem, but I just thought it might be worth pointing out that you may well have a larger problem that you hadn't considered.

Specifically - writing emailers to be sent via PHP is filled with potential gotchas and should only be done if you have a really good idea of what can go wrong.

If you're planning on sending emails fairly intensively I would strongly suggest doing it through either a dedicated email marketing client or implementing one of the many email marketing API's out there that will send it for you. (mailchimp is apparently a decent one).

Steerpike
A: 

Try out swiftmailer here is a good example how to use embedded image http://swiftmailer.org/wikidocs/v3/embedding_images?s[]=embed

vaske