This tutorial we are going send mail using Swiftmailer. We are going to create SMTP user to send mail here are the steps.
- Create SMTP user.
- Install SwiftMailer using composer.
- Create File for sending mail.
Install SwiftMailer using Composer.
composer require swiftmailer/swiftmailer
When installation completed, create new file for sending mail and include autoload.php file.
Create sendmail.php file
<?php
require_once './vendor/autoload.php';
$transport = (new Swift_SmtpTransport(SMTP_SERVER, SMTP_Port))
->setUsername(SMTP_Username)
->setPassword(SMTP_Password)
;
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create a message
$message = (new Swift_Message($subject='Wonderful Subject'))
->setFrom([SMTP_Username => $from_mail='John Due'])
->setTo($email)
->setBody('<h1>This is HTML body text</h1>' ,"text/html");
// Send the message
$result = $mailer->send($message);
if ($result) {
echo "Message has been successfully sent!";
}