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/