How send mail in laravel with file attachment

Im trying to send attachment on laravel but i couldnt figure out how do that.
im completely new to laravel, able to receive mail without attachment but could go further for file attachment steps, need to be guide further by mentors.

<form action="{{ route('careers.store') }}" class="contact-form" method="POST" enctype="multipart/form-data">
     <div class="md-form">
          <label>Attach Resume</label>
           <input id="dropzone-file" type="file" name="file" class="form-control">
        </div>
</form>

Controllers

public function sendCareerMessage(Request $request)
    {
        $request->validate([
                'name' => 'required|max:190',
                'address' => 'required|max:190',
                'email' => 'nullable|required_without:phone|email||max:190',
                'phone' => 'nullable|required_without:email|max:190',
                'message' => 'required',
                'recaptcha_action' => 'required',
                'recaptcha_token' => ['required', new Recaptcha]
            ]);

        $data = $request->only('name', 'phone', 'email', 'message', 'address');

        try {
            Mail::to(config('mail.to'))->send(new CareerMail($data));
        } catch (Exception $e) {
            $msg = config('app.debug') ? $e->getMessage() : "Oops! Could not send your message currently. Please try again later.";

            return response()->json([
                    'response_status' => 'error',
                    'msg' => $msg
                ]);
        }

        return response()->json(['response_status' => 'success']);
    }
    

app/Mails/CareerMail.php

class CareerMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public $data;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $data = $this->data;
        $subject = "You've a new message from a visitor to your website ".env('APP_DOMAIN');
        return $this->subject($subject)
            ->markdown('emails/career_message', $data);
    }
}

resources/views/emails/career_message.blade.php

<div style="text-align:center">
	<p class="text-right">Date: {{ date('m/d/Y') }}</p>
	<h2>Hello there,</h2>
	<p class="color-danger">You have receieved a new message from a visitor to your website<a href="{{ route('home') }}" target="_blank"><b>{{ env('APP_DOMAIN') }}</b></a>.</p>
	<h3>Message: </h3>
	<p>{!! nl2br($message) !!}</p>
	<h3>Sender Details:</h3>
	<p>
		<ul>
			<li><strong>Name: </strong> {{ $name }}</li>
			<li><strong>Address: </strong> {{ $address }}</li>
			<li><strong>Email: </strong> <a href="mailto:{{ $email }}" target="_blank">{{ $email }}</a></li>
			<li><strong>Phone: </strong> <a href="tel:{{ $phone }}">{{ $phone }}</a></li>
		
		</ul>
	</p>
</div>
<div>
	<br><br>
	<p class="regard" style="margin-bottom:5px;"><strong>Thank You, </strong></p>
	<p style="margin-bottom:0;"><a href="{{ route('home') }}">{{ config('app.name') }}</a></p>
</div>

When you make the mailable, in the build method you have there, you can add on the attach method. Example below…

return $this->subject($subject)
            ->markdown('emails/career_message', $data)->attach($this->data->getRealPath(),
                [
                    'as' =>  $this->data->getClientOriginalName(),
                    'mime' =>  $this->data->getClientMimeType(),
                ]
            );

Now the as and mime parts are optional, but you can focus on passing the filename of the data and use the attach method to then attach it.

Hopefully that makes sense.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.