Mailable get a message-id after mail successfully send in laravel using the withSwiftMessage method used in the build method, may sometimes developer need a mail message id after a mail is successfully sent to the users for reference purpose.
I stored whole details of the mail sent in the database email table, for knowing the mail sent status and uses notification purpose, after a lot's research I got a solution using withSwiftMessage we will get a reference message id, and this reference id I will store in the email table in the database.
Refer to the below example of how I am getting after mail sending a message id in mailable and then after storing it in the database. apart from the mail message id, I am storing many more details like email, customer_id, email IP, HTML template dynamics that I am sending to the users, etc, etc...
Example
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQu eue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;
public $data;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($input)
{
$this->data = $input;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('mail.welcome')
->subject('Welcome to Webappfix - Verify Email ID.')
->with('data',$this->data)
->withSwiftMessage(function ($swiftmessage){
\App\Models\Email::create([
'email_date' => now(),
'email_ip' => request()->ip(),
'email_for' => 0,
'customer_id' => 1,
'customer_email' => $this->data['email'],
'ref_id' => $swiftmessage->getId(), //MESSAGE ID
'email_html' => view('mail.welcome',['data' => $this->data])->render()
]);
});
}
}
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- How To Connect PostgreSQL With Laravel Application
- Laravel 10 Http Guzzle Request Crud Example
- Error : Trying To Get Property Of ID Non-Object All Possible Solution Guideline In Laravel - PHP
- Laravel 9 Authorize.Net Payment Gateway Integration
- Laravel 6 REST API with Passport Tutorial
- Laravel JSON Data Search/Filter From Database Example
- How to Send Email with Laravel 10
- How To Clone Laravel
- Laravel Pagination Not Working While Using Map Method [solved]
- How to return laravel collection in json format - toJson() Method - Laravel Collection