简体   繁体   English

PHP将图像附加到电子邮件

[英]PHP Attaching an image to an email

Is there a way to attach an image to an html formatted email message created in PHP? 有没有办法将图像附加到用PHP创建的html格式的电子邮件消息?

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). 我们需要确保将公司徽标发送给发送给客户的电子邮件,这些客户在阅读电子邮件时可能无法访问互联网(他们显然会将其下载文件)。

Try the PEAR Mail_Mime package, which can embed images for you . 试试PEAR Mail_Mime包,它可以为您嵌入图像

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. 您需要使用addHTMLImage()方法并传递内容id(cid),这是一个唯一的文本字符串,您还将在img的src属性中将其用作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的Mail_Mime

PEAR's Mail_Mime package is what you're after here. PEAR的Mail_Mime包就是你在这里所追求的。

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. 这种方法可能会有不同的结果,具体取决于您的用户的邮件客户端,因此在发送之前,请尝试在虚拟gmail,yahoo和hotmail帐户上测试您的格式。

试试swiftmailer这里是一个很好的例子如何使用嵌入式图像http://swiftmailer.org/wikidocs/v3/embedding_images?s[]=embed

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. 我自己推荐PHP Mailer [0],还有PEAR :: Mail_Mime [1]等谷歌很乐意帮你找到。 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. 我一直在使用PHP Mailer发送带有嵌入式图像的消息[2]多年来没有任何障碍,但请记住,每个图像都会极大地增加电子邮件的带宽权重,因此通常它不应该用于批量处理。 And to echo Bill, do make use of the text-only alternative too. 并响应比尔, 不要使用纯文本替代的了。

[0] http://phpmailer.sourceforge.net/ [0] http://phpmailer.sourceforge.net/

[1] http://pear.php.net/manual/en/package.mail.mail-mime.php [1] http://pear.php.net/manual/en/package.mail.mail-mime.php

[2] http://phpmailer.sourceforge.net/docs/PHPMailer/PHPMailer.html#AddEmbeddedImage [2] http://phpmailer.sourceforge.net/docs/PHPMailer/PHPMailer.html#AddEmbeddedImage

taken from http://lists.evolt.org/archive/Week-of-Mon-20060612/183029.html 取自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. 具体来说 - 编写通过PHP发送的电子邮件充满了潜在的陷阱,只有在你真正了解可能出现的问题时才应该这样做。

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. 如果您打算相当密集地发送电子邮件,我强烈建议您通过专门的电子邮件营销客户端或实施其中一个将为您发送的电子邮件营销API之一。 ( mailchimp is apparently a decent one). mailchimp显然是一个体面的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM