|1||Multiple mailing accounts|
|3||Send emails from multiple acc|
|4||Setting up blade file|
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 firstname.lastname@example.org and to seller email@example.com 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).
.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
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
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
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.
Also read:HELPER OR COMMON FUNCTION IN LARAVEL 9
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
So as you can see we got both the mails in my inbox .