Mails
In VaahCMS there are two ways by which we can send mails.
- Send mails without Laravel Queues
- Send mails with laravel Queues
1. Send mails without Laravel Queues
If you want to send your mails immediately , then you just need to use the following
code snippet into your php
file.
VaahMail::send($mail, $to, $cc, $bcc);
$mail
should be an instance of Laravel MailIlluminate\Mail\Mailable
class.$to
is list of recipients in array format.$cc
is the list of cc recipients in array format.bcc
is list ofbcc
recipients in array format.
2. Send mails with laravel Queues
By default,
VaahCMS
does not use Laravel queues/jobs to schedule the mail. Hence, mails will be send immediately.For sending mails with laravel Queues, first you need to Setup Laravel Queues for VaahCMS.
Steps to Setup laravel Queues :
- Set
QUEUE_CONNECTION
todatabase
in your activeenv
file - Visit
Setting > General > Site Settings > Laravel Queues
in yourbackend dashboard
and enable it. - Run or setup
cron/daemon
job for
php artisan queue:work --queue=high,medium,low,default --env=env_filename
If you want to run without cache use following command:
php artisan queue:listen --queue=high,medium,low,default --env=env_filename
eg:
php artisan queue:listen --queue=low --env=.env.test
If you make any changes in code of your Job
class, then you must restart the queue:work
command.
Send a generic mail
Step 1. To send a generic mail, you just need to copy the below code snippet into your php file.
VaahMail::dispatchGenericMail($subject, $message, $to, $from_email,
$from_name, $cc, $bcc, $priority);
Following are the parameters that we need to define to send a generic mail in vaahcms.
Name | Description |
---|---|
$subject | Subject of the email |
$message | Content of the message, it can have html tags |
$to | List of recipients in array format. |
$from_email | From email, if it's not set, then it will try to get the domain from APP_URL and add noreply . Eg: noreply@<domain> |
$from_name | From name, if it's not set, the name will be taken from APP_NAME |
$cc | List of cc recipients in array format. |
$bcc | List of bcc recipients in array format. |
Step 2. When you run your code a job will be created in jobs
table in database, you will need to run the following command to run the created jobs from jobs table.
php artisan queue:listen --queue=high,medium,low,default --env=env_filename
when you run this command, the job will be removed from the jobs
table and mail will be sent.
Send Laravel mails
VaahMail::dispatch($mail, $to, $cc, $bcc, $priority);
$mail
should be an instance of Laravel MailIlluminate\Mail\Mailable
class.
Command to create mailable class in laravel
php artisan make:mail className
e.g
php artisan make:mail HelloEmail
After you run this command a class named HelloEmail
will be created in app/Mail
directory.
Then your mailable class should look like this
As you can see there is a file named
hello.blade.php
file in views directory, this file contains
the code used to define the content of the email.
Follow Below image for reference:
Now you need to create an instance of mailable
class and
use the method VaahMail::dispatch
.
Follow below image for reference:
Once you run your code a job will be created in jobs
table in database,
to run the jobs use any of the methods defined above.
$to
is the array of recipient:$priority
it is the order of execution of the jobs. You can provide following valueshigh
,medium
low
&default
Send mail to a user
$user = User::find(1);
VaahMail::dispatchToUser($mail, $user, $cc, $bcc, $priority);
$user
is an instance ofWebReinvent\VaahCms\Entities\User
Send a mail to Super Administrators
User::notifySuperAdmins($subject, $message);
Customizing The Templates
To customize the default Laravel mail & notification template you must publish the asset, using following commands:
php artisan vendor:publish --tag=laravel-mail
php artisan vendor:publish --tag=laravel-notifications
This will publish files under resources/views/vendor/mail
directory. To update the UI of the VaahCMS Mails
, you can edit following file:
resources/views/vendor/mail/html/header.blade.php
resources/views/vendor/mail/html/footer.blade.php
resources/views/vendor/mail/html/themes/default.css