Laravel send e-mail A to Z

Laravel send e-mail A to Z

https://ift.tt/2w6CjjC

Sending e-mail is an important part of our application. Laravel made it easy with simple e-mail sending API. You can send email from STMP, Gmail, Webmail, Mailgun, Amazon SES, SendGrid, Mailtrap and etc with the same API. If you are looking for sending an email in Laravel framework via SMTP, Gmail, Mailgun, Amazon SES, Webmail, SendGrid or any email service then this post will help you by providing the complete guide on how to send an e-mail via Laravel 6. By following this post you can do exactly the same thing in Laravel 5 or Laravel 6. By completing this post, We’ll learn below.

  • How to send test e-mail for confirming all are ok via MailTrap.
  • How to send an email with Webmail SMTP in Laravel
  • How to send an email with Gmail in Laravel
  • How to send an email with SendGrid SMTP in Laravel
  • Laravel Send Mail without a view
  • Recommendations

 

Step 01: E-mail View

We have to make an e-mail template for sending the email content to our recipients. For that, Here I’m creating a simple HTML email template in resources/views/emails/template.blade.php with the content below.

<div>
 <h3>Hi, </h3>
 <p><p> 
 </div>

Look, In this template, we will pass $name and $msgBody variable data and send it to our recipients. With this simple e-mail template, I’ll show you how to send an e-mail via Mailtrap, SMTP, Gmail, Webmail etc.

Important Note Do not pass any variable name with message. If you do then it’ll conflict and you will get an exception.

 

Step 02: Define Route

We need to define a route in web.php file for sending an e-mail. Let’s do that.

Route::get('/send-mail','HomeController@sendMail');

 

Step 03: Controller Code

Now we need to code in sendMail function for sending our email.

// HomeController.php
 
 use Illuminate\Support\Facades\Mail;
 
 public function sendMail()
 {
 $data = [
 'name'=>'Jhon Smith',
 'msgBody'=>'Welcome to Laravel Article'
 ];
 
 Mail::send('emails.template',$data,function($mail){
 $mail->from('info@example.com');
 $mail->to('jhon@example.com');
 $mail->subject('Welcome E-mail');
 });
 
 return 'Successfully send';
 }

 

First testing E-mail with MailTrap

Here first we’ll send e-mails via Test mail sending sandbox service name Mailtrap. Mailtrap is really awesome for testing email before sending with our actual email sending credentials. We are doing this for confirming that our email is sending correctly with the correct content. Sometimes from Webmail, Gmail STMP are not work as we expected for some miss-configuration. If we succeed with MailTrap then we can ensure that our sending process ok we have to focus on other things.

  • Login to Mailtrap account and goto Demo Inbox.
  • From the integrations dropdown, select laravel
  • Now copy the env configuration and paste into your env file

  mailtrap-integration.png

Now, start the server php artisan serve and hit URL http://localhost:8000/send-mail

test-email-snap.png

 

2. How to send an e-mail with Webmail SMTP in Laravel?

– Do login into cPanel and create a Webmail.

– Collect mail client configuration for your webmail.

– Update configuration in your project env file.

MAIL_DRIVER=smtp
 MAIL_HOST=mail.example.com
 MAIL_PORT=465
 MAIL_USERNAME=info@example.com
 MAIL_PASSWORD=your_webmail_password
 MAIL_FROM_ADDRESS=from@example.com
 MAIL_FROM_NAME=Example

and finally, do exactly the same as step 1, 2, 3

Now, start the server php artisan serve and hit URL http://localhost:8000/send-mail

 

3. How to send an email with Gmail in Laravel?

Important: Sending e-mail using Gmail, first you need to enable less secure apps settings from your Google Account. If you do not enable this option then you will not able to send email using Gmail from your Laravel application.

– After enabling the less secure apps option, update the env file.

MAIL_DRIVER=smtp
 MAIL_HOST=smtp.gmail.com
 MAIL_PORT=587
 MAIL_USERNAME=your_gmail_address
 MAIL_PASSWORD=your_gmail_password
 MAIL_ENCRYPTION=tls

and finally, do exactly the same described in step 1, 2, 3

Now, start the server php artisan serve and hit URL http://localhost:8000/send-mail

 

4. How to send an email with SendGrid SMTP in Laravel?

Login to your SendGrid account.

Do sender authentication from Settings > Sender Authentication

Goto Settings > API keys and Create an API key by enabling Restricted Access ( Only send email )

sendgrid-api-key.png

 

Now update the env file with configuration.

MAIL_DRIVER=smtp
 MAIL_HOST=smtp.sendgrid.net
 MAIL_PORT=587
 MAIL_USERNAME=apikey
 MAIL_PASSWORD=your_api_secret
 MAIL_ENCRYPTION=tls

N.B: Keep exactly MAIL_USERNAME=apikey

and finally, do exactly the same described in step 1, 2, 3

Now, start the server php artisan serve and hit URL http://localhost:8000/send-mail

 

5. Laravel Send Mail without a view

For sending email without view in Laravel, you have to use the raw method of Laravel Mail facade. Just follow the steps below.

– Simply define a route.

Route::get('/send-mail','HomeController@sendMail');

– Setup mail credentials in env file from one of these ( Webmail, Gmail, SendGrid, Mailtrap etc)

– Do code in the sendMail method

// HomeController.php
 
 use Illuminate\Support\Facades\Mail;
 
 public function sendMail()
 {
 
 Mail::raw('Hi, welcome to laravelarticle.com!', function ($mail) {
 $mail->from('info@example.com');
 $mail->to('jhon@example.com');
 $mail->subject('Welcome E-mail');
 });
 
 return 'Successfully send';
 }

 

Recommendations

Use your credentials in the env file only local development. On the production side, do not put credentials in the env file, keep your credentials in the config/mail.php file.

 

Hope this post will help you to send email in Laravel framework with confidently. If this post helps you to learn how to send email in Laravel framework then please share this post with others.

programming

via Laravel News Links https://ift.tt/2dvygAJ

April 2, 2020 at 08:18AM