How to send SMTP mail with Attachments in Laravel?

Firstly get the extension of the file to send and get the MIME TYPE for it.

$data['filename'] = Auth::User()->file;

$extension = explode('.',Auth::User()->file)[1];

if ($extension == 'doc') {

	$data['mine_type'] = 'application/msword';

}
elseif ($extension == 'docx') {

	$data['mine_type'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';

}
elseif ($extension == 'pdf') {

	$data['mine_type'] = 'application/pdf';
	
}

Use the mail function and pass the required data needed by the layout and SMTP connection protocol.

Mail::send('emails_layout',$data, function ($message) use ($data) {

	$message->from('noreply@maxester.com', 'MaXEster');

	$message->to($data['email'])->subject('Find the attachment');

	$message->attach(
		$data['pathToFile'], array(
	    'as' => $data['filename'], 
	    'mime' => $data['mine_type'])
		);

});

Leave a Reply

Your email address will not be published. Required fields are marked *