Home Free Templets Works Mind Chiller Photo Gallery Family & Friends Links
 
 

 

Sending mails through PHP

You can send mails through PHP very easily. Only one fuction "mail()" does it all .The prototype of the mail function is

bool mail ( string to , string subject , string message [, string additional_headers [, string additional_parameters ]] )

Parameters

To : This is the mail addres of the receiver. This address must be of the form

user@example.com
user@example.com, anotheruser@example.com
User <user@example.com>
User <user@example.com>, Another User <anotheruser@example.com>


subject : This is the subject of the mail. It must not contain any '\n'(Newline character), if it does then the mail may not be sent properly.

message : Message to be send.

Optional parameters :

These are the parameters which you may ommit for general purpose mails. These are

additional_headers (optional) :

String to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).

additional_parameters (optional) : It is used to for security purpose of the Header option.

Return Values :

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

Example code :

<?php
$email_address = "johndoe@example.com"; //This is receivers mail address
$from = "From: rajib_kundu@indiatimes.com"; //This is senders mail address .
//You must write "From: " at the beginning of senders address
$subject = "This is the subject";
$body = "Hi, How are you john. I am fine . rajib"; // this is the message body
$mailsuccess = mail($email_address, $subject, $body , $from );
if ($mailsuccess ) //mail function returns true if it successful
echo "Your mail has been send successfully";
?>

You can also send mails in HTML Format, add Cc and Bcc options to your mail

<?php
// multiple recipients
$to   = 'johndoe@example.com' . ', ' ; // note the comma
$to .= 'webmaster@example.com' ;

// subject
$subject = 'Happy Anniversary' ;

// message
$message = '
<html>
<head>
  <title>Greetings for Anniversary</title>
</head>
<body>
  <p>Happy Anniversary. Where is the party! </p>
 
</body>
</html>
' ;

// To send HTML mail, the Content-type header must be set
$headers   = 'MIME-Version: 1.0' . "\r\n" ;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n" ;

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n" ;
$headers .= 'From: koderguru <webmaster@koderguru.com>' . "\r\n" ;
$headers .= 'Cc: anniversarygroups@example.com' . "\r\n" ;
$headers .= 'Bcc: friendsgroups@example.com' . "\r\n" ;

// Mail it
mail ( $to , $subject , $message , $headers );
?>

So, that's it , I hope tutorial help you to understand the basics of php mail function.