Send HTML emails with images from PHP

Posted on 2009-02-18
Medium Priority
Last Modified: 2012-05-06
Hi there,

I am looking for a way to send HTML emails using PHP. i have managed to source info on the basics but wondered if anyone knew how i imbed images into my html?

i found what i want but i think i have to install something server side and im not sure if my hosting will do that or not. The site i found was

Does anyone know this class and do you know if somethig has to be installed? i think i do as i was reading this tutorial and it says to install something to the server.

Hope you can help

Question by:satmanuk

Accepted Solution

Mahdii7 earned 2000 total points
ID: 23677157
This is actually very simple. You need to have absolute paths in the email for images (ie. http://www.domain.com/images/image1.jpg instead of images/image1.jpg), and there are many good classes for sending HTML mail via PHP that require no server-side modifications.

I highly recommend PHPMailer, a class that I use frequently in my projects requiring MIME/HTML mail.


They provide native support for HTML mail.

Good luck!

Expert Comment

ID: 23682367
Either use PHPMailer as mentioned or you can set your headers to HTML if you want to use the mail() class in PHP.
I've added the code to send out a multi part e-mail, so it'll include HTML and plain text. Something that PHPMailer won't do.

$htmlVer is your HTML version of the e-mail, with full HTML code including full paths to images.
$textVer is your plain text version, nothing but text.
//Additional headers
$headers = 'From: you@yourdomain.com' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"==MB_$random_hash\"\r\n"; 
//E-mail code
 $strCode="This is a multi-part message in MIME format.\n\n";
    $strCode.="Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
    $strCode.="Content-Transfer-Encoding: 7bit\n";
    $strCode.="Content-Type: text/html; charset=\"iso-8859-1\" \n";
    $strCode.="Content-Transfer-Encoding: 7bit\n";
if(mail($to, $subject, $strCode, $headers))
{ echo("Done"); }
{ echo("Something went wrong"); }

Open in new window


Author Comment

ID: 23686310


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question