Mail_Mime::addHTMLImage()

Mail_Mime::addHTMLImage() – イメージをメッセージに追加

Synopsis

require_once 'Mail/mime.php';

boolean addHTMLImage ( string $file , string $c_type = 'application/octet-stream' , string $name = '' , boolean $isfile = = true )

Description

組み込みイメージを持ったHTMLメッセージを送る場合は、 イメージを追加するこの関数を使用してください。

Parameter

  • string $file - 画像ファイル名か、画像データ自身です。

  • string $c_type - イメージかファイルの内容の種類です。

  • string $name - 画像のファイル名で、 もし$file の内容が画像データの時のみ使用します。

  • boolean $isfile - $file がファイル名かどうかです。

Return value

boolean - Returns TRUE on success, PEAR_Error on failure.

Throws

Possible PEAR_Error values
エラーコード エラーメッセージ 原因 解決
NULL "ファイル file_name が読み込めない" ファイルが見つからないか、 スクリプトのファイルへのアクセス権限が足りません。 ファイル名とパスを確認します。ユーザーとファイルパーミッションを確認します。
NULL "file_name を開くことができない" The file is already opened and exclusivly locked by another application. In the most cases a programm opens the file for writing. addHTMLImage() does no file locking, so this problem is not caused by competitve callings of this function.

Note

This function can not be called statically.

添付を追加する (Previous) メッセージを構築する (Next)
Last updated: Wed, 30 Jul 2014 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

Note by: public@cloman.com
Most of you guys have this wrong. The reason your images will only display if you use the correct relative path is because it's actually loading the image file from your website.

The correct way to reference an inline image is to have the "src" attribute specify a "cid", like so:
<img src="cid:whatever">
where "whatever" is the Content-ID field for the attached image.

You can specify the Content-ID by using a fifth, undocumented parameter to AddHTMLImage(). See the source code for more information.
Note by: epommate@gmail.com
I use the code above with success :
$mimeMessage->addHTMLImage(file_get_contents($image),mime_content_type($image),basename($image),false);
Note by: paulh@attik.com
If you must use a file location that you can't (or would rather not) put into your html, just read it in and specify the file name.

$mime->addHTMLImage(file_get_contents('/root/server/path/img/image.jpg'),'image/jpeg','img/image.jpg',false);

Now my html template refers to the image as <img src="img/image.jpg"/> but I can read the image from wherever I need to store it.
Note by: ryan@ohioconnect.net
I recommend this function be renamed to addInlineAttachment() since it doesn't specifically apply to images. You can also use this to embed style sheets etc. So far i've only seen Thunderbird support embedded style sheets but i hope more mail clients start to.

I'd also like the ability to set the encoding type like you can with AddAttachment()
Note by: erwin@darwine.nl
I had troubles using INLINE IMAGES in HTML mail.
It seems a lot of people were confused (judging by the many posts in all kind of fora)
I couldn't use inline images untill I found out that the image must use the same path in the HTML as in the functioncall addhtmlimage().

So:
WRONG:
$mail->setHTMLBody('<html><body><img src="myImg.gif"> <p>You don't see this image.</p></body></html>');
$mail->addHTMLImage('./images/myImg.gif', 'image/gif');

RIGHT:
$mail->setHTMLBody('<html><body><img src="images/myImg.gif"> <p>You don't see this image.</p></body></html>');
$mail->addHTMLImage('images/myImg.gif', 'image/gif');

This was a little confusing for me since I expected the addHTMLImage() to simply fetch the image from source, but appearantly it is also using that same path in the actual source of the email.

Great package!
Regards,
Erwin
Note by: Lawrence
Re: Joseph's and jlang's notes about the filename -- I believe you just have to use the *same* full filename in both your HTML and in the call to addHTMLImage(). If you pass a relative path to addHTMLImage(), you need to use the relative path as the URL in the HTML. I.e.:

----
$mime->addHTMLImage("../../img_dir/butterfly.gif");
----
<img src="../../img_dir/butterfly.gif">
----

If you pass a full path to addHTMLImage(), you need to use the full path in your HTML:

----
$mime->addHTMLImage("/home/lawrence/img_dir/butterfly.gif");
----
<img src="/home/lawrence/img_dir/butterfly.gif">
----
Note by: lech.jonczyk@gmail.com
This is how it worked for me:

In the code:

$mime = new Mail_mime($crlf);
$mime->addHTMLimage('mail_templates/img/bg.png', 'image/png');

In the html part of mail body:

<img src="mail_templates/img/bg.png"/>

Cheers
Note by: nwalter@easymailing.eu
How can i get Mime_mail to replace my HTML-Image-Tags by CID strings?
Note by: Joseph@ppool.de
Post #1 is wrong for me it is exactly the opposit:
You must not use an absolute system path but must flatten the file name in the SRC="" tag to contain no folder names or slashes.
Note by: jlang
If you want to add inline images and reference them in your HTML you must use the fully qualified system path.

That means year image should look like this:
<img src="/home/public_html/image.gif">
not
<img src="image.gif">

Mail_Mime will replace your src with the appropriate CID value
Note by: info@roxlu.com
I found out that when you are adding html images inline you need to make sure what kind of transport you use. When using the default "mail" function my images weren't shown in some clients. When I switched to "smtp" everything worked perfectly