Do you want to use the Gmail SMTP server for sending emails in Laravel? Sometimes your server does not send emails due to its limitations or misconfigurations. In such scenarios, you can use the SMTP server for sending emails. In this article, we discuss how to send email using the Gmail SMTP server in Laravel.
Using the SMTP server, your emails will not end up in the spam folder. In general, it will be treated as a genuine email and goes inside the user’s inbox.
One more benefit of using the SMTP server is, you can send emails from your local server also. It will be helpful to test the email functionality on the local server itself.
Setup of Gmail SMTP Server in Laravel
Laravel uses config/mail.php
file to store the details related to email providers. This file contains settings like MAIL_HOST, MAIL_PORT, MAIL_ENCRYPTION, etc. The user should provide this information which will be used by Laravel and send your emails.
To add this information, you don’t need to edit config/mail.php. Instead, you should store these details in the .env
file.
Open your .env
file which is located in your root directory and you will find below code related to email settings.
MAIL_MAILER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Edit the above details as follows.
MAIL_MAILER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=GMAIL_USERNAME
MAIL_PASSWORD=GMAIL_PASSWORD
MAIL_ENCRYPTION=ssl
Here, I set the driver as smtp, the host for Gmail as smtp.googlemail.com, the SMTP port for Gmail as 465, and the encryption method as ssl. Make sure to replace the placeholders GMAIL_USERNAME, and GMAIL_PASSWORD with your actual Gmail username and password.
In order to use the Gmail SMTP server, you need to change some settings on your Google account. Login to your Google account and click on ‘Account’. Once you are on the ‘Account’ page, click on ‘Security’. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set it to ON.
Code for Sending Email in Laravel
At this stage, you are completed with all basic setups. Now, you need to write a Laravel code that will send an email.
For this tutorial, I am going to use a ‘Mail’ class to write code. The user can also write the code using Laravel Mailable. It will not make any difference. In both cases, Laravel uses the Gmail SMTP server in the background.
When you use the ‘Mail’ class your code will be something like as written below.
$to_name = 'TO_NAME';
$to_email = 'TO_EMAIL_ADDRESS';
$data = array('name'=>"Sam Jose", "body" => "Test mail");
Mail::send('emails.mail', $data, function($message) use ($to_name, $to_email) {
$message->to($to_email, $to_name)
->subject('Artisans Web Testing Mail');
$message->from('FROM_EMAIL_ADDRESS','Artisans Web');
});
In the above code, I am using the view ’emails.mail’. It means you have to create a folder and file as resources->views->emails->mail.blade.php
.
Your mail.blade.php
will contain the code as follows.
Hi <strong>{{ $name }}</strong>,
<p>{{ $body }}</p>
That’s it! Now, in the background, Laravel will automatically use the Gmail SMTP server and send your emails.
Related Articles
- Laravel Email Testing: Mailtrap vs. Other Options
- How to Log Query in Laravel
- How to Install and Use CKEditor In Laravel
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
what is MAIL_PORT
hi sir ,
i ahve got errro like this after connecting to smtp
Expected response code 220 but got an empty response , Could you please help
will this work on live server
Yes. It will work in production.
no it doesnt work in production
Could you tell me what error you got?
Hello
I need to explain my situation.
Localhost (php artisan serve) : First I got some problem and I needed to add following code in config/mail.php and everything worked fine :
‘stream’ => [
‘ssl’ => [
‘allow_self_signed’ => true,
‘verify_peer’ => false,
‘verify_peer_name’ => false,
],
],
But when I hosted a website to 000webhost I got the following error :
Expected response code 250 but got code “550”, with message “550 5.7.1 Relaying denied ”
I would be very grateful for an answer. Thank You
The from mail address is not shown in mail.
Yeah, that’s the issue with Gmail SMTP. Alternatively, you can use the Mailjet SMTP server which allows setting our own from address. https://artisansweb.net/send-email-using-mailjetalternative-to-gmail-smtp-server-in-php
hello, dear!
Thanks for your article.
$to_name = ‘TO_NAME’;
$to_email = ‘TO_EMAIL_ADDRESS’;
$data = array(‘name’=>”Sam Jose”, “body” => “Test mail”);
To_NAME, TO_EMAIL_ADDRESS
what is that?
What can I write here?
Replace these placeholders with the actual values.
Is it required ssl certificate for shared hosting?
I followed all setting that you are are followed.
It show “Connection could not be established with host smtp.googlemail.com [Network is unreachable #101]”
We don’t need an SSL certificate for sending an email. We can even send emails from a local server using SMTP.
Hi Sajid,
Thank you for the tutorial! It was really helpfull.
The end of july, I received an email from Google Gsuite that they will drop support for apps with ‘less secure access’. They will remove the option the 30th of October.
This means we will have to authenticate differently. I don’t know what options there are , but keep in mind, this will probably not being not supported in the future.
regards,
tom
Hi Tom,
Thanks for the updates. I will go through it and update the article if needed.
Hi i got this error please help me out
Address in mailbox given [TO_EMAIL_ADDRESS] does not comply with RFC 2822, 3.6.2.
Hello good evening, I would like to realy appreciate this post thank you worked well on me. Now is that we pushed the files through GIT and my other members get to open this page where email is needed. They received the error of 250 but received 530. Hope you see this asap need help. Thank you
Did you check .env file? This file should have Gmail details added.
sir
please help me
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=email@domain.com
MAIL_PASSWORD=123456
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS= What is here????????
MAIL_FROM_NAME= What is here????????
I do not understand what I want to write here
You don’t need to add these details as GMAIL server overrides it. But when you don’t use GMAIL SMTP then you can put your admin address and name there.
so what if we use, MAIL_HOST=smtp.googlemail.com
i had this problem InvalidArgumentException
View [emails.mail] not found.
i verified the path of mail.blade.php inside the views folder and its correct
It should not be the case. There must be something wrong in your code or file path. Can you share a screenshot of filesystem and code you have written?
Hi,
I’m new to Laravel. Can you tell me which file should the following code be written on? Thank you.
$to_name = ‘TO_NAME’;
$to_email = ‘TO_EMAIL_ADDRESS’;
$data = array(‘name’=>”Sam Jose”, “body” => “Test mail”);
Mail::send(’emails.mail’, $data, function($message) use ($to_name, $to_email) {
$message->to($to_email, $to_name)
->subject(‘Artisans Web Testing Mail’);
$message->from(‘FROM_EMAIL_ADDRESS’,’Artisans Web’);
});
Regards.
This code will go in your controller file.
Thanks for your help, it served a lot
Hello,
This works just fine, thanks so much for sharing!
I just have one question, how can i do to set the subject and the to_email_address by using variables??
Something like:
Mail::send($template, $data, function($message) {
$message->to($to_email_adrres, $to_name)
->subject($subject);
});
My $data variable has that info, but is not available inside the send function, and if I pass an object through the function it throws me an error.
You can do this through ‘use’ statement. I did this in my another article https://artisansweb.net/using-cron-schedule-in-laravel-to-automate-tasks/
How can I send mails using corporate email accounts other than gmail?
You should use your company email’s SMTP server.
Thanks for great tutorial.
One challenge though. My emails are being dropped on spam folder. Any idea why?
“Class ‘App\Http\Controllers\Mail’ not found”
i am getting this error
You need to include below statement in your controller.
use Mail;
this is great document
i have the error of Connection could not be established with host smtp.googlemail.com [ #0]
Is there a way we can configure without enabling “Less Secure App” setting please?
We have to enable ‘Less Secure App’. This is how Gmail SMTP works.
I get error like this
with host smtp.googlemail.com [Network is unreachable #101]
thank you! you saved me a million years
(1/1) Swift_TransportException
Process could not be started [The system cannot find the path specified.
]
how to fix this?
what to add in the route/web.php
Hello, the email is not being saved in the outbox.
Can you help me?
Daniel, saving email in the outbox is up to your email client. This tutorial is only focused on sending emails to users.
help me please
Connection could not be established with host smtp.googlemail.com [ #0]
Try by setting mail_driver to sendmail. Sometimes server failed to connect with SMTP driver.
MAIL_DRIVER=sendmail
how to send email by clicking the button?
Write the code in the controller function which path you have set for form action.