PHP Attaching Image to Email

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

We need to make sure that the corporate logo is on emails sent to customers who may not have access to the Internet by reading their email (they will obviously have to upload files).

+6
php email attachment
source share
6 answers

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

You need to use the addHTMLImage () method and pass the content identifier (cid), which is a unique line of text, which you will also use in your img src attribute as the URL cid: . 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); 
+11
source share

Perhaps the easiest way is to use a library that can handle email attachments. For example, PEAR Mail_Mime .

+2
source share

The PEAR Mail_Mime package is what you do here.

Once you customize your message, adding an attachment will be 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 are looking for your logo to display in a specific place in the letter, and not just 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 may have mixed results depending on your user mail client, so before sending it, try checking your format on dummy gmail, yahoo and hotmail accounts.

+1
source share

Are you compiling on your own or using the prefab class? I recommend PHP Mailer [0] myself, and there is also PEAR :: Mail_Mime [1] among others that Google will be happy to help you find. I use PHP Mailer to send messages with embedded images [2] for many years without a hitch, although the bear in mind that each image increases the email bandwidth density is extremely, therefore, as a rule, it should not be used for anything in bulk. And to repeat Bill, also use a textual alternative.

[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

0
source share

It contains more than enough answers to help fix your specific problem, but I just thought it might be worth noting that you might have a big problem that you did not consider.

In particular, writing mail senders sent via PHP is filled with potential errors and should only be done if you have a really good idea about what could go wrong.

If you plan to send messages intensively enough, I would strongly suggest doing this through a dedicated email client or implementing one of the many email APIs that will send it to you. ( mailchimp , apparently worthy).

0
source share

Try swiftmailer here is a good example of how to use the inline image http://swiftmailer.org/wikidocs/v3/embedding_images?s[{=embed

0
source share

All Articles