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

Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238

6 Answers6

11

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' => 'foo@bar.org', 
        '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('person@somewhere.org', $hdrs, $body);
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • 1
    Damn, you were 20 seconds faster ;-) – Joonas Pulakka Feb 11 '09 at 13:54
  • 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? – Logan Wayne Mar 30 '16 at 02:49
  • 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 https://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. – Paul Dixon Mar 30 '16 at 18:58
  • 1
    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 = ''; [1]https://pear.php.net/manual/en/package.mail.mail-mime.addhtmlimage.php – Matt Jan 17 '17 at 01:04
  • I didn't reject it :( Edit looked fine to me so I've changed the answer. Many thanks. – Paul Dixon Jan 17 '17 at 09:07
2

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

Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
1

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("joe@bloggs.com", $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
  • 40,958
  • 16
  • 80
  • 86
0

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

vaske
  • 9,332
  • 11
  • 50
  • 69
0

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
  • 30,772
  • 17
  • 59
  • 73
0

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
  • 17,163
  • 8
  • 39
  • 53