How to Send Email from Multiple Mailing accounts in Laravel 10

How to Send Email from Multiple Mailing accounts in Laravel 10

14-Sep-2024
| |
Image Carousel

Table of Contents

S.no Contents-table
1 Multiple mailing accounts
2 Basic configuration
3 Send emails from multiple acc
4 Setting up blade file

1:Multiple mailing accounts in laravel

Laravel provides us very useful feature to use multiple mailing accounts in a single application to use different mailing accounts at the same time , like sometimes you have different departments in your company or any other business  for eg - you are having an e-commerce with customer panel , seller panel  and you want send email like to customer [email protected] and to seller [email protected] as both are using smtp mail service and every departments should get email according to their prespective , so here we using different mail addresses with same mail driver(SMTP driver for mails).

2:Basic configuration

.ENV: First open your .env file in root director and add your credentials like MAIL_MAILER , MAIL_HOST , MAIL_PORT , MAIL_USERNAME , MAIL_PASSWORD , MAIL_ENCRYPTION , MAIL_FROM_ADDRESS , MAIL_FROM_NAME and CUSTOMER_CARE_USERNAME , CUSTOMER_CARE_PASSWORD here we are using smtp mail driver with both the mails MAIL_USERNAME and CUSTOMER_CARE_USERNAME that's why we only write customer care username and password , as we can use mail mailer , host and other configuration from smtp mail driver , if you want to use other mail driver you can change it here.
code for .env  
Copy

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=UserName
MAIL_PASSWORD=Password
MAIL_ENCRYPTION=SSL
MAIL_FROM_ADDRESS=UserName
MAIL_FROM_NAME="Developer Corner"
 
CUSTOMER_CARE_USERNAME=UserName
CUSTOMER_CARE_PASSWORD=Password

config: Here we add different mail credentials using gmail smtp .Go to config/mail.php  and paste the following lines to add another driver as we are using same mail driver accept username and password so we only import username and password for customer care mail and use same driver as on username.
code for config file:
 Copy

 'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.gmail.com'),
            'port' => env('MAIL_PORT', 465),
            'encryption' => env('MAIL_ENCRYPTION', 'SSL'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'auth_mode' => null,
        ],
        'customer_care' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.gmail.com'),
            'port' => env('MAIL_PORT', 465),
            'encryption' => env('MAIL_ENCRYPTION', 'SSL'),
            'username' => env('CUSTOMER_CARE_USERNAME'),
            'password' => env('CUSTOMER_CARE_PASSWORD'),
            'timeout' => null,
            'auth_mode' => null,
        ],

3:Sending emails from multiple accounts

For sending emails we are using simple functions in controller as you can see sendMailUser() and SendMailCustomer() are the functions which we used as in sendMailUser()  we are not providng any from attribute so it takes the bydefault username and password  but in SendMailCustomer() we are providing from attribute $user['from']=env('CUSTOMER_CARE_USERNAME'); But if you are using other credentials like domain.com then please get the email configuration of that particular domain.like if i am usng email from hostinger then smtp should be smtp.hostinger.com . 

Code for controller: Copy 

use Mail;
  public function SendMailUser()
  {
    $data  = ['name' => 'Devloper'];
    $user['to'] = '[email protected]';
    Mail::send('welcomeMail', $data, function ($messages) use ($user) {
      $messages->to($user['to']);
      $messages->subject('Single Driver Mail');
    });
  }
 
  public function SendMailCustomer()
  {
 
    $data  = ['name' => 'Devloper'];
    $user['to'] = '[email protected]';
    $user['from'] = env('CUSTOMER_CARE_USERNAME');
    Mail::mailer('customer_care')->send('customerMail', $data, function ($messages) use ($user) {
      $messages->from($user['from']);
      $messages->to($user['to']);
      $messages->subject('Multiple Driver Mail');
    });
  }

Here  in SendMailCustomer   function we are defining our mail driver customer_care and adding from paarmeter as by-default laravel uses smtp driver so in SendMailUser we don't need to define the driver.

4:Setting up blade file

Now create two views in resources/views  1-welcomeMail  2-customerMail which is used to send data in mails so here we are using only name variables in view file to display  you can use other data like billing invoice and other mail data accordingly.

code for view file: Copy

<h4>Hello {{$name}}</h4>
<h5>This is Multi-Mail Driver as Customer care mail</h5>

So as you can see we got both the mails in my inbox .

 

Tags: how to send email from multiple mailing accounts in laravel 10 , how to send email from multiple account using laravel 10 , how to add multiple mail account in laravel 10,laravel , php ,laravel-php , mvc laravel, advance laravel , bugs in laravel , laravel advance level,
0 Comments (Please let us know your query)
Leave Comment
Leave Comment
Articles from other Categories
Load More

Newsletter