How to get Laravel Mail response after send function called?
Asked Answered
C

2

5

Is it possible to get Mail response in Laravel after Mail:to a method called? Where is the best place to do that?

Mail::to($order->email)
  ->send(new ThankYouMail($order->fresh())); 

I am using Sendgrid as a Laravel Mail driver and want to get messageID in order to use for afterward Sendgrid hooks (get email delivery status etc).

public function build()
{
    $from = '[email protected]';
    $subject = 'Thank You for Ordering';
    $name = 'Name XYZ';

    $order_id = (string)$this->order->id;
    $headerData = [
      'category' => 'Order',
      'unique_args' => [
          'OrderID' => $order_id
      ]
    ];

    $header = $this->asString($headerData);

    $this->withSwiftMessage(function ($message) use ($header) {
      $message->getHeaders()
              ->addTextHeader('X-SMTPAPI', $header);
    });

    return $this->view('mails.thank-you')
                ->from($from, $name)
                ->replyTo($from, $name)
                ->subject($subject);
}

UPDATE: 2019/06/25

If anyone wants to get and store Mail statuses it is the best solution to use Sendgrid Event Webhook.

  • Probably you will attach (to the email) an unique arg like OrderID during the email send process (Check the Sendgrid API or my example from above).
  • After that you have to create API POST route in order to receive/store Webhook Email data where you can filter/aim and connect status data.

UPDATE #2: 2021/01/21

In order to receive data from Sendgrid it is required to create some logic:

Route:

Route::post('sendgrid/events','SendgridOrderEventController@store');

Controller:

/**
* Store a sendgrid event in database.
* @param  App\Http\Requests\API\CreateSendgridOrderEventRequest $request
* @return Response
*/
public function store(CreateSendgridOrderEventRequest $request)
{
  $sendgrid_request = $request->all()[0];

  if($sendgrid_request['OrderID']) {
    
    $sendgrid_request['order_id'] = $sendgrid_request['OrderID'];
    $sendgrid_request['sendgrid_timestamp'] = $sendgrid_request['timestamp'];
    $sendgrid_request['sendgrid_message_id'] = $sendgrid_request['sg_message_id'];

    $sendgridOrderEvent = SendgridOrderEvent::create($sendgrid_request);
  }

  return $this->sendResponse($sendgridOrderEvent, 'SendGrid Order Event created successfully');
}

Within the Sendgrid dashboard, find webhooks settings and place your API POST endpoint: api.yourdomain.com/sendgrid/events so after that, Sendgrid will be able to generate events and send a POST request to your API endpoint where your logic can receive and handle incoming data.

More details: https://sendgrid.com/docs/for-developers/tracking-events/event/

Courtship answered 8/4, 2019 at 12:55 Comment(3)
Hey @Courtship Can you share with us how you used the SendGrid Event Webook to get more info about the email sent? I am not finding examples using this with Laravel/PHP. ThanksKana
Hey @Kana check for the update from my question ;)Courtship
Just a heads up to devs out there that Laravel 9 now uses withSymfonyMessage instead of withSwiftMessage. laravel.com/docs/9.x/upgradeSodomite
A
9

An Illuminate\Mail\Events\MessageSent is dispatched after the mail is sent [1]

This event is dispatched with the instance of the swift message instance. [2]

You can listen on this event and get the message id.

This event can be subscribed to by registering in the boot method of the EventServiceProvider an event listener. [3]

protected $listen = [
    'Illuminate\Mail\Events\MessageSent' => [
        'App\Handlers\Events\MessageIdTranscript',
    ],
];

The MessageIdTranscript is a plain PHP class with a handle method that is called with the event. Instances of Swift_Message expose the message Id via a public getter method named getId.

namespace App\Handlers\Events;

use Illuminate\Mail\Events\MessageSent;

class MessageIdTranscript {

    /**
     * Handle the event.
     *
     * @param  MessageSent  $event
     * @return void
     */
    public function handle(MessageSent $event)
    {
        $messageId = $event->message->getId();
        // continue handling...
    }

}
Azerbaijan answered 8/4, 2019 at 15:1 Comment(1)
How do I get the MailJet response in Laravel 9. Can you help me?Paschall
A
1

Attach a listener to the sendgrid api response not the build.

Asseveration answered 8/4, 2019 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.