Documentation / Tutorials / Generic
How to send the bulk mail
/ Generic / How to send the bulk mail
This tutorial helps you to send bulk email and notification to the many profiles at a time .
- App: Site Notification App > Module: site-notification
- Service: common (module: send-notification)
- We need two plugin > Plugin Name: “CPT UI” and “Custom Fields”
- CPT: Notification Template (post_type > slug: notification_tpl)
- Service: notification_service (module: send)
- DB – Table Name: mailing_queue
- Table Columns: id, stamp, candidate_id, name, email, job_count, job_ids, notification_template, status
Create App: Site Notifications
(if you want to use another app, do it from Step 2.)
Create Module: send-notifications
Module Name and Slug: send-notifications
Create a template in the module (“send-notifications”) and add the following code.
This code will fetch the email records from the database and set the values in the “args” variable. This values will be accessible in the HTML template using the variable “notify_data”. This code will call the template using the “notification_template” value (Step#12).
Email Template slug should be same as template name that we will create in the Step#12.
Search for the Common Service, If present then skip Step#5
Go to Awesome Enterprise > Awesome Core
Open module “services”
Add the following code:
Create a module “send-notifications” in common service
Module Title and Slug: “send-notifications”
Add following code in this module.
This code will fetch the content from the post with post_type “notification_template” and willl use as the mail body. This code will pass all the values in the “notify_data” to the mail body.
We need a plugin to create the CPT: Plugin name: CPT UI
If not present install it.
Find the menu “CPT UI” in the left side bar
Select menu “Add/Edit Post Types”
- Post Type Slug *: notification_tpl
- Plural Label *: Notification Templates
- Singular Label *: Notification Template
- Public: False
- Supports: Check the checkboxes for “Title” and “Editor”
Click on the button “Add Post Type” to save
We need a plugin “Custom Fields”
If not present install it.
Find the menu “Custom Fields” in the left side bar
Select menu “Field Group”
Click button “Add New”
Click “Add Field” button
Fill the following information:
- Field Label: Subject
- Field Name: subject
- Field Type: Text
- Required?: Yes
- Rules: Post Type > is equal to > Notification Template
Find the “Notification Template” in the left sidebar
Create a mail template > Click button “Add new”
Give it a title – my-first-email-template
- Add your mail HTML in to the template Editor
- Add the subject…
We can add dynamic variables in editor and also with subject line. This is the actual HTML email template that we have to send as mail body. Mail body can contain the dynamic values that are accessible in the variable notify_data.
Open the module created in the Step#3 “send-notifications”
Create a new template with the same slug Step#11 (email template slug)
This template will be executed when this type of “notification_template” will get run. (Refer comment in the Step#3 //** Dynamic template calling **//). This template name is same as the database column “notification_template” value.
Download Notification Service (If not present)
Follow import instructions:
Create a table mailing_queue
Insert mail ids in this table along with mailing template and status (pending). This table can hold (makes mailing queue) the primarily name, email id and which template to be send. “status” column have 2 possible values
1. pending: mail to be send.
2. success: once mail has been sent, the status changes to “success”. (mail will get sent to the mail ids with pending status only)
So insert the record with status “pending” in this table and once you run the module; query will fetch all the records with status=pending and send the mail to the mail ids with the mail body (post has slug “notification_template”).
Add a record in to the mailing_queue table
Insert data into the mailing_queue, Two ways to do this
- Create a module and write logic to add the data in the mailing queue.
- Insert query result into the mailing_queue table using: INSERT INTO mailing_queue SELECT * FROM…..
Table to table insertion from DB (All active profiles)
After done! all above things you need to run cron.
Hit the following url: /site-notification/send-notifications/
(If you used your own app, then your url will be /your-app/send-notification/)
Cron base query: (set it for every 30 mins)
SELECT * FROM mailing_queue WHERE status = ‘pending’ LIMIT 200
Improvement in mail notification:
- Schedule emails to send on the specific date (We will update)