March 29, 2024
how to send mail using phpmailer in php

Email, also known as electronic mail, is a method of exchanging messages between people over the internet. It’s a convenient and widely-used form of communication that allows people to send and receive messages quickly and easily. Below we have described step by step guide to send mail using PHPmailer in php

There are several ways to send email in PHP:

1. How to Send Mail using Simple PHPMail:

In PHP, you can use the built-in mail() function to send email messages. The mail() function takes several parameters, including the recipient’s email address, the email subject, the email message, and any additional headers. Here’s an example of how to use the mail() function to send an email in PHP:
$to = 'recipient@example.com';
$subject = 'Test email';
$message = 'This is a test email from PHP.';
$headers = 'From: sender@example.com' . "\r\n" .
'Reply-To: sender@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

Here is the complete simple mail in PHP with header details:

<?php
// recipient email address
$to = "recipient@example.com";
// subject
$subject = "Example Subject";
// message
$message = "This is an example message.";
// sender email address
$from = "sender@example.com";
// additional headers
$headers = "From: $from\r\n";
$headers .= "Reply-To: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// send email
mail($to, $subject, $message, $headers);
?>

In this example, we define the recipient’s email address using the $to variable, the email subject using the $subject variable, and the email message using the $message variable. We also define the email headers using the $headers variable. The headers specify the sender’s email address and other optional parameters, such as the email’s reply-to address and the version of PHP being used.
Finally, we call the mail() function, passing in the recipient’s email address, subject, message, and headers as arguments. This function sends the email.
Note that the mail() function may not work on all servers, and you may need to configure the PHP mail settings on your server to send email properly. Additionally, sending email using the mail() function may not be the most reliable or secure method, and you may want to consider using a third-party email service provider instead.

2.How to Send Mail Using PHPMailer in PHP:

PHPMailer is a popular third-party library for sending emails in PHP. It provides a more robust and flexible solution than the built-in mail() function.
Here’s an example of how to send an email using PHPMailer:

1. Download and install PHPMailer:
You can download the latest version of PHPMailer from the official website or install it using Composer.
If you don’t have composer installed on your system download composer first and install it on your system. Visit the official composer website or click here to download composer.

2. Run the following command to install PHPMailer:

composer require phpmailer/phpmailer

install composer command

and press enter.
3. After Successfully installing PHPMailer you will see the following directory structure:

PHPMailer Directory Structure

– Composer is installed with an “autoload.php” file you can use.
– Import the PHPMailer class into the global namespace.
Note: Make sure that these lines are at the top of the script not inside any function.

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

Load the composer’s autoloader.
require 'vendor/autoload.php';
Create a Mailer Object:
$mail = PHPMailer();
Now write a below script:
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'yourhostname.com'; // Specify main SMTP server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'youremailid@mail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption,'ssl' also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from@senderemailid.com',); // Set sender of the mail
$mail->addAddress(to@receivermail.com'); // Add a recipient
$mail->addAddress(secondto@receiver.com'); // Name is optional
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'HTML message body in bold!';
$mail->AltBody = 'Body in plain text for non-HTML mail clients';

here is the screenshot:

And finally send the mail using:
$mail->send();

4. Here we have written a Complete PHPMailer Script:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';

$mail = new PHPMailer(true);
try {
	$mail->SMTPDebug = 2;					
	$mail->isSMTP();								
	$mail->Host = 'yourhostname.com';					
	$mail->SMTPAuth = true;							
	$mail->Username = 'youremailid@mail.com';				
	$mail->Password = 'your_email_password';			
	$mail->SMTPSecure = 'tls';						
	$mail->Port = 587;
	$mail->setFrom('from@frommail.com', 'FromName');		
	$mail->addAddress('receiver@mail.com');
	$mail->addAddress('receiver2@mail.com');
	$mail->isHTML(true);							
	$mail->Subject = 'Subject';
	$mail->Body = 'HTML message body in <b>bold</b> ';
	$mail->AltBody = 'Body in plain text for non-HTML mail clients';
	$mail->send();
	echo "Mail has been sent successfully!";
} catch (Exception $e) {
	echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

The above code will send an email using PHPMailer. Note that you can set various other properties and options for the email, such as CC and BCC addresses, attachments, and email templates. Check out the PHPMailer documentation for more information on how to use this library to send emails in PHP.

You can also download Complete PHPMailer Script
DOWNLOAD SCRIPT

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *