2

I have a page which is sending an HTML email with the following PHP code:

$message = $body;//Create message body
$sender = $sender;//Create sender
$to = $customer_email;//Set who the email is being sent to
$subject = 'Quote - '.$_SESSION['quote_number'];//Subject for email
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";//Content-type header for mail() PHP built-in function
$headers .= 'To: '.$to . "\r\n";//To header for mail()
$headers .= 'From: '.$sender . "\r\n";//From header for mail()
$mail = mail($to, $subject, $message, $headers);//Send Email

Here is the part of the body with the image:

<img src="some_img.gif" alt="Title" width="400" height="55" align="left"/>

Right now when the email is sent, the image is not automatically downloaded, you have to right click it and download it. Is there a solution to automatically download this image within the mail() PHP function? If not, what is the easiest way I could do it?

I have done some research on this already, I have already looked at over 4 articles, here are a few that did not make sense to me, so I have not used the code in them. They are also pretty outdated.

How to embed images in email

Embed images for use in email message using PHP?

I am not interested in really complex code, for me I cannot stand classes and things like that where I just plain don't understand them.

Community
  • 1
  • 1
Tigerman55
  • 233
  • 10
  • 20

1 Answers1

4

Here is what you need:

<img src="data:image/gif;base64,<BASE64_ENCODED_IMG>" alt="Title" width="400" height="55" align="left"/>

<BASE64_ENCODED_IMG> should be the result of encoding the source binary of some_img.gif into Base64

You can do it on the fly with PHP: base64_encode(file_get_contents('/path/to/some_img.gif')); Or get it done by an online service such http://www.base64-image.de/ and copy&paste the results.

You can find further info regarding this in Embedding Base64 Images

Edit: This may be obvious but... it is not possible to force the mail client to display images. But some clients just avoid to download them, so in this case this is a valid workaround.

Community
  • 1
  • 1
Carlos
  • 4,949
  • 2
  • 20
  • 37
  • I apologize, but I am still a little confused. Do I just paste the second line of code you mentioned with the path to my image in the server, then I can just change the source of my image to the code you mentioned on the first line? I tried that, but it does not seem to be working. If you could expound a little, I would really appreciate it. – Tigerman55 Oct 09 '13 at 13:53
  • You need to get your image base64-encoded. You can do it with PHP or with an online service such http://www.base64-image.de/ – Carlos Oct 09 '13 at 14:00
  • If preventing the display and/or download of graphics in an email is a provider or client feature (such as Gmail or Outlook), then how will this force the display of the graphic? – Brian Warshaw Oct 09 '13 at 14:12
  • @BrianWarshaw You're right, you cannot force the client to 'display' images. But some clients avoid to 'download' images so this way is valid in that cases. – Carlos Oct 09 '13 at 14:17
  • So you're saying this would cause Outlook, for example, to download the image content with the message? I ask because I can't recall ever receiving mail from an external domain at work that didn't have images blocked initially. – Brian Warshaw Oct 09 '13 at 19:28
  • Yes, the image will be downloaded as part of the message. That doesn't mean Outlook is going to display it but beyond the download you can't do anything. – Carlos Oct 09 '13 at 21:09