PHP Attaching an image to an email

20,354

Solution 1

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

You need to use the addHTMLImage() method and pass a content id (cid), which is a unique string of text you will also use in your img's src attribute as a cid: URL. For example:

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); 

//attach our image with a unique content id
$cid="mycidstring";
$mime->addHTMLImage("/path/to/myimage.gif", "image/gif", "", true, $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);

Solution 2

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

Solution 3

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.

Share:
20,354
Omar Kooheji
Author by

Omar Kooheji

I've given up a life of code and turned to the dark side. I now lead a team of talented software engineers to deliver quality systems.

Updated on July 09, 2022

Comments

  • Omar Kooheji
    Omar Kooheji almost 2 years

    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).

  • Joonas Pulakka
    Joonas Pulakka about 15 years
    Damn, you were 20 seconds faster ;-)
  • Logan Wayne
    Logan Wayne about 8 years
    I'm getting twice the error of Notice: Undefined property: Mail_mime::$_html_images in .... It's the line where you declare the $cid variable. It sends the email but it does not display the image. Will you be able to update your answer?
  • kliron
    kliron about 8 years
    This answer is 7 years old! If you look at the source for Mail_Mime, you'll see that the $_html_images member I access is renamed $html_images and protected github.com/pear/Mail_Mime/blob/1.10.0/Mail/mime.php#L111 Wouldn't be hard to derived a new class from Mail_mime and add a method to give you that cid value though.
  • Matt
    Matt over 7 years
    Tried to edit this answer, but it was rejected... The Mail_Mime module allows you to specify the content-id with addHTMLImage()[1]. So instead of needing to fish it out, simply do: $mime->addHTMLImage("/path/to/myimage.gif", "image/gif", "", true, "mycidstring"); $html = '<html><body><img src="cid:mycidstring"></body></html>'; [1]pear.php.net/manual/en/package.mail.mail-mime.addhtmlimag‌​e.php
  • kliron
    kliron over 7 years
    I didn't reject it :( Edit looked fine to me so I've changed the answer. Many thanks.