Hello friends tired of sending normal text mail today i am going to show the script for sending HTML Mail from PHP. Sometimes we need to send some designer mails for this we will use some editor to create the mail and we will use simple mail() function for sending it. We need a good editor which is quite easy to use and integrate. Here we will use ckeditor which is very good editor to use and easy to integrate in your application and the best part is its under Open licence so we can use it free. Lets start.
Send HTML Email PHP Script
1. Create Page
First of all we will create a page on which we will put our form with our major values. Create an .php page so that will do our php working here only.
2. Create Form
After creating the page we will create a form in it and we will describe the action as $_SERVER[“PHP_SELF”]. Which means we will do our whole working in the same page and we will send our request to the same page.
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"><?form>
3. Create Table
After creating the form we will create the table in the form and include our email main parts as textfields.
- Receiver Email Id
- Subject
- Message
We will set the from email in the code but you can do it dynamic later at your part.
4. Create form with fields
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"> <table> <tr> <td>Receiver Mail Id</td><td>:</td><td><input type="text" id="mailid" name="mailid"></td> </tr> <tr> <td>Subject</td><td>:</td><td><input type="text" id="subject" name="subject"></td> </tr> <tr> <td>Message</td><td></td><td><textarea class="ckeditor" id="editor1" name="editor1"></textarea></td> </tr> <tr> <td colspan="3"></td> </tr> <tr> <td></td><td></td><td><input type="submit" id="sendmail" name="sendmail"></td> </tr> </table> </form>
5. CkEditor working
I think you have downloaded the ckeditor so we will put our editor in it and one more thing please download the ckfinder too. So that you will be able to get the images from your pc. Now add the script part to show the editor. Your text area will become editor.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script src="ckeditor/ckeditor.js" type="text/javascript"></script>
Here we put the two reference of javascript one is of jquey and another is of ckeditor js. Now your whole html code will look like this
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"> <table> <tr> <td>Receiver Mail Id</td><td>:</td><td><input type="text" id="mailid" name="mailid"></td> </tr> <tr> <td>Subject</td><td>:</td><td><input type="text" id="subject" name="subject"></td> </tr> <tr> <td>Message</td><td></td><td><textarea class="ckeditor" id="editor1" name="editor1"></textarea></td> </tr> <tr> <td colspan="3"></td> </tr> <tr> <td></td><td></td><td><input type="submit" id="sendmail" name="sendmail"></td> </tr> </table> </form> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script src="ckeditor/ckeditor.js" type="text/javascript"></script>
6. PHP Script for sending mail
After getting the from we have to send our mail and this main part will be done through the PHP. So first will get the values of every fields so that we will send our mail.
$getmailid=trim($_POST["mailid"]); $getSubject=trim($_POST["subject"]); $getmessage=stripcslashes($_POST["editor1"]); $to=$getmailid;
Now we will check if any of the above value is null than dont send else send.
if($getmailid==""||$getSubject==""||$getmessage=="") { $alert="Error please check again"; }
In else part we will include our mail code. First we will initialize our headers so that your mail provider treat it as HTML Format.
$headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .="From:$getmailid\r\n"; $headers .="Return-Path:$getmailid\r\n"; $headers .= "X-Mailer: Simply Mailer\n"; $headers .= "X-Priority: 1 (Highest)\n"; $headers .= "X-MSMail-Priority: High\n"; $headers .= "Importance: High\n";
In header part as you can see we are putting the text/html in content-type so that it will be treated as text and html otherwise it will be treated as text only. Default is text only. Let move to design our mailer we have to write only one line code nothing else.
$message=$getmessage;
At last our mail sending code.
mail($to, $getSubject, $getmessage,$headers);
After the mail function we will check – > mail is send or not if yes than it will show mail has been sent otherwise it will show error
if(mail) { echo "Mail has been sent to $to"; } else { echo 'Please try again'; }
You can manipulate your mail according to your need but always remember about the headers. Else you will get the text format. You can copy or paste the whole code
<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); if(isset ($_POST["sendmail"])) { $getmailid=trim($_POST["mailid"]); $getSubject=trim($_POST["subject"]); $getmessage=stripcslashes($_POST["editor1"]); $to=$getmailid; if($getmailid==""||$getSubject==""||$getmessage=="") { $alert="Error please check again"; } else { $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .="From:$getmailid\r\n"; $headers .="Return-Path:$getmailid\r\n"; $headers .= "X-Mailer: Simply Mailer\n"; $headers .= "X-Priority: 1 (Highest)\n"; $headers .= "X-MSMail-Priority: High\n"; $headers .= "Importance: High\n"; $message=" <html> <body> <span>If you cant see the image please allow from above</span><br><br> $getmessage </body> </html> "; } $to=$getmailid; mail($to, $getSubject, $getmessage,$headers); if(mail) { echo "Mail has been sent to $to<br>"; } else { echo 'Please try again'; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Send HTML Mail in PHP</title> <body> <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"> <table> <tr> <td>Receiver Mail Id</td><td>:</td><td><input type="text" id="mailid" name="mailid"></td> </tr> <tr> <td>Subject</td><td>:</td><td><input type="text" id="subject" name="subject"></td> </tr> <tr> <td>Message</td><td></td><td><textarea class="ckeditor" id="editor1" name="editor1"></textarea></td> </tr> <tr> <td colspan="3"></td> </tr> <tr> <td></td><td></td><td><input type="submit" id="sendmail" name="sendmail"></td> </tr> </table> </form> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script src="ckeditor/ckeditor.js" type="text/javascript"></script> </body> </html>
Download from the below link
Thank You for reading the Send HTML Email PHP Script tutorial hope it will help you in your applications. If you have any comment or queries than please ask and put your valuable comments and main part dont forget to share it.
~~~~ HAPPY CODING ~~~~
Thank you Thank You Thank You so much.
I had been looking for this from many days. Finally got it. It is very helpful for me.
Thanks Again
Thank you very much this is very helpful for me
it is working fine
but it is going to spam in gmail?
outing work … very helping