Sending Emails using PHP - Manoj Patial

Sending Emails using PHP

Top 7 PHP Frameworks For Developers
April 28, 2018
How to create WordPress custom plugin
May 2, 2018

In this article I’ll show you how to send email using PHP mail() function. This article will cover send email in both format plain text and HTML.

The PHP mail() Function

It is very easy to send email from a PHP script. You can even specify whether the PHP email script should use a local or remote SMTP server for sending messages.

You can use the PHP built-in mail() function for sending an email messages to one or more recipients dynamically from your PHP application either in a plain text form or in HTML format. The basic syntax of this function is:

<?php
mail($to,$subject,$message,$headers,$parameters);
?>

This function requires three mandatory parameters that specify the email address of recipient, subject of the the email and the message additionally there are other two optional parameters.

Here is the description for each parameters.

ParameterDescription
$toRequired. The recipient’s email address.
$subjectRequired. The subject of the emails sent through this PHP script.
$messageRequired.This parameters message defines the message to be sent.
$headersOptional. $headers can be used to include information such as CC, BCC
$parametersOptional. Specifies an additional parameter to the send mail program

After the mail function is called PHP will send the email then and return true if successful or false if it is failed.

We can specify multiple recipients as the first argument to the mail() function in a comma separated list.

Send Plain Text Emails

Simplest way to send an email with PHP is to send an email in text format. In the below example we will first specify the variables like recipient’s email address, subject and message body and then we pass these variables to the mail() function to send the email.

<?php
$to = 'contact@manojpatial.com';
$subject = 'Welcome!';
$message = 'Hi User, Thanks for contact with me.'; 
$from = 'user@example.com';

// Sending email
if(mail($to, $subject, $message)){
    echo 'Message sent successfully.';
} else{
    echo 'Message could not be sent. Please try again.';
}
?>

Send Emails in HTML Format

When you want to send a text message using PHP then all the content will be treated as simple text. Now we will improve that output and make the email into a HTML formatted email.

The process will be the same for send an HTML email. But for this we need to provide additional headers like mime version, content type and character set as well as a message in HTML format.

<html>

   <head>
      <title>Sending HTML email using PHP</title>
   </head>

   <body>

      <?php
        $to = 'contact@manojpatial.com';
		$subject = 'Welcome!';
		$from = 'user@example.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 User!</h1>';
		$message .= '<p style="color:#080;font-size:18px;">Thanks for contact with me</p>';
		$message .= '</body></html>';

		// Sending email
		if(mail($to, $subject, $message)){
			echo 'Message sent successfully.';
		} else{
			echo 'Message could not be sent. Please try again.';
		}
      ?>

   </body>
</html>
0 0 vote
Article Rating
Manoj Patial
Manoj Patial
I am Manoj Patial, a website developer from India, I have more than 10 years of experience in website development. I developed more than 200+ website using Drupal, WordPress, PHP, Codeigniter, shopify, HubSpot and other CMS/PHP frameworks.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Pin It on Pinterest

0
Would love your thoughts, please comment.x
()
x