PDF attachment not showing in mailtrap HTML output but showing in RAW section - Laravel 5.7
Asked Answered
F

1

9

I find out that the Pdf I attached to my email does not show when delivered to my email in Mailtrap but it shows in the RAW data section.

My Controller Method to send an email:

public function store(Request $request)
{
    $this->validate($request, [
      'name' => 'required',
      'email' => 'required|email',
      'quantity' => 'required|numeric|digits:1',
    ]);

    if ($request->quantity > 5) {
        toastr()->error('Only 5 tickets per booking is allowed');
        return redirect()->back();
    }

    $a = 0;
    $file = array();
    while ($a < $request->quantity) {

        $ordercode = substr(md5(time() . mt_rand(1, 1000000)), 0, 22);
        $qrcodepath = 'assets/payments/qrcodes/'.str_slug($request->name).time().'.png';
        $qrcode = QrCode::format('png')->size(400)->generate($ordercode, $qrcodepath);

        $data["name"] = $request->name;
        $data["email"] = $request->email;
        $data["qrcode"] = $qrcodepath;
        $data["ordercode"] = $ordercode;

        $pdf = PDF::loadView('pdf.payment', $data);
        $pdfpath = 'assets/payments/pdf/'.str_slug($request->name).time().'.pdf';
        $pdf->save($pdfpath);

        $payment = Payment::create([
          'fullname' => $request->name,
          'qrcode' => $qrcodepath,
          'ordercode' => $ordercode,
          'email' => $request->email,
          'quantity' => 1,
          'pdfticket' => $pdfpath
        ]);

        $file[] = $payment->pdfticket;
        $a++;
        sleep(1);
    }

    $data = array(
        'name' => $payment->fullname,
        'tickets' => $file,
    );

    Mail::to($payment->email)->send(new PurchaseComplete($data));

    dd($payment->email);

    toastr()->success('Payment created successfully');

    return redirect()->back();
}

My Mailable:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class PurchaseComplete extends Mailable
{
    use Queueable, SerializesModels;

    public $data;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $message = $this->subject('Ticket Purchase - CodeRed')->markdown('emails.purchase.complete')->with('data', $this->data);

        $count = 0;
        foreach ($this->data['tickets'] as $ticket) {
            $count++;
            $message->attach(asset($ticket), array(
                'as' => 'ticket'.$count.'.pdf',
                'mime' => 'application/pdf',
            ));
        }

        return $message;
    }
}

My Email Markdown:

@component('mail::message')
# Hello {{ $data['name'] }}

Your Ticket Purchase: Please find attached files below.

Ensure you download the ticket and load it on your phone when you arrive!

You can also Login to your profile to download the Qrcode for a quick scan and copies of your tickets!

@component('mail::button', ['url' => route('login')])
Login
@endcomponent

If you have any further questions: contact us through the contact section on our website.

Thanks,<br>
{{ config('app.name') }}

@endcomponent

Mailtraps HTML output of the mail:

No PDF attached to the email

But in the RAW section: it shows that a pdf was attached:

PDF shows that it was attached

I have searched the web and implemented practically every solution I found to try solving this problem but no success. I have tried using public_path(), storage_path(), attachData() and more! I just don't know how to proceed and make this work.

Fubsy answered 2/2, 2020 at 2:10 Comment(2)
What shows when you click show info in mail trap?Metallic
Thanks for your response! It was my ignorance and I didn't know Mailtrap does not display attachments the same way mail service providers like Gmail, Yahoo and Yandex do -- all the time it was in my face at the top right section of the mail...Fubsy
F
15

My bad for this: I didn't know Mailtrap displays attachments differently from other mail service providers...

enter image description here

Fubsy answered 2/2, 2020 at 3:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.