How to Send Email in PHP using SMTP | PHPMailer

Sending main in PHP application is very basic functionality in any PHP web application. if you work on a live web application then you should need many times to send mail notification or send a simple mail to the application's end-use. you can send whatever in your mail. like send simple text content or you can also send some files in the mail by attachment.

So, we will share with you in this article how to send mail in PHP using SMTP and in this example, we will also see how to send a file as an attachment in your mail.

You can send mail in PHP my SMTP library it provides us much helpful mail function for sending mail and works with mail functionality in web application. you will see send mail using SMTP in PHP application is very easy.

PHPMailer is one of the most popular open-source PHP libraries to send emails with. It was first released way back in 2001 and since then it has become a PHP developer’s favorite way of sending emails programmatically, aside from a few other fan favorites like Swiftmailer.

If you don't want to install this library by composer then download manually from this GitHub link PHPMailer/PHPMailer

Installing PHPMailer

You can install PHPMailer using Composer:

composer require phpmailer/phpmailer

Example - 1

In the first example, we will see how to send mail in PHP without a file attachment.

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer;

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

Example - 2

In this example, we will see how to send mail in PHP with a file attachment.

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

Here we are attaching two files i.e., file.txt which resides in the same directory as the script and images/profile.png which resides in images directory of the script directory.

Using Gmail SMTP Example

You can utilize the mail server of another host to send an email, but for this, you first need to have authentication. For example: to send an electronic mail from Gmail’s mail server you require to have a Gmail account.

SMTP is a protocol utilized by mail clients to send an electronic mail to send a request to a mail server. Once the mail server verifies the electronic mail it sends it to the destination mail server.

Here is an example of sending an electronic mail from Gmail’s mail server from your domain. You don’t need a local mail server to run the code. We will be utilizing the SMTP protocol:

require_once "vendor/autoload.php";

$mail = new PHPMailer;

//Enable SMTP debugging. 
$mail->SMTPDebug = 3;                               
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
//Provide username and password     
$mail->Username = "[email protected]";                 
$mail->Password = "super_secret_password";                           
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = 587;                                   

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recepient Name");

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

i hope you like this useful article.

Tags:

Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

Or

No Comment