In this article, you going to see the PHP mail, How to use phpmailer in a detail and step by step in a simple way.
If you are looking for this topic then you are at right place.
The PHP mail is a function allows you to send emails directly with the help of a script.
Below is the Syntax.
mail(to,subject,message,headers,parameters);
Now we going to discuss regarding phpmailer.
PHPMailer is a code library and it is also used to send mails very securely and with the help of PHP code by a web server. Sending emails directly through PHP code demands a high tech familiarity with SMTP regular protocol and relevant topics and vulnerabilities about Email injection for spamming. PHPMailer simplifies the practice of sending emails and it’s quite simple to use.
Setup: The perfect method to set up PHPMailer is using composer. Before proceeding be sure to set up composer.
Wait for the installation to complete. It will download all the necessary classes to your project folder.
Using PHPMailer: Import the PHPMailer class into the global namespace.
Notice: Be sure these lines are on peak of the script not in any function.
use PHPMailer\PHPMailer\PHPMailer;use PHPMailer\PHPMailer\Exception;
After that, Step is to Load the composer’s autoloader.
require 'vendor/autoload.php';
Now, Create a PHPMailer class object.
$mail = PHPMailer()
Below are given the requirement to Configure the server settings:
$mail->SMTPDebug = 2; // Enable verbose debug output$mail->isSMTP(); // To Set mailer to use SMTP$mail->Host = 'smtp.techone.com;';// Represent main SMTP server$mail->SMTPAuth = true; // Enable SMTP authentication$mail->Username = '[email protected]'; // SMTP username$mail->Password = 'password'; // SMTP password$mail->SMTPSecure = 'tls'; // Enable TLS encryption, 'ssl' also accepted$mail->Port = 587; // TCP port to connect to
Next to Add the recipients of the mail.
$mail->setFrom('[email protected]', 'Name'); // Set sender of the mail$mail->addAddress('[email protected]'); // Add a recipient$mail->addAddress('[email protected]', 'Name'); // Name is optional
Add attachments (if any).
$mail->addAttachment('url', 'filename'); // Name is optional
Add the content.
$mail->send();
And Your e-mail would be sent.
Program: Complete PHP program to send e-mail using PHPMailer.
With the help of below code snippet you come to understand regarding PHPmailer.
<?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 = 'smtp.techone.com;'; $mail->SMTPAuth = true; $mail->Username = '[email protected]'; $mail->Password = 'password'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->setFrom('[email protected]', 'Name'); $mail->addAddress('[email protected]'); $mail->addAddress('[email protected]', 'Name'); $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}"; } ?>