Skip to main content

Send Email via SMTP server using PHPMailer

Email sending is the most useful feature for web application. mail() function in PHP is used to send mail from PHP script.While sending email using mail() ,the mail will be sent from web server. Sometimes it may cause issues on sending an email and fails to deliver. With SMTP you can overcome this issue,when you send an email through SMTP server email will be sent through mail server  rather than web server.
      The easiest way to send email in PHP with SMTP is to use PHPMailer library. PHPMailer provides an ability to send email via SMTP server in PHP.

In this tutorial, you’ll show how you can send HTML email with SMTP in PHP using PHPMailer.

Send Email via SMTP server:

PHPMailer library:
The script uses PHPMailer to send the email via SMTP server, so PHPMailer library needs to be included first. Include the PHPMailerAutoload.php file and create a new PHPMailer instance.

<?php
require ("phpmailer/PHPMailerAutoload.php");
$mail = new PHPMailer ();
?>

SMTP Configuration:
you need to specify SMTP server host,username,password and port

<?php
// Include and initialize phpmailer class
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->Host = 'smtp.gmail.com';
//SMTP Configuration
$mail->isSMTP();
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Password = 'your password'; // SMTP password
$mail->Username = 'user@example.com'; // SMTP username
$mail->addReplyTo('info@example.com', 'esoftsolutions');
$mail->setFrom ( 'info@example.com', 'esoftsolutions' );
// Add a recipient
$mail->addAddress ( 'sowmya@gmail.com' );
// Set email format to HTML
$mail->isHTML ( true );
// Email body content
$mail->AltBody = '<h1>Hi this is test mail</h1>';
$mail->Subject = 'Test Email';
$mail->Body = '<h1>Hi this is test mail</h1>';
// Send email
if (! $mail->send ()) {
}
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
?>

Note that:  You need to download PHPMailer library files separately

Send HTML Email with Attachments:
Use addAttachment() method of PHPMailer class to add an attachment to the email. You can add multiple attachments to the email by adding addAttachment() method multiple times.
<?php
// Add attachments
$mail-&gt;addAttachment('softsolutions_1.pdf');
$mail-&gt;addAttachment('softsolutions2_2.docs');
$mail-&gt;addAttachment('images/softsolutions.png', 'new-name.png'); //set new name
?>

Send Email using Gmail SMTP:
If you want to use Gmail SMTP to send email, you need to make some changes in Google account settings.
Follow the below steps to use Gmail SMTP in PHPMailer library.
  • Login to your Google account.     
  • Go to the My Account page. Click the Signing in to Google link from Sign-in & security section.

  • Scroll down the Password & sign-in method section and turn Off the 2-Step Verification.

  • Scroll down the Connected apps & sites section and turn On Allow less secure apps.

  • You are done! Now you can use Gmail SMTP to send email from the PHP script.
If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook.Next Week i will come with a new php solution. Thank you! Have a nice day ..😃😃


Comments

Popular posts from this blog

Reading and Generating QR codes in Python using QRtools

What are QR codes? A Quick Response (QR) code is a 2 dimensional barcode that is used due to its fast readability and relatively large storage capacity.  2 dimensional barcodes are similar to one dimensional barcodes, but can store more information per unit area. Installation and Dependencies Linux:   qrtools can be installed on debian based linux systems with the following commands $sudo apt-get update $sudo apt-get install python-qrtools The following dependencies must be installed as well [sudo] pip install pypng [sudo] pip install zbar [sudo] pip install pillow Windows:   qrtools can be installed on windows by downloading the file from here(https://pypi.python.org/pypi/qrtools/0.0.1). On downloading and extraction, run the following command from inside the folder python setup.py install Generating a qrCode: qrtools contains a class QR (can be viewed in the source code), for which we must initially create an object. The object takes the ...

MYSQL CONNECTION USING PDO WITH PHP

Hello guys, In the previous tutorial we have seen how to connect to MySQL database with PHP using MySQLi. Today we will learn how to connect with PDO. As a PHP programmer we have learned how to access database by using either MySQL and MySQLi. As of PHP 5.1, there's a better way. PHP Data Objects(PDO) provide methods for prepared statements and working with objects that will make you far more productive! PDO Introduction :        PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. Database Support:       The extension can support any database that a PDO driver has been written for. The following drivers currently implement the PDO interface: PDO_CUBRID Cubrid PDO_DBLIB FreeTDS / Microsoft SQL Server / Sybase PDO_FIREBIRD Firebird PDO_I...