I'm trying to implement a code to send HTML email with embedded image.
I already tried for simple HTML email with image but this image is taken from server.
I'm trying to implement a code to send HTML email with embedded image.
I already tried for simple HTML email with image but this image is taken from server.
PHPMailer has the ability to automatically embed images from your HTML email. You have to give full path in the file system, when writing your HTML:
It will automaticaly convert to:
I would strongly recommend using a library like PHPMailer to send emails.
It's easier and handles most of the issues automatically for you.
Regarding displaying embedded (inline) images, here's what's on their documentation:
To give you a more complete example of how it would work:
Edit:
Regarding your comment, you asked how to send HTML email with embedded images, so I gave you an example of how to do that.
The library I told you about can send emails using a lot of methods other than SMTP.
Take a look at the PHPMailer Example page for other examples.
One way or the other, if you don't want to send the email in the ways supported by the library, you can (should) still use the library to build the message, then you send it the way you want.
For example:
You can replace the line that send the email:
With this:
Hope that helps. Let me know if you run into trouble using it.
Based on Arthur Halma's answer, I did the following that works correctly with Apple's, Android & iOS mail.
I'm using this function that find all images in my letter and attaches it to the message.
Parameters: Takes your HTML (which you want to send);
Return: The necessary HTML and headers, which you can use in
mail()
;Example usage:
You have to encode your email as multipart mime and then you can attach emails as attachments basically. You reference them by cid in the email.
Alternatively you could not attach them to the email and use URLs directly but most mail programs will block this as spammers use the trick to detect the liveness of email addresses.
You don't say what language but here is one example.
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