Jun 28, 2018
0
Level 8
Issue attaching file to mail and setting name and mime type
Hi, I'm trying to attach a file to a mail before sending it, I'm getting the file through a form upload via AJAX (I dont want to save the file to storage).
But here is my question, I don't know how to get the mime and name of the file to put them as arguments into the attachData function of the mail build method.
Here is the mail:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class AdvContactMail extends Mailable
{
use Queueable, SerializesModels;
public $name;
public $email;
public $body;
public $file;
public $subject;
public $sender_mail;
public $restaurant;
public function __construct($name, $email, $body, $file, $subject, $restaurant, $sender_mail)
{
$this->name = $name;
$this->email = $email;
$this->body = $body;
$this->file = $file;
$this->subject = $subject;
$this->restaurant = $restaurant;
$this->sender_mail = $sender_mail;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$sender_mail = config('globals.admin_mail');
return $this->markdown('emails.adv_contact')
->from($this->sender_mail, 'ReseñasMálaga')
->subject($this->subject)
->attachData($this->file, 'name.pdf', [
'mime' => 'application/pdf',
]);;
}
}
I think to get the mime and name it would be done in controller so here is my controller method:
public function contactarArchivo(ContactFileMail $request)
{
$sender_mail = $request->email;;
$destination_mail = config('globals.client_mail');
$name = $request->name;
$email = $request->email;
$body = $request->body;
$file = $request->file;
$subject = "Solicitud de contacto de $name";
$restaurant = config('globals.web_name');
Mail::to($destination_mail)->send(new AdvContactMail($name, $email, $body, $file, $subject, $restaurant, $sender_mail));
return response()->json(['success'=>'Mensaje enviado']);
}
Please or to participate in this conversation.