Free and Opensource software review, Operating system review, Tips and Tricks, System tweaks, troubleshooting, Tools and Techniques, Hacks

How to send html mail using php?

Send html mail using phpI have already written an article explaining how to send a mail using php. That contained  only information about sending an ordinary text mail. This technique is mostly used to send newsletters and other marketing stuff into your mail. You can send your own custom html mail using php by providing a simple addition to the headers of the mail send in my previous post.

The example used in the previous post was

<?php
$email = $_REQUEST[‘email’] ;
$subject = $_REQUEST[‘subject’] ;
$message = $_REQUEST[‘message’] ;
if ( mail(“someone@example.com”, $subject,$message, “From:” . $email) )  /* mail function returns true if the server have accepted the mail for deliver ( not necessary to send) */
echo “your mail have been successfully send”;
else
echo “Failed to send message”;
?>

The inputs to this php file came from an html file and now lets see the changes required.

The only thing you have to do is to specify the MIME-Version and set the content type in the headers of the mail sent. The changed code will look like

<?php
$email = $_REQUEST[‘email’] ;
$subject = $_REQUEST[‘subject’] ;
$message = $_REQUEST[‘message’] ;

//here we specify the MIME-Version and the content type in a variable mailinfo.
$mailinfo = ‘MIME-Version: 1.0′ . ‘rn';
$mailinfo .= ‘Content-type:text/html;charset=iso-8859-1′ . ‘rn';
$mailinfo .= ‘From: ‘.$email.’rn';

//Specifying the html content to be sent
$content='<html><body>
<h1>’.$subject.'</h1>
<p>’.$message.'</p>
</body></html>’

if ( mail(“someone@example.com”, $subject,$content, $mailinfo) )
echo “your mail have been successfully send”;
else
echo “Failed to send message”;
?>

The above code will send html mail to someone@example.com but it is not at all guaranteed that it will reach the inbox in most mail servers. These mails usually land in the spam folder.

If you want to make additional customization to the message (html message) you send, you can do it by using advanced text editor for web like the ckeditor. Ckeditor will generate an html output for the rich text you enter in the editor thus it can be used to improve the quality of the mail you send.

You will find this very useful when you want to send an html formatted newsletter to your subscribers or customers.

, , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by sweet Captcha