home

Php Send Email Tutorial


PHP uses a simple function mail to send emails. This php mail function requires mainly where to send email (to), the subject of email (subject), the message (message) and from address (from).

A simple Example of PHP Mail Function

<?php
//to send mail
mail($to, $subject, $message, $from);
echo "Thank you for sendin email";
?>

How to send HTML Emails using PHP?

To send HTML emails using mail php function we will need to add MIME type to our mail function. Our message header should contain MIME type.


 if(isset($_POST['submit']))
 {
  $UName = $_POST['UName'];
  $UEmail = $_POST['UEmail'];
  $UMessage = $_POST['UMessage'];
  $MDate = date('Y-m-d');
  $To = "me@hostbay.co.uk";
  
  if($UName == "" OR $UEmail == "" OR $UMessage == "")
  {
   $error = "Please fill all fields to send a message";
  }
  else
  {
   
   //send email to admin
   $subject = "Contact Us Message!";
   $headers = "From:" . $UEmail . " ";
   $headers .= "Reply-To:" . $UEmail . " ";
   $headers .= "Return-Path:" . $UEmail . " ";
   $headers .= "MIME-Version: 1.0 ";
   $headers .= "Content-Type: text/html; charset=ISO-8859-1 ";
   $headers .= "bcc:test@shahidweb.co.uk"; // BCCs to
   
     //$headers .= "CC: sombodyelse@noplace.com ";
     
   $message = "<html><body>";
   $message .= "<h1>Contact Us Message! </h1><br><B>Date: </b>" . $MDate . "<br><B>Name: </b>" . $UName . "<br><B>Email: </b>" . $UEmail . "<br><B>Message: </b>" . $UMessage . "." ;
   $message .= "</body></html>";
   
   if ( mail($To,$subject,$message,$headers) )
   {
      $confirmation = "done";
   }
   else
   {
      echo "Email has failed!";
   }

  }
  
 }

 

WHAT IS MIME

MIME stands for Multipurpose Internet Mail Extensions. It is a set of instructions that allows emails to be sent in more than just plain text. Content-Type sets what type of data is goign to be sent. The default value would be "text/plain". 

Charset is character set. Each language or set of characters has its own title or name. This value lets the recipient email know which one is used to display the body message.

 


Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

all rights reseverd by Cramerz.com©. web design london by dotPeak. hosting by HostBay
all contents on cramerz are the property of Cramerz.com