In this lesson, you will learn how to send emails using PHP.

Send email using PHP

Nowadays, almost every web application use mail functionality. Even simple one-page business websites have these contact forms where you enter your email, contact email, and message which goes directly to the business management or owner’s inbox. This looks convenient for the users as well, as they don’t have to open and use a separate email sending service like Gmail, Hotmail, etc. to send contact the business.

In PHP, sending an email is really easy. You can send a simple text message or a fancy newsletter using a simple mail() function. To understand how this function works, let’s have a look at its syntax first.

The syntax

mail ( string $to , string $subject , string $message [, mixed $additional_headers [, string $additional_parameters ]] ) : bool

The parameters

  • $to – the email id of the person you want to send email to.
  • $subject – the subject of the email.
  • $message – The message goes here. Each line of text should be separated with a CRLF(\r\n). mail() function require you to keep each line limited to 70 characters.
  • $additional headers (optional) – It is an array or string to be inserted at the end of the email header. It is used to add extra headers like From, Cc, and Bcc. To separate extra headers, CRLF is used.
  • $additional parameters – It can be used to pass additional flags.
  • If mail is sent successfully, a Boolean value true is returned, false otherwise.

Sending plain email

Sending plain text email using mail() function is pretty straight forward.

Example of sending email in PHP

<?php
$ToEmail = "your_friend@gmail.com";
$Subject = "Let's meet";
$MessageBody = "Hi John! I would like to see you regarding a business proposal. Let me know if you are interested. ";
if(mail($ToEmail, $Subject, $MessageBody))
{
echo 'Email has been sent successfully!';
} 
else
{
echo 'Sorry! We couldn\'t send your email. Please try again.';
}
?>

Sending HTML emails

If you try to send HTML email using mail() function, PHP will treat it as plain text. To make mail() function treat HTML elements separately, you need to write some additional headers.

Example of sending HTML email

<?php
$to = 'email@domain.com';
$subject = 'Subject';
$message = 'Lets meet';

// carriage return type (RFC)

$crlf = "\r\n";

// The main header

$headers = "From: name <whatever@sender.com>" . $crlf;
$headers .= "MIME-Version: 1.0" . $crlf;
$headers .= "Content-Type: text/html; charset = iso-8859-1; " . $crlf;
$headers .= 'From: '.$from.$crlf. 'Reply-To: '.$from.$crlf . 'X-Mailer: PHP/' . phpversion();

// The message

$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi John!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Hi John! Hope all is well! It has been a long time since we met. Why not you come over to my place this weekend ?</p>';
$message .= '</body></html>';

//send email using mail() function

if (mail($mailto, $subject, $body, $headers)) 
{
echo "Success! Mail has been sent successfully!";
} 
else echo "Mail couldn't be sent";
$msg =  mail($to, $subject, $message);

?>

 

PHP Sessions Tutorial Home PHP File Uploading
Last modified: April 18, 2019