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 Get Youtube Video Thumbnail From URL In PHP - Laravel
- Laravel 9 Ajax Image Upload With Preview Tutorial
- How To Configuration Laravel Supervisor
- [Fixed] Binary Does Not Exist Laravel
- Laravel 9 Dropzone Multiple File Upload Example
- How to Use Google Translate Multi Language In Laravel
- map() Method | Laravel Collection Example
- Laravel 10 Arr::pluck() Helper Function Use Example
- How to Integrate PayUMoney Payment Gateway in Laravel 9
- How To Add After Validation Hook In Laravel Example