
How to send mail using smtp in Laravel ?
In this article, we will send mail using the PhpMailer library .
PHPMailer is perhaps the most popular open-source PHP library to send emails with. Using the below step we can send mail using this library.
Step 1: Install the PhpMailer library.
First, install phpmailer package using composer into your laravel project.
composer require phpmailer/phpmailer
Step 2: Use below code for send mail
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username'; // your email (without two step verification on)
$mail->Password = 'smtp_password'; // your email password
$mail->SMTPSecure = 'encryption'; // TLS
$mail->Port = 'smtp_port'; // 587
//Recipients
$mail->setFrom('from_mail'); // from_mail : this email will be recipients email
$mail->addAddress('receiver_email'); // receiver_email: whom you want to send mail
// Content
$mail->isHTML(true);
$mail->Subject = 'subject' ; // subject: email subject
$mail->Body = view('emails.mail_template', compact('variables')); // variables: if you want to pass variable which include data
$mail->send();