{"id":14878,"date":"2020-06-13T14:19:25","date_gmt":"2020-06-13T14:19:25","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=14878"},"modified":"2021-11-05T06:49:32","modified_gmt":"2021-11-05T06:49:32","slug":"how-to-send-email-in-laravel-with-attachment-without-saving-in-database","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-send-email-in-laravel-with-attachment-without-saving-in-database\/","title":{"rendered":"How to send email in laravel with attachment without saving in database."},"content":{"rendered":"\n<p>I was trying to send an email with an attachment that will be uploaded by the user and then sent to an admin email.<\/p>\n\n\n\n<p>In this tutorial, we will discuss how to configure our Laravel Application to send emails with an attachment without saving the user data or attachment in the database and sent it directly to the Admin or defined user. <\/p>\n\n\n\n<p>We first need to configure our SMTP server ie. in our .env file or (environment file). I&#8217;m using mailtrap.io you can use any of your favorite mail servers. <br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"401\" height=\"152\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/env.png\" alt=\"Mail server configuration in your .env file\" class=\"wp-image-14882\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/env.png 401w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/env-300x114.png 300w\" sizes=\"auto, (max-width: 401px) 100vw, 401px\" \/><figcaption>Mail server configuration in your .env file<\/figcaption><\/figure>\n\n\n\n<p>as you can see the image above containing my credential for my mailtrap.io server. Make sure to change <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> MAIL_DRIVER= <em>your mail driver<\/em><\/li><li>MAIL_HOST= <em>your smtp.host<\/em><\/li><li>MAIL_PORT=<em> your mail port<\/em><\/li><li>MAIL_USERNAME=<em>your username<\/em><\/li><li>MAIL_PASSWORD=<em>your password<\/em><\/li><li>MAIL_ENCRYPTION= <em>encryption type<\/em><br><\/li><\/ul>\n\n\n\n<p class=\"has-very-dark-gray-color has-text-color\">Note:-<em>  If you want to set up it for a Gmail account <a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/www.devopsschool.com\/blog\/how-to-setup-your-gmail-account-to-send-emails-in-laravel\/\">here <\/a>is my another blog to setup mail server for Gmail Account <\/em><\/p>\n\n\n\n<p>Let&#8217;s  continue so after configuring the .env file you need to clear you config cache  for that go to you commander and run this command PHP<em><strong> artisan config:cache<\/strong><\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"390\" height=\"192\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/command-line-to-clear-config-cache-2.png\" alt=\"command to clear config cache\" class=\"wp-image-14885\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/command-line-to-clear-config-cache-2.png 390w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/command-line-to-clear-config-cache-2-300x148.png 300w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><figcaption>command to clear config cache<\/figcaption><\/figure>\n\n\n\n<p class=\"has-normal-font-size\">After this, we need a model and controller to handle the mail Function. Laravel provides a clean, simple API over the popular&nbsp;<a href=\"https:\/\/swiftmailer.symfony.com\/\" target=\"_blank\" rel=\"noopener\">SwiftMailer<\/a>&nbsp;library with drivers for SMTP, Mailgun, Postmark, Amazon SES, and&nbsp;<code>sendmail<\/code>, allowing you to quickly get started sending mail through a local or cloud-based service of your choice.<\/p>\n\n\n\n<p><a href=\"https:\/\/laravel.com\/docs\/7.x\/mail#configuration\" target=\"_blank\" rel=\"noopener\">Configuration<\/a>:-Laravel&#8217;s email services may be configured via the&nbsp;<code>mail<\/code>&nbsp;configuration file. Each mailer configured within this file may have its own options and even its own unique &#8220;transport&#8221;, allowing your application to use different email services to send certain email messages. For example, your application might use Postmark to send transactional mail while using Amazon SES to send bulk mail.<\/p>\n\n\n\n<p>Let&#8217;s make it easier, go to your command line and run this command <br><em><strong>php artisan make:mail<\/strong><\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"185\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/mail-folder-laravel.png\" alt=\"Laravel mailable folder after running command php artisan make:mail\" class=\"wp-image-14887\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/mail-folder-laravel.png 367w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/mail-folder-laravel-300x151.png 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><figcaption>Laravel mailable folder<\/figcaption><\/figure>\n\n\n\n<p>after this make a controller to process the data sent by the user and bind it into the mail in the array . But first, we need some user data to send it in the mail.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/pradeeprjth\/297beb5f1d663b1d7c3149d781423de8.js\"><\/script>\n\n\n\n<p>here is a form that acting on a route name <strong>action=&#8221;{{route(&#8216;sendemail&#8217;)}}&#8221;<\/strong><br>now go to your <em><strong>Route\/web.php <\/strong><\/em>and make a route like this.<br><strong>Route::post(&#8216;\/sendemail&#8217;,&nbsp;&#8216;SendEmailController@send&#8217;)-&gt;name(&#8216;sendemail&#8217;);<\/strong><\/p>\n\n\n\n<p><br>The route redirecting the form to the controller name <strong><em>SendEmailController@send <\/em><\/strong> where you will find a function named <em><strong><em>function<\/em>&nbsp;send(<em>Request<\/em>&nbsp;$request)<\/strong><\/em><\/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=\"575\" height=\"556\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/SendMailController-2.png\" alt=\"\" data-id=\"14890\" data-full-url=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/SendMailController-2.png\" data-link=\"https:\/\/www.devopsschool.com\/blog\/?attachment_id=14890\" class=\"wp-image-14890\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/SendMailController-2.png 575w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/SendMailController-2-300x290.png 300w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/figure><\/li><\/ul><\/figure>\n\n\n\n<p>Here you can see we are getting all the data in the <em><strong>$Request <\/strong><\/em>perimeter and validating them for error handling in client-side. <br>Then getting all the validated data and <strong><em>attachment<\/em><\/strong>  in a array named <strong><em>$data<\/em><\/strong> Then at the end we are sending the data to the mail model with whom you want to send the mail here you can use any <strong>email-id<\/strong><\/p>\n\n\n\n<p class=\"has-normal-font-size\">Here we go the final steps:- Go to your mail model and do as follow..<\/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=\"791\" height=\"619\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/mailmodel.png\" alt=\"\" data-id=\"14892\" data-full-url=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/mailmodel.png\" data-link=\"https:\/\/www.devopsschool.com\/blog\/?attachment_id=14892\" class=\"wp-image-14892\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/mailmodel.png 791w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/mailmodel-300x235.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/mailmodel-768x601.png 768w\" sizes=\"auto, (max-width: 791px) 100vw, 791px\" \/><\/figure><\/li><\/ul><\/figure>\n\n\n\n<p class=\"has-very-dark-gray-color has-text-color\">Here we are creating a function and defining the array<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\">public&nbsp;<em>function<\/em>&nbsp;__construct($data=[])&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;data&nbsp;=&nbsp;$data;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>After then the build function returning (redirecting to <strong><em>dynamic_email_template<\/em><\/strong>) with the subject and from whom the mail is sending, with attaching the file attachment <br>Now the only thing left is to make a blade file in the <strong>resources\/views<\/strong> that make a template for the Email.<br><\/p>\n\n\n\n<figure class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-3 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=\"377\" height=\"237\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/email-template-1.png\" alt=\"\" data-id=\"14894\" data-link=\"https:\/\/www.devopsschool.com\/blog\/?attachment_id=14894\" class=\"wp-image-14894\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/email-template-1.png 377w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/email-template-1-300x189.png 300w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/><\/figure><\/li><\/ul><figcaption class=\"blocks-gallery-caption\">Dynamic email Template<\/figcaption><\/figure>\n\n\n\n<p> And at last Design the template as you want <\/p>\n\n\n\n<figure class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-4 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=\"924\" height=\"211\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/email-template-2-1.png\" alt=\"\" data-id=\"14902\" data-full-url=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/email-template-2-1.png\" data-link=\"https:\/\/www.devopsschool.com\/blog\/how-to-send-email-in-laravel-with-attachment-without-saving-in-database\/email-template-2-1\/\" class=\"wp-image-14902\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/email-template-2-1.png 924w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/email-template-2-1-300x69.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/email-template-2-1-768x175.png 768w\" sizes=\"auto, (max-width: 924px) 100vw, 924px\" \/><\/figure><\/li><\/ul><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"423\" height=\"231\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/b24e0d7e6bd6b47121bf49a0750bbaa6.gif\" alt=\"\" class=\"wp-image-14897\"\/><\/figure>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_82267\"><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_82999\"  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_82267\"  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>I was trying to send an email with an attachment that will be uploaded by the user and then sent to an admin email. In this tutorial, we will discuss how to configure our Laravel Application to send emails with an attachment without saving the user data or attachment in the database and sent it&#8230;<\/p>\n","protected":false},"author":14,"featured_media":14898,"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":[6139,5651,5355,473,5203,1692],"class_list":["post-14878","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-attachment","tag-controller","tag-database","tag-email","tag-laravel","tag-model"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14878","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=14878"}],"version-history":[{"count":6,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14878\/revisions"}],"predecessor-version":[{"id":24768,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14878\/revisions\/24768"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/14898"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=14878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=14878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=14878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}