{"id":14919,"date":"2020-06-14T07:05:27","date_gmt":"2020-06-14T07:05:27","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=14919"},"modified":"2021-11-05T06:46:41","modified_gmt":"2021-11-05T06:46:41","slug":"how-to-setup-your-gmail-account-to-send-emails-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-setup-your-gmail-account-to-send-emails-in-laravel\/","title":{"rendered":"How to setup your Gmail account to send emails in Laravel"},"content":{"rendered":"\n<p>In my previous blog, I described how can you send an email with an attachment without saving it in the database you can read that <a rel=\"noreferrer noopener\" href=\"https:\/\/www.devopsschool.com\/blog\/how-to-send-email-in-laravel-with-attachment-without-saving-in-database\/\" target=\"_blank\">here<\/a>. <\/p>\n\n\n\n<p>Now let&#8217;s continue with this topic. If you are laravel lover then definitely somehow it comes in your mind that how I can send an email through my own Gmail account &#8230; Here is the solution for it  <\/p>\n\n\n\n<p>In this tutorial, we will discuss how to configure our Laravel applications to send emails using your Gmail account as a Gmail SMTP server with the default Laravel SMTP configurations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"952a\"><strong>Step 1: Configure Gmail SMTP Server in Laravel Application<\/strong><\/h2>\n\n\n\n<p>Laravel uses<code>&nbsp;<em>config\/mail.php<\/em><\/code>&nbsp;file for storing details used in mail sending. This file contains settings like MAIL_DRIVER, MAIL_HOST, MAIL_PORT, etc. In order to successfully send an email, we need to provide this information.<\/p>\n\n\n\n<p>To add this required information, we need not edit this<code>&nbsp;<em>config\/mail.php<\/em><\/code>&nbsp;file, rather we would supply these details accordingly in the&nbsp;<code><em>.env<\/em><\/code>&nbsp;file.<\/p>\n\n\n\n<p>Thus, open your<code>&nbsp;<em>.env<\/em><\/code>file which is located in your root directory of our application and checkout for this settings:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"337\" height=\"168\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/env-for-setup.png\" alt=\"default .env mail setup\" class=\"wp-image-14920\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/env-for-setup.png 337w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/env-for-setup-300x150.png 300w\" sizes=\"auto, (max-width: 337px) 100vw, 337px\" \/><figcaption>default env mail setup<\/figcaption><\/figure>\n\n\n\n<p>Now edit the details above as follows.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>MAIL_DRIVER=&nbsp;<em>smtp<\/em><\/li><li>MAIL_HOST=&nbsp;<em>smtp.googlemail.com<\/em><\/li><li>MAIL_PORT=<em>&nbsp;465<\/em><\/li><li>MAIL_USERNAME=<em>your <em>Gmail<\/em> username<\/em><\/li><li>MAIL_PASSWORD=<em>your <em>Gmail<\/em> password<\/em><\/li><li>MAIL_ENCRYPTION= <em>ssl<\/em><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7c12\"><strong>Step 2: Configure your Google Account<\/strong><\/h2>\n\n\n\n<p>Login to your Google Email Account and click on Google Account Button. This button is displayed when you click on the profile picture in your Gmail Dashboard as shown.<\/p>\n\n\n\n<figure class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"284\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/gmail-profile.png\" alt=\"\" data-id=\"14923\" data-full-url=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/gmail-profile.png\" data-link=\"https:\/\/www.devopsschool.com\/blog\/?attachment_id=14923\" class=\"wp-image-14923\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/gmail-profile.png 402w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/gmail-profile-300x212.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/figure><\/li><\/ul><\/figure>\n\n\n\n<p>Once you are on My Account Page then click on Security and scroll down to the bottom and you will find \u2018Less secure app access\u2019 settings. Click on the radio button to set it ON.<\/p>\n\n\n\n<figure class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/gmail-account--1024x576.png\" alt=\"\" data-id=\"14925\" data-full-url=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/gmail-account-.png\" data-link=\"https:\/\/www.devopsschool.com\/blog\/?attachment_id=14925\" class=\"wp-image-14925\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/gmail-account--1024x576.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/gmail-account--300x169.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/gmail-account--768x432.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/gmail-account--355x199.png 355w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/gmail-account-.png 1366w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/li><\/ul><figcaption class=\"blocks-gallery-caption\">Gmail Account Security Option<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9ea8\"><strong>Step 3: Send Emails from your Laravel Application<\/strong><\/h2>\n\n\n\n<p>At this point, all the basic setup has been completed. We can now write some Laravel PHP codes to send an email.<\/p>\n\n\n\n<p>To get started, create any controller of choice where the mail sending logic will be handled, then in this controller write your codes using the code snippet below as a guide.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/pradeeprjth\/4ac5b842f4eebff298dc2a2389ad0ae5.js\"><\/script>\n\n\n\n<p>In the above code, we are using our mail template as \u2019emails.mail\u2019 file. hence we need to create an \u2018emails\u2019 folder and the<code>&nbsp;<em>mail.blade.php<\/em><\/code>&nbsp;file at&nbsp;<code><em>resources\\views\\emails\\mail.blade.php<\/em><\/code><\/p>\n\n\n\n<p>Our test mail template&nbsp;<code>mail.blade.php<\/code>&nbsp;should just contain a few test codes as shown below.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">Hello <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">strong<\/span>&gt;<\/span>{{ $name }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">strong<\/span>&gt;<\/span>,\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>{{body}}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We are done, simply create any route of your choice to and start sending mails from your Laravel application. Enjoy <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"480\" height=\"270\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/giphy.gif\" alt=\"\" class=\"wp-image-14926\"\/><\/figure>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_26825\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_88993\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_26825\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>In my previous blog, I described how can you send an email with an attachment without saving it in the database you can read that here. Now let&#8217;s continue with this topic. If you are laravel lover then definitely somehow it comes in your mind that how I can send an email through my own&#8230;<\/p>\n","protected":false},"author":14,"featured_media":14927,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5201],"tags":[6140,6141,5203,5957],"class_list":["post-14919","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-gmail","tag-google-account","tag-laravel","tag-mail"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=14919"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14919\/revisions"}],"predecessor-version":[{"id":24766,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14919\/revisions\/24766"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/14927"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=14919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=14919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=14919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}