1 Answer 5394 Views
Sending mail via gmail from an application built on laravel is very easy. In fact, Laravel Mail recommends developers to use other APIs such as mailgun and sparkpost to send mail if possible as they are simpler and faster than SMTP servers. If you are thinking what SMTP stands for then it stands for Simple Mail Transfer Protocol, which is the standard protocol for email exchange on the internet. These API drivers need Guzzle HTTP library which can be installed via the Composer package manager.
composer require guzzlehttp/guzzle
However, sending mail via SMTP servers don't require that library. Here are the steps you need to follow to send mail via gmail from an application built on laravel.
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=webtrickshome@gmail.com MAIL_PASSWORD=j8Iy1sU5@p*^GPQ%%^ MAIL_ENCRYPTION=tls
Sending a mail via Laravel application requires the details of the mailing address it's going to use. Define the sender address in MAIL_USERNAME and it's password in MAIL_PASSWORD fields. The MAIL_DRIVER will be smtp and the MAIL_HOST will be smtp.gmail.com. Define the MAIL_PORT as 587 and the MAIL_ENCRYPTION as tls. TLS stands for tansport layer security that encrypts the email being exchanged to prevent it from being read by third parties across the server.
Gmail tends to block usage of mailing addresses which are being used in other applications as username for security reasons. Either you should create a new email address for mail purpose or you must go to the Less Secure App Access and turn on the access for less secure apps. Gmail will send you a mail for confirmation from where you can verify that those changes were made by yourself. Only then, you can use such mailing addresses for mailing purpose through appliations.
The same configuration needs to be defined in your config » mail.php configuration file as well. Mail won't be sent without the address defined in the 'from' array in this file. You can leave the username and password field undefined though.
'driver' => env('MAIL_DRIVER', 'smtp'), 'host' => env('MAIL_HOST', 'smtp.gmail.com'), 'port' => env('MAIL_PORT', 587), 'from' => [ 'address' => 'webtrickshome@gmail.com', 'name' => 'webtrickshome', ], 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'),
php artisan make:mail customMail
Each mailable class will contain a constructor method and a build method as shown below. The constructor() method is used to retrieve the public data that is made available from your other controllers to integrate in the mail while build() method is used to call various inbuilt methods like from, subject, view, attach, etc.
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; class customMail extends Mailable { use Queueable, SerializesModels; public function __construct() { } public function build() { return $this->view('view.name'); } }
Laravel supports custom blade template for mails which you can define via the view() method inside the build() method of mailable class. Make sure that you add all the styles inline in that template or else those styles won't be applied in the mails. Here, we've added a blade template named Helloworld.
public function build() { return $this->view('mails.HelloWorld'); }
You can also define the subject of your mail via subject() method inside the build() method. Laravel tends to take up the mailable class name as default subject of your mail.
public function build() { return $this->subject('Hello World')->view('mails.HelloWorld'); }
Now, you can simply instantiate the mailable class in your controller form where you are sending the mail after adding the namespaces on the top.
use Mail; use App\Mail\customMail; public function sendMail(Request $request){ $email = $request->email; Mail::to($email)->send(new customMail()); if( count(Mail::failures()) > 0 ){ session::flash('message','There seems to be a problem. Please try again in a while'); return redirect()->back(); }else{ session::flash('message','Thanks for your message. Please check your mail for more details!'); return redirect()->back(); } }
Here, $request is assumed to hold the data being sent to the controller that holds the email address of the receiver. It's good to verify if the mail is sent or not via the application so we added a little more blocks of codes to check if any error occurs in the process while redirect()->back() redirects user back to the same page from where the mail function is initialized..
If you are working on the registration process of an user, it's much better to send a link in the email template and validate the user once he clicks that link. That'll ensure that the email address provided by the user exists in real. Gmail sends a delivery failure notice to the sender's email address as well in case the receiving email address does not exist.
Sending a personlized email would be more appealing to the receiver rather than the plain one when we want to make them carry out some tasks via that email which may be completing the registration or confirming subscription or anything else.
Simply, go to your mailable class and add use Illuminate\Http\Request on the top as shown below.
namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Http\Request;
Now, you can use the variables from the form that is used to initialize this mailable class with a little modification in your build() method.
public function build(request $request) { return $this->subject('Hello World')->view('mails.HelloWorld',['request'=>$request]); }
You can even declare the values you need individually here like 'receiver' =>$request->receiver but it'll be more easier to use it in the mail blade as shown below.
Thank you Mr/Ms {{ $request->receiver }} More texts as required ...
Leave a comment