How to attach image from storage in mail Laravel

laravel smtp

2012 观看

2回复

161 作者的声誉

I am trying to attach an image stored in [storage/app/proof] onto an email however, I am getting...

mail_attach

instead of the image file and this is what I've done so far.

Controller

        $user->accType = "TBC";
        //stored as "proof/img_name.jpg" in DB
        $user->proofLink = request()->file('proofFile')->store('proof'); 
        $user->save();
        \Mail::to('ehrs.office@gmail.com')->send(new ConfirmAccount($user));

ConfirmAccount.php enter image description here

What am I doing wrong?

作者: SpencerChantler123 的来源 发布者: 2017 年 12 月 27 日

回应 2


0

7599 作者的声誉

Hi you are missing the attachment mime type..

                attach('/path/to/file', [
                    'as' => 'name.pdf',
                    'mime' => 'application/pdf',
                ]);

you can find more on https://laravel.com/docs/5.5/mail#attachments

Hope this helps.

作者: Adnan Mumtaz 发布者: 2017 年 12 月 27 日

1

161 作者的声誉

The error was actually caused by an incomplete path.

$location = storage_path("app/$user->proofLink");

was returning "app/" instead of "app/records/img_name.jpg".

To solve this, I edited ConfirmAccount.php as follows:

 public function __construct(User $user)
{
    $this->URL = $user->proofLink;
}
  public function build()
{
    $location = storage_path("app/$this->URL");
    return $this->view('emails.confirmaccount')->attach($location);
}
作者: SpencerChantler123 发布者: 2017 年 12 月 27 日
32x32