Monday, August 20, 2007

Php Mail

Here I will show you the complete tutorial to send mails using php

To send email using PHP, you use the mail() function. This accepts 5 parameters as follows (the last 2 are optional)

mail(to,subject,message,headers,parameters)
Parameter Description
to Required. The recipient's email address.
subject Required. The email's subject line.
message Required. The actual email body.
headers Optional. Additional header fields such as "From", "Cc", "Bcc" etc.
parameters Optional. Any additional parameters.

Examples

Simple Mail

 "Thank you for registering!",
"Hello ayon, thank you for the tutorial!",
"From: viewer@somebloggersite.com");
?>

----Or--------

// Set up parameters
$to = "exampleayon@qmail.com";//or $_GET/$_POST['postorgetvalue']; To get value from forms.
$subject = "Thanks";
$message = "Hello ayon, thank you for the tutorial!";
$from = "viewer@somebloggersite.com";//or $_GET/$_POST['postorgetvalue']; To get value from forms.
$headers = "From: $from";

// Send email
mail($to,$subject,$message,$headers);

// Inform the user
echo "Thanks for registering! We have just sent you an email with your password.";
?>

Advance Html Mails
//add From: header
$headers = "From: webserver@localhost\r\n";

//specify MIME version 1.0
$headers .= "MIME-Version: 1.0\r\n";

//unique boundary
$boundary =
uniqid("HTMLDEMO");

//tell e-mail client this e-mail contains//alternate versions
$headers .= "Content-Type: multipart/alternative" .
"; boundary = $boundary\r\n\r\n";

//message to people with clients who don't
//understand MIME
$headers .= "This is a MIME encoded message.\r\n\r\n";

//plain text version of message
$headers .= "--$boundary\r\n" .
"Content-Type: text/plain; charset=ISO-8859-1\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .=
chunk_split(base64_encode("This is the plain text version!"));

//HTML
version of message
$headers .= "--$boundary\r\n" .
"Content-Type: text/html; charset=ISO-8859-1\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .=
chunk_split(base64_encode("This the HTML version!"));

//send
message
mail("root@localhost", "An HTML Message", "", $headers);
?>


OR----------------

You may download the greate php class http://phpmailer.sourceforge.net/ from which you can send mails with all the features like attachments, html, simple, multiple adresses more.
Copyright Ayon Baidya

No comments:

Post a Comment