Send Email in Php

Wikitechy | 3704 Views | php | 13 Jun 2016


  • The mail() function is used to send emails directly from a script.
  • For the mail functions to be available in PHP, it requires an installed and working email system where the program to be used is defined by the configuration settings in the php.ini file.


Script for sending mail:

// The message
$message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('[email protected]', 'My Subject', $message);


Script for Sending mail with extra headers:

The addition of basic headers, tells the MUA the from and Reply-To addresses.






Mandatory. Define the receiver / receiver of the email


Mandatory. Specifies the subject of the email and in this subject we cannot define any new line.


Mandatory. Defines the message to be sent. Each line should be separated with a   LF - line feed (\n). Lines should not exceed 70 characters.


Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n).


Optional. Specifies an additional parameter to the send mail program (the one defined in the sendmail_path configuration setting). (i.e. this can be used to set the envelope sender address while using sendmail with the -f sendmail option)


Sample Script for sending mail using mail () function:

$to      = '[email protected]';
$subject = 'the subject';
$message = 'welcome to wikitechy';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);


Sending mail with an additional command line parameter

  • The additional_parameters parameter can be used to pass an additional parameter for the program configured to use while sending mail using the sendmail_path.
mail('[email protected]', 'the subject', 'the message', null,
   '[email protected]');


Sending HTML email

In php using the mail () function we can send a HTML email 

// multiple recipients
$to  = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';
// subject
$subject = 'Wikitechy Event Meet';
// message
$message = '
Wikitechy Event Meet</title>
Here are the Wikitechy upcoming event meet in November</p>
Bangalore</td><td>3rd</td><td> November </td><td>2016</td>
Chennai</td><td>17th</td><td> November </td><td>2016</td>

// 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: kavi <[email protected]>, arunprasath < [email protected]>' . "\r\n";
$headers .= 'From: Event Remainder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected] ' . "\r\n";
$headers .= 'Bcc: [email protected] ' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);

Applies to:

  • PHP 3 and 4
  • PHP 5
  • PHP 6 and Unicode
  • PHP 7

Related tags:

  • PHP mail() Function
  • Sending Emails using PHP 
  • Send mail - PHP
  • Basic PHP mail() Function code to send emails from
  • How to send an email using PHP? 
  • Sending Nice HTML Email with PHP 
  • PHP mail()
  • send email in php using smtp
  • send html email in php
  • send email in php code
  • send email in php code with form


Bug Bounty

Join our Community