Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Attaching an image to an email

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

like image 357
Omar Kooheji Avatar asked Feb 11 '09 13:02

Omar Kooheji


People also ask

How do I create a PHP form email with an attachment?

Check and validate the file extension to allow certain file formats (PDF, Image, and MS Word files). Upload file to the server using PHP move_uploaded_file() function. Add form data and uploaded file to the email content. Send an email to the specified recipient with an attachment using the PHP mail() function.


1 Answers

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);
like image 114
Paul Dixon Avatar answered Nov 01 '22 21:11

Paul Dixon