How to send Mail using PHP Mailer

How to send Mail using PHP Mailer

PHPMailer is a code library and used to send emails safely and easily via PHP code from a web server. Sending emails directly via PHP code requires a high-level familiarity to SMTP standard protocol and related issues and vulnerabilities about Email injection for spamming. PHPMailer simplifies the process of sending emails and it is very easy to use.

Installation: The best way to install PHPMailer is by using composer. Before proceeding make sure to install composer.

  • Open the Command prompt and go to the directory of the project in which you want to use PHPMailer.
  • Run the following command:

composer require phpmailer/phpmailer

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

Using PHPMailer:
Import the PHPMailerclasse into the global namespace.
Note: Make sure that these lines are at the top of the script not inside any function.

usePHPMailer\PHPMailer\PHPMailer;

usePHPMailer\PHPMailer\Exception;

Load the composer’s autoloader.

require 'vendor/autoload.php';

Create a PHPMailer class object.

 


 

$mail = PHPMailer()

Configure the server settings:

  • SMTPDebug: Used to display messages regarding problems in connectivity and sending emails. It has following values:
    • 0: It is 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(): Set mailer to use SMTP.
  • isMail(): Set mailer to use PHP’s mail function.
  • Host: Specifies the servers.
  • SMTPAuth: Enable/Disable SMTP Authentication.
  • Username: Specify the username.
  • Password: Specify the password.
  • SMTPSecure: Specify encryption technique. Accepted values ‘tls’ or ‘ssl’.
  • Port: Specify the TCP port which is to be connected.

 

Source code

 

Create send_mail.php

 

<?php

/**

 * This example shows sending a message using PHP's mail() function.

 */

 

require '../PHPMailerAutoload.php';

 

//Create a new PHPMailer instance

$mail = new PHPMailer;

//Set who the message is to be sent from

$mail->setFrom('from@example.com', 'First Last');

//Set an alternative reply-to address

$mail->addReplyTo('replyto@example.com', 'First Last');

//Set who the message is to be sent to

$mail->addAddress('whoto@example.com', 'John Doe');

//Set the subject line

$mail->Subject = 'PHPMailermail() test';

//Read an HTML message body from an external file, convert referenced images to embedded,

//convert HTML into a basic plain-text alternative body

$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

//Replace the plain text body with one created manually

$mail->AltBody = 'This is a plain-text message body';

//Attach an image file

$mail->addAttachment('images/phpmailer_mini.png');

 

//send the message, check for errors

if (!$mail->send()) {

echo "Mailer Error: " . $mail->ErrorInfo;

} else {

echo "Message sent!";

}


Please Share this course

It Looks like you have completed your course!! We have created Online series based on the above content. You can give a test and check your skill.




Be a Part of CollegeSpike by giving your precious comment here and help us in improving our content.