-1

Duplicates:

How to embed images in email

How to embed images in html email

Embed images for use in email message using PHP?

I am sending HTML emails using php. I want to use embedded images in the HTML. Is it possible? I have tried lot of different methods, but none are working. Is anyone able to help me please?

Thanks

Community
  • 1
  • 1
Ahammed K M
  • 329
  • 1
  • 2
  • 7

4 Answers4

2

I find the best way to send images via email is to encode them with base64.

There are loads of links and tutorials on this but here is this code for Codeigniter should suffice:

/* Load email library and file helper */
$this->load->library('email');
$this->load->helper('file');

$this->email->from('whoever@example.com', 'Who Ever'); // Who the email is from
$this->email->to('youremailhere@example.com'); // Who the email is to

$image = "path/to/image"; // image path

$fileExt = get_mime_by_extension($image); // <- what the file helper is used for (to get the mime type)

$this->email->message('<img src="data:'.$fileExt.';base64,'.base64_encode(file_get_contents($image)).'" alt="Test Image" />'); // Get the content of the file, and base64 encode it

if( ! $this->email->send()) {
    // Error message here
} else {
    // Message sent
}
TheT4llOne
  • 43
  • 5
2

You need to provide the whole url where your image resides example:

<img src='http://www.mydomain.com/imagefolder/image.jpg' alt='my-image' width='' height=''>
iThink
  • 1,239
  • 3
  • 13
  • 34
2

This isn't really trivial, but with a couple of tries doable.

First of all, learn how to build a multipart email, that has the correct images attached to it. If you can't attach the images, they obviously won't be in the email. Make sure to set the type to multipart/related.

Secondly, find out how to set the cid references, in particular the Content-ID header of the attachment.

Third, glue it all together.

At each step, do the following:

  • look at the result
  • send the email to yourself, and compare it to what you received
  • compare it to a working example email
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
0

Here is a way to get a string variable without having to worry about the coding.

If you have Mozilla Thunderbird, you can use it to fetch the html image code for you.

I wrote a little tutorial here, complete with a screenshot (it's for powershell, but that doesn't matter for this):

powershell email with html picture showing red x

And again:

How to embed images in email

Community
  • 1
  • 1
bgmCoder
  • 6,205
  • 8
  • 58
  • 105