1. Introduction
As you know, for most applications, sending mail is a function that seems indispensable in the dozens of functions that the application must have. Today I will write a tutorial on how to send mail with Laravel 5.x.
Laravel uses a feature-rich and completely free SwiftMailer library for sending mail. In the past, sending mail to the library and config was quite complicated, but with this library in Laravel, we can easily send mail without too much trouble. SwiftMailer provides popular drivers such as SMTP, Mailgun, SpartPost, … will help us deploy the mailing feature much simpler and easier.
2. Install the package
All API drivers require the Guzzle HTTP library, which you can install through the Composer package manager with the following command:
1 2 | composer require guzzlehttp/guzzle |
3. Config file mail environment variables in the .env file
1 2 3 4 5 6 7 | MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> MAIL_PASSWORD=********* MAIL_ENCRYPTION=tls |
I explain a bit offline
The method to send mail is SMTP, host of gmail is smtp.gmail.com , port is 587, MAIL_USERNAME is your gmail, and MAIL_PASSWORD is your password. Here we need to note a few of the following:
If your gmail has 2-layer security turned on, it will not be possible to login to send mail with the gmail password as usual you still use to login, now you need to create an application password for gmail and get that password fill in the MAILPASSWORD key. Instructions on how to create an app password for gmail can be found here
Or if you do not like using the .env file, we can configure in config/mail.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | <?php return [ /* |-------------------------------------------------------------------------- | Mail Driver |-------------------------------------------------------------------------- | | Laravel supports both SMTP and PHP's "mail" function as drivers for the | sending of e-mail. You may specify which one you're using throughout | your application here. By default, Laravel is setup for SMTP mail. | | Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses", | "sparkpost", "log", "array" | */ 'driver' => env('MAIL_DRIVER', 'smtp'), /* |-------------------------------------------------------------------------- | SMTP Host Address |-------------------------------------------------------------------------- | | Here you may provide the host address of the SMTP server used by your | applications. A default option is provided that is compatible with | the Mailgun mail service which will provide reliable deliveries. | */ 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), /* |-------------------------------------------------------------------------- | SMTP Host Port |-------------------------------------------------------------------------- | | This is the SMTP port used by your application to deliver e-mails to | users of the application. Like the host we have set this value to | stay compatible with the Mailgun e-mail application by default. | */ 'port' => env('MAIL_PORT', 587), /* |-------------------------------------------------------------------------- | Global "From" Address |-------------------------------------------------------------------------- | | You may wish for all e-mails sent by your application to be sent from | the same address. Here, you may specify a name and address that is | used globally for all e-mails that are sent by your application. | */ 'from' => [ 'address' => env('MAIL_FROM_ADDRESS', ' <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> '), 'name' => env('MAIL_FROM_NAME', 'Example'), ], /* |-------------------------------------------------------------------------- | E-Mail Encryption Protocol |-------------------------------------------------------------------------- | | Here you may specify the encryption protocol that should be used when | the application send e-mail messages. A sensible default using the | transport layer security protocol should provide great security. | */ 'encryption' => env('MAIL_ENCRYPTION', 'tls'), /* |-------------------------------------------------------------------------- | SMTP Server Username |-------------------------------------------------------------------------- | | If your SMTP server requires a username for authentication, you should | set it here. This will get used to authenticate with your server on | connection. You may also set the "password" value below this one. | */ 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), /* |-------------------------------------------------------------------------- | Sendmail System Path |-------------------------------------------------------------------------- | | When using the "sendmail" driver to send e-mails, we will need to know | the path to where Sendmail lives on this server. A default path has | been provided here, which will work well on most of your systems. | */ 'sendmail' => '/usr/sbin/sendmail -bs', /* |-------------------------------------------------------------------------- | Markdown Mail Settings |-------------------------------------------------------------------------- | | If you are using Markdown based email rendering, you may configure your | theme and component paths here, allowing you to customize the design | of the emails. Or, you may simply stick with the Laravel defaults! | */ 'markdown' => [ 'theme' => 'default', 'paths' => [ resource_path('views/vendor/mail'), ], ], /* |-------------------------------------------------------------------------- | Log Channel |-------------------------------------------------------------------------- | | If you are using the "log" driver, you may specify the logging channel | if you prefer to keep mail messages separate from other log entries | for simpler reading. Otherwise, the default channel will be used. | */ 'log_channel' => env('MAIL_LOG_CHANNEL'), ]; |
Now we can leave the mail config key in the .env file because if it is in the .env file, the value will be taken from the .env file only, next we find the keys corresponding to the key in the .env file and correct it to your liking, otherwise it will use the default values.
4. Test the mailing function
Here we will test the sending function in the simplest way, not to mention creating the mailable class, we will only test the sending function via processing in a controller.
Create a controller file
1 2 | php artisan make:controller MailController --plain |
Copy the following code into the newly created MailController.php file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?php namespace AppHttpControllers; use IlluminateHttpRequest; use Mail; use AppHttpRequests; use AppHttpControllersController; class MailController extends Controller { public function basic_email() { $data = array('name'=>"Virat Gandhi"); Mail::send(['text'=>'mail'], $data, function($message) { $message->to(' <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> ', 'Tutorials Point')->subject ('Laravel Basic Testing Mail'); $message->from(' <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> ','Virat Gandhi'); }); echo "Basic Email Sent. Check your inbox."; } public function html_email() { $data = array('name'=>"Virat Gandhi"); Mail::send('mail', $data, function($message) { $message->to(' <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> ', 'Tutorials Point')->subject ('Laravel HTML Testing Mail'); $message->from(' <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> ','Virat Gandhi'); }); echo "HTML Email Sent. Check your inbox."; } public function attachment_email() { $data = array('name'=>"Virat Gandhi"); Mail::send('mail', $data, function($message) { $message->to(' <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> ', 'Tutorials Point')->subject ('Laravel Testing Mail with Attachment'); $message->attach('path/to/attachment.txt'); $message->attach('path/to/image.png'); $message->from(' <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> ','Virat Gandhi'); }); echo "Email Sent with attachment. Check your inbox."; } } |
Create a template file to test the view of the mail
resources / views / mail.blade.php Copy the code below into the newly created template file
1 2 3 | <h1>Hi, {{ $name }}</h1> <p>Sending Mail from Laravel.</p> |
Create route
app / Http / routers.php Copy the following code into the route file
1 2 3 4 | Route::get('sendbasicemail',' <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> _email'); Route::get('sendhtmlemail',' <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> _email'); Route::get('sendattachmentemail',' <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> _email'); |
Then visit the following url to check basic email
1 2 | http://localhost:8000/sendbasicemail |
And the output screen will look like this
Next, visit the url http://localhost:8000/sendhtmlemail
to check email with html
1 2 | http://localhost:8000/sendhtmlemail |
Output screen of html
And finally visit the following url http://localhost:8000/sendattachmentemail
to check email with attachments. We will see the following screen:
5. Finish
So basically we have the configuration and conditions for sending mail in the most basic way. This article is also a pretty basic guide and there are definitely shortcomings that you can contribute and ignore.
Reference source: https://laravel.com/docs/5.7/mail https://www.tutorialspoint.com/laravel/laravel_sending_email.htm