PHP Send Email Example

E-mail(electronic mail) has become an integral part of the internet. Approximately all tech savy people have an email address. If you have a facebook account, you sure have registered it with an email address.

E-mails are simply messages that can contain text, attachments and they are sent through a computer network to individuals or groups of individuals.
In this example we are going to learn how to send e-mails with PHP.
For this example we will use:

  1. A computer with PHP 5.5 installed
  2. notepad++


1. PHP mail() function

function boolean mail(to,subject,message,headers,parameters) is used to send e-mails in PHP. It takes six parameters and returns true if email was sent, or false on failure.

  • to: This is a required field and it specifies the receiver of the email. It must be a correctly formated e-mail address.
  • subject: This is a required field. It specifies the subject of the email. This field should not contain new line characters.
  • message: This is a required field. It specifies the message to be sent. Lines should not be larger than 70 characters and each line should be separated by (\n)
    Note: (Windows only) If a full stop is found on the beginning of a line in the message, it might be removed. To solve this problem, replace the full stop with a double dot:

    $text = str_replace("\n.", "\n..", $text);
  • headers: This field is optional. It specifies the headers
  • parameters: This is an optional field, it specifies additional parameter.


// the message
$msg = $_POST["message"];
$rece= $_POST["receiver"];
// use  if lines arelonger than 70 characters
$msg = wordwrap($msg,70);
// send email
echo "done";

echo "error";



<!DOCTYPE html> 
<html lang=en>
	html, body{
	font-family:"helvetica","verdana","calibri", "san serif";

	 		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
	<div id=cen align=center>
	<h1>Email Client</h1>
	<form action="mail.php" method=post>
<input type=email class=a placeholder=receiver name=receiver required><br>

<input type=text class=a placeholder=subject name=subject required><br>

<textarea rows="4" cols="50" required name=message></textarea><br>
<input type=submit class=a value="Send Mail" name=sendmail>



This script presents an html form to the user. The users fill in the receiver email. The subject of the email and the email body. On line ten we call PHP mail() function and pass it the required parameters. On line 8 we use PHP wordwrap function to break the message into new lines.

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

1.2 Sending emails with extra headers

This script sends an email to


$to = '' ;
$subject = 'Just Testing PHP mail function' ;
$message = 'I am just testing PHP mail function' ;
$headers = 'From: tester@example.
com' . "\r\n" . "CC:";
( $to , $subject , $message , $headers )

1.2 Sending an HTML email

When you send a message using PHP, then all the content will be treated as simple text. Even if you include HTML tags in the message, it will be displayed as simple text and HTML tags will not be formatted according to HTML syntax. PHP provides though an option to send an HTML message as actual HTML message. While sending an email message you can specify a Mime version, content type and character set to send an HTML email.

// multiple recipients
$to = '' . ', ' ;
// note the comma
$to .= '' ;
// subject
$subject = 'Sending you a test email' ;
// message
$message = '
  <title>Just testing this email, please do not take it too seriously</title>

  <h3><b>Reminder</b>: do not take this email too seriously</h3>
  <h2>This is a test email</h2>
  <h3>This is a test email</h3>
  <h4>This is a test email</h4>
  <h5>This is a test email</h5>
  <h6>This is a test email</h6>
' ;
// 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 <>' .
"\r\n" ;
$headers .= 'From: Group of testers
<>' . "\r\n" ;
$headers .= 'Cc: testerarchive@exa' . "\r\n" ;
$headers .= 'Bcc: testercheck@exam' . "\r\n" ;
// Mail it
mail( $to , $subject , $message , 
$headers );

2. Summary

In this example we learnt about emails and how to send different kind of e-mails in PHP.

3. Download the source code

You can download the full source code of this example here:  phpsendmailexample

Olayemi Odunayo

I am a programmer and web developer, who has experience in developing websites and writing desktop and mobile applications. I have worked with both schematic and schemaless databases. I am also familiar with third party API and working with cloud servers. I do programming on the client side with Java, JavaScript, html, Ajax and CSS while I use PHP for server side programming.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button