W e b t u t o r i a l s

Define PHP

PHP Installation

PHP INI File

PHP Case Sensitivity

PHP Variable

PHP Echo & Print

PHP Operators

PHP Receiving Input

PHP Decision Making

PHP Loops

PHP Jumping Statement

PHP Image Gallery

PHP Arrays

PHP Date Functions

PHP String Functions

PHP Math Functions

PHP Functions

PHP Variable Scope

PHP Constant Variable

PHP Superglobals

PHP Form Validation

PHP Include Statement

PHP Filter

PHP File Handling

PHP File Upload

PHP Cookies

PHP Session

PHP Send Emails

PHP Captcha

PHP-MySQL Connectivity

PHP MySQL Select

PHP MySQL Insert

PHP MySQL Delete

PHP MySQL Update

PHP Send Emails

Sending email messages is very common for a web application, for example: sending a welcome email when a user create a new account on your website, sending newsletters to your registered users, or getting user feedback or comment through the website's contact from and many more. You can use the PHP built-in mail() function for sending email messages to one or more recipients dynamically from your PHP application either in a plain-text form or formatted HTML.

PHP mail() function syntax:

mail(to, subject, message, headers);
Parameter Description
to The recipient's email address.
subject Subject of the email to be sent. This parameter cannot contain any newline characters.
message Defines the message to be sent. Each line should be separated with a line feed-LF (\n). Lines should not exceed 70 characters.
headers (Optional)This is typically used to add extra headers such as "From", "Cc", "Bcc". The headers should be separated with a carriage return plus a line feed-CRLF (\r\n).

Sending Plain Text Emails

The simplest way to send an email with PHP is to send a text email.

<?php
$to = 'sendmail@email.com';
$subject = 'Test Mail';
$message = 'Hi this is a simple mail send from php function.'; 
$from = 'from@email.com';
$cc = 'somebodyelse@email.com';

// use wordwrap() if lines are longer than 70 characters
$message = wordwrap($message,70);

$headers = "From: ".$from . "\r\n" . "CC: ".$cc;

// Sending email
if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}
?>

Sending HTML Formatted Emails

When you send a text message using PHP then all the content will be treated as simple text. We're going to improve that output, and make the email into a HTML-formatted email. To send an HTML email, you need to provide additional headers as well as an HTML formatted message.

<?php
$to = 'sendmail@email.com';
$subject = 'Mail with HTML Format';
$from = 'from@email.com';
 
// 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";
 
// Create email headers
$headers .= 'From: '.$from."\r\n". 'Reply-To: '.$from."\r\n" .   'X-Mailer: PHP/' . phpversion();
 
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi There!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Did you got my mail?</p>';
$message .= '</body></html>';
 
// Sending email
if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}
?>