0

I'm trying to send an email through Laravel from a form that has two different file inputs with different names, cv and cover_letter.

The build function works just fine but it only attaches one file when it sends the email, I'd like it to send both.

I've tried this solution here on SO but no luck sending both attachments either.

    public function build(Request $request)
    {
       $today = Carbon::now()->format('Y-m-d');

        return $this->from([
            'email' => $request->email, 
            'name' => $request->name 
        ])
        ->to( 'jobs@domain.com' )
        ->subject( New job application '.$request->name.' for the position of '.$request->role.'.')
        ->view('emails.jobsform')
        ->with([    
            'name' => $request->name,
            'tel' => $request->tel,
            'email' => $request->email,
            'role' => $request->role,
            'location' => $request->location,
            'call_code' => $request->call_code,

        ])
        ->attach(
            $request->cv, [
            'as' => $today.".".$request->name.".".$request->role.'.pdf', 
            'mime' => 'application/pdf'],
            $request->cover_letter, [
                'as' => $today.".".$request->name.".".$request->role.'-Cover Letter.pdf', 
                'mime' => 'application/pdf']
            );

    }

Hope someone can help, thanks.

MegaBeetle
  • 15
  • 4

1 Answers1

0

In example, he use attach with foreach(), it means that for new file he has new attach, and u want to attach all files in one attach(). Try this

->attach(
            $request->cv, [
            'as' => $today.".".$request->name.".".$request->role.'.pdf', 
            'mime' => 'application/pdf'])
->attach(
            $request->cover_letter, [
                'as' => $today.".".$request->name.".".$request->role.'-Cover Letter.pdf', 
                'mime' => 'application/pdf']
            );
Bohdan Petrenko
  • 340
  • 1
  • 7
  • Hello Bohdan, this didn't work, thanks for your input though – MegaBeetle Mar 24 '20 at 11:26
  • Hi MegaBeetle, first argument in `attach()` must be a file path, not file name, are u sure that `$request->cover_letter` and `$request->cv` has file path? – Bohdan Petrenko Mar 24 '20 at 15:35
  • Hi, Bohdan, yeah, they do, if i send just one ```->attach( $request->cover_letter, [ 'as' => $today.".".$request->name.".".$request->role.'-Cover Letter.pdf', 'mime' => 'application/pdf'] );```it works just fine, problem is that together both attachments don't send – MegaBeetle Mar 25 '20 at 14:43
  • try to make working attachment twice `->attach( $request->cover_letter, [ 'as' => $today.".".$request->name.".".$request->role.'-Cover Letter.pdf', 'mime' => 'application/pdf'] )->attach( $request->cover_letter, [ 'as' => $today.".".$request->name.".".$request->role.'-Cover Letter.pdf', 'mime' => 'application/pdf'] );` something like this, it must work – Bohdan Petrenko Mar 25 '20 at 14:51
  • I did as you suggested @BohdanPetrenko but it only sent one attachment in the email. – MegaBeetle Mar 25 '20 at 16:37