Home » Tutorials » Web Development » PHP » PHP Basics » Using the mail function to send emails..

Using the mail function to send emails

Added July 14, 2008, read 72 times

Using the mail() function in PHP you can send emails from your website. This simple tutorial shows you how use the mail() function to send html emails thorugh your server's smtp protocol.

The PHP function syntax

PHP Code
mail(mailto, subject, message, additional_header);
Select all

mailto - is the email address to which the email will be sent. You can add multiple recipients by separating the addresses with commas.

subject -  is the subject of the message and will be displayed in the subject field.

message -  is the body of the email. Each line will be separated by LF (\ n) and will have more than 70 characters

additional_header - additional information, for example: From, Cc, Bcc being separated by CRLF (\ r \ n)

All emails must have the From: field completed. This can be done by adding it in the additional header information or by setting the proper address in the file php.ini.

The mail() function returns TRUE if mail was accepted for delivery, or FALSE otherwise. Accepting mail for delivery does not mean that it was sent.

Example: Sending an email

PHP Code
$to="info@tutorialhelpdesk.com"; // recepient
$subject="testing the mail() function in PHP"; // subject
$message="Message send using the mail() function"; // body
mail($to, $subject, $message); // sending the email
Select all

Example: Sending an email with additional header information

We have used Content-type: text/html to send an html message and \r\n to display each header information per line.

PHP Code
$to="tudor@tutorialhelpdesk.com";
$subject="Using the email() function in PHP";
$message="Mail sent using the mail() function in PHP";
$headers  = 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$headers .= 'From: info@tutorialhelpdesk.com'."\r\n";
$headers .= 'Cc: cc@tutorialhelpdesk.com'."\r\n";
$headers .= 'Bcc: bcc@tutorialhelpdesk.com'."\r\n";
mail($to, $subject, $message, $headers);
Select all

 

Discuss







Nobody posted any comments regarding this story. Be the first!