FahmidasClassroom

Learn by easy steps

Less Secure App

There are many ways to send email using PHP script. PHPMailer is one of the ways to send email using PHP. It is a PHP library that contains various functions to send email in different ways. The email sending operation can be tested from localhost by using PHPMailer. How you can send email in text format, HTML format and email with attachment are shown in this tutorial by using different examples.

Gmail account setup to send email

Select any Gmail account and login to your account for sending email using PHPMailer.

Type ‘less secure app’ in the search box and enable it.

Enable less secure apps to access gmail account smtp server.

Download the necessary files to send email

Create a folder named emailSend in htdocs folder. Download the following files and store in that folder.

class.phpmailer.php
class.smtp
PHPMailerAutoload.php

Example-1: Sending simple text email


<?php
//Include necessary script
require 'PHPMailerAutoload.php';
//Create an object
$mail = new PHPMailer();
//Enable SMTP
$mail->IsSMTP();
/* You can use the following debug option to find out any email sending error.
Here, 1 = errors and messages,
2 = messages only*/
//$mail->SMTPDebug = 2;
//Enable authentication
$mail->SMTPAuth = true;
//Enable secure transfer
$mail->SMTPSecure = 'tls';
//$mail->SMTPAutoTLS = false;
//Set SMTP host name
$mail->Host = 'smtp.gmail.com';
//Set SMTP port
$mail->Port = 587;

//Setup the following configuration for smtp authentication
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);

//Set your gmail email address as SMTP username
$mail->Username = 'wdpfr41@gmail.com';
//Set your email password as SMTP password
$mail->Password = 'hello@123';

//Set the sender address
$mail->setFrom('wdpfccsl@example.com', 'WDPF R41');
//Set the reply address
$mail->addReplyTo('wdpfccsl@example.com', 'WDPF R41');
//Set the recipient address
$mail->addAddress('wdpf41@yahoo.com');

/* You can set CC and BCC address by using the following functions

$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

*/

//Set the subject of the email
$mail->Subject = 'Email from CCSL';
//Set the message of the email
$mail->Body = 'This is a testing email';
//Call send() function to send the email
if(!$mail->send()) {
echo 'Email is not sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Email has been sent';
}
?>

Example-2: Sending simple HTML formatted email
Add the following setting and HTML formatted content as message body with the previous code to send HTML formatted email.


// Set email format to HTML
$mail->isHTML(true);
$message = '<h1>Testing Email</h1>
<p>This email has been send using phpmailer</p>';

Example-3: Sending email with attachment
Add the following code to send email by attaching an image file named ‘Tulips.jpg’ with the code of example-1.


$mail->addAttachment('Tulips.jpg');

Conclusion

You may need to modify the above code according to your PHP version and PHPMailer version. In this script, PHP version 7 and PHPMailer version 5 are used for sending email. Another way to send email using PHP is to use mail() function. It is a built-in function of PHP for sending email but you can’t test this function from localhost.