MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to send email in laravel with attachment without saving in database.

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 directly to the Admin or defined user.

We first need to configure our SMTP server ie. in our .env file or (environment file). I’m using mailtrap.io you can use any of your favorite mail servers.

Mail server configuration in your .env file
Mail server configuration in your .env file

as you can see the image above containing my credential for my mailtrap.io server. Make sure to change

  • MAIL_DRIVER= your mail driver
  • MAIL_HOST= your smtp.host
  • MAIL_PORT= your mail port
  • MAIL_USERNAME=your username
  • MAIL_PASSWORD=your password
  • MAIL_ENCRYPTION= encryption type

Note:- If you want to set up it for a Gmail account here is my another blog to setup mail server for Gmail Account

Let’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 artisan config:cache

command to clear config cache
command to clear config cache

After this, we need a model and controller to handle the mail Function. Laravel provides a clean, simple API over the popular SwiftMailer library with drivers for SMTP, Mailgun, Postmark, Amazon SES, and sendmail, allowing you to quickly get started sending mail through a local or cloud-based service of your choice.

Configuration:-Laravel’s email services may be configured via the mail configuration file. Each mailer configured within this file may have its own options and even its own unique “transport”, 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.

Let’s make it easier, go to your command line and run this command
php artisan make:mail

Laravel mailable folder after running command php artisan make:mail
Laravel mailable folder

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.

here is a form that acting on a route name action=”{{route(‘sendemail’)}}”
now go to your Route/web.php and make a route like this.
Route::post(‘/sendemail’, ‘SendEmailController@send’)->name(‘sendemail’);


The route redirecting the form to the controller name SendEmailController@send where you will find a function named function send(Request $request)

Here you can see we are getting all the data in the $Request perimeter and validating them for error handling in client-side.
Then getting all the validated data and attachment in a array named $data 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 email-id

Here we go the final steps:- Go to your mail model and do as follow..

Here we are creating a function and defining the array

public function __construct($data=[])    {        $this->data = $data;    }

After then the build function returning (redirecting to dynamic_email_template) with the subject and from whom the mail is sending, with attaching the file attachment
Now the only thing left is to make a blade file in the resources/views that make a template for the Email.

And at last Design the template as you want

Game show host GIF saying That's it

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals

Related Posts

PHP Code for Godaddy VPS Server SMTP Email Relay

This page shows how to send email from a GoDaddy VPS through GoDaddy's SMTP relay on port 25 with plain PHP sockets and no extra libraries. Below…

Read More

Email Authentication & Communication Infrastructure in Modern DevOps: The 2026 Definitive Guide

In the high-stakes world of modern DevOps, we spend our days (and sometimes our nights) obsessing over the “golden signals” of site reliability: latency, traffic, errors, and…

Read More

The 5 Most Popular Email APIs Among Developers In 2026

In the modern world, where everything is going digital, email is among the most important means of communication both in personal and business life. As a developer,…

Read More

Senior Exchange Administrator: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path

The Senior Exchange Administrator is accountable for the reliability, security, performance, and lifecycle management of the organization’s Microsoft messaging platform, typically spanning Exchange Online (Microsoft 365), Exchange Server (on-premises), or a hybrid configuration. This role ensures email and calendaring services are highly available, compliant, and aligned to business needs while proactively reducing operational risk and improving user experience.

Read More

Principal Exchange Administrator: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path

The Principal Exchange Administrator is the senior technical authority responsible for the reliability, security, lifecycle, and operational excellence of the enterprise messaging platform—typically Microsoft Exchange Online/Exchange Server in a hybrid Microsoft 365 environment. This role exists to ensure email and calendaring services remain highly available, performant, compliant, and aligned with enterprise security and governance requirements while enabling modern collaboration capabilities across the organization.

Read More

Lead Exchange Administrator: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path

The Lead Exchange Administrator owns the reliability, security, and operational excellence of the organization’s messaging platform, typically Microsoft Exchange Online (Microsoft 365) and/or a hybrid Exchange deployment with on‑premises Exchange servers. This role ensures email and calendaring services remain highly available, performant, compliant, and resilient, while continuously improving automation, monitoring, and service management maturity.

Read More