9.7 C
Thursday, March 30, 2023

PHP mail : How to use phpmailer

Must read

In this article, you going to see the PHP mail, How to use phpmailer in a detail and step by step in a simple way.

If you are looking for this topic then you are at right place.

Let’s have a look PHP mail and How to use phpmailer

What is PHP mail?

The PHP mail is a function allows you to send emails directly with the help of a script.

Below is the Syntax.


Now we going to discuss regarding phpmailer.

What is phpmailer and How to use phpmailer?

PHPMailer is a code library and it is also used to send mails very securely and with the help of PHP code by a web server. Sending emails directly through PHP code demands a high tech familiarity with SMTP regular protocol and relevant topics and vulnerabilities about Email injection for spamming. PHPMailer simplifies the practice of sending emails and it’s quite simple to use.

How to install phpmailer?

Setup: The perfect method to set up PHPMailer is using composer. Before proceeding be sure to set up composer.

Wait for the installation to complete. It will download all the necessary classes to your project folder.

Using PHPMailer: Import the PHPMailer class into the global namespace.

Notice: Be sure these lines are on peak of the script not in any function.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

After that, Step is to Load the composer’s autoloader.

require 'vendor/autoload.php';

Now, Create a PHPMailer class object.

$mail = PHPMailer()

What is php mail configuration?

Below are given the requirement to Configure the server settings:

  • SMTPDebug: Used to display messages regarding problems in connectivity and sending emails. It has the following values:
  • 0: It is the default value. Disable debugging.
  • 1: Display output messages sent by the client.
  • 2: As 1, plus display responses received from the server.
  • 3: As 2, plus more information about the initial connection – this level can help diagnose STARTTLS failures.
  • 4: As 3, plus display even lower-level information.
  • isSMTP(): It is used to Set mailer to use SMTP.
  • isMail(): It is used to Set Set mailer to use PHP’s mail function.
  • Host: Represent the servers.
  • SMTPAuth: It’s Enable/Disable SMTP Authentication.
  • Username: Represent the username.
  • Password: Represent the password.
  • SMTPSecure: Represent encryption technique. The main use is to Accepted values ‘tls’ or ‘ssl’.
  • Port: Represent the TCP port that is to be connected.

How PHP mail function works ?

$mail->SMTPDebug = 2;                   // Enable verbose debug output
$mail->isSMTP();                        // To Set mailer to use SMTP
$mail->Host       = 'smtp.techone.com;';// Represent main SMTP server
$mail->SMTPAuth   = true;               // Enable SMTP authentication
$mail->Username   = '[email protected]'; // SMTP username
$mail->Password   = 'password';         // SMTP password
$mail->SMTPSecure = 'tls';              // Enable TLS encryption, 'ssl' also accepted
$mail->Port       = 587;                // TCP port to connect to

Next to Add the recipients of the mail.

$mail->setFrom('[email protected]', 'Name');           // Set sender of the mail
$mail->addAddress('[email protected]');           // Add a recipient
$mail->addAddress('[email protected]', 'Name');   // Name is optional

Add attachments (if any).

$mail->addAttachment('url', 'filename');    // Name is optional

Add the content.

  • isHTML(): If passed true, sets the email format to HTML.
  • Subject: Set the subject of the Mail.
  • Body: Set the contents of the Mail.
  • AltBody: Alternate body in case the e-mail client doesn’t support HTML.
  • $mail->isHTML(true);
  • $mail->Subject = ‘Subject’;
  • $mail->Body = ‘HTML message body in bold!’;
  • $mail->AltBody = ‘Body in plain text for non-HTML mail clients’;
  • Finally, send the email.

And Your e-mail would be sent.

How to use PHP mail function?

Program: Complete PHP program to send e-mail using PHPMailer.

With the help of below code snippet you come to understand regarding PHPmailer.


use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception; 

require 'vendor/autoload.php'; 

$mail = new PHPMailer(true); 

try { 
	$mail->SMTPDebug = 2;									 
	$mail->Host	 = 'smtp.techone.com;';					 
	$mail->SMTPAuth = true;							 
	$mail->Username = '[email protected]';				 
	$mail->Password = 'password';						 
	$mail->SMTPSecure = 'tls';							 
	$mail->Port	 = 587; 

	$mail->setFrom('[email protected]', 'Name');		 
	$mail->addAddress('[email protected]'); 
	$mail->addAddress('[email protected]', 'Name'); 
	$mail->Subject = 'Subject'; 
	$mail->Body = 'HTML message body in <b>bold</b> '; 
	$mail->AltBody = 'Body in plain text for non-HTML mail clients'; 
	echo "Mail has been sent successfully!"; 
} catch (Exception $e) { 
	echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; 

- Advertisement -spot_img

More articles


Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest article