PHP Attaching an image to an email

蓝咒 提交于 2019-11-26 20:44:09

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

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

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.

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!