louischappell's avatar

Sending an image with Mail, does it need to be stored first?

I have an enquiry form, a user would add their email address, telephone comment and a photo.

The file is stored as part of an array $request->file('photo'); If I dd the image in the controller, I can see the file is present. When I send it (mailtrap) I see the temporary local filepath rather than the image.

In similar situations, images can be uploaded, then sent on, I don't have a problem with that, but it is unnecessary.

Do I need to store the image before I can send it?

Thanks

0 likes
2 replies
xiarnousx's avatar
Level 3

I think you can have the image as an attachment.

$data = file_get_contents($request->file('photo'));
$message->from($address, $name = null);
$message->sender($address, $name = null);
$message->to($address, $name = null);
$message->cc($address, $name = null);
$message->bcc($address, $name = null);
$message->replyTo($address, $name = null);
$message->subject($subject);
$message->priority($level);
$message->attach($pathToFile, array $options = []);

// Attach a file from a raw $data string...
$message->attachData($data, $name, array $options = []);

// Get the underlying SwiftMailer message instance...
$message->getSwiftMessage();

Ref: https://laravel.com/docs/5.1/mail

louischappell's avatar

As xiarnousx mentioned, images should be attached, I made another mistake;

<img src="{{ $message->embed($image->getRealPath()) }}">

Is the correct way to display the message, I had:

<img src="{{ $message->embedData($image, $image->getClientOriginalName()) }}">
1 like

Please or to participate in this conversation.