Reputation: 21
I need send a file (pdf) from a contact form to a email.
For now I can send the email with a empty pdf file.
This is the code that I have in my controller. Thanks!
public function send(ContactFormRequest $request)
{
$data = $request->all();
Mail::send('emails.contact', $data, function ($message) use ($data) {
$message->from('[email protected]', 'Contact Form');
$message->to('[email protected]')->subject('test');
if($data['document'] != '')
{
$message->attachData($data['document'], $data['document']->getClientOriginalName());
}
});
return "Your email has been sent successfully";
}
Upvotes: 1
Views: 606
Reputation: 3488
To attach a file you can use below options :
// Attach a file from location
$message->attach($pathToFile, array $options = []);
// Attach a file from a raw $data string...
$message->attachData($data, $name, array $options = []);
Refer below link : Sending a file via form to email with Laravel (Localhost)
Upvotes: 2
Reputation: 21
Now it work properly.
I changed
$message->attachData($data['document'], $data['document']->getClientOriginalName());
by
$message->attach( $data['document']->getRealPath(), [
'as' => $data['document']->getClientOriginalName(),
'mime' => $data['document']->getMimeType()
]);
Upvotes: 1
Reputation: 495
Are you using PHPMailer?
To use PHPMailer:
Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer
Extract the archive and copy the script's folder to a convenient place in your project.
Include the main script file -- require_once('path/to/file/class.phpmailer.php');
Set the Headers:
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
Also: PHPMailer, provides this itself,
$email = new PHPMailer();
$file_to_attach = 'filepath';
$email->AddAttachment($file_to_attach , 'filename + file extension');
$email->Send();
Upvotes: -1