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 .

Contents:

  1. App: Site Notification App > Module: site-notification
  2. Service: common (module: send-notification)
  3. We need two plugin > Plugin Name: “CPT UI” and “Custom Fields”
  4. CPT: Notification Template (post_type > slug: notification_tpl)
  5. Service: notification_service (module: send)
  6. DB – Table Name: mailing_queue
  7. Table Columns: id, stamp, candidate_id, name, email, job_count, job_ids, notification_template, status

 

Step 1

Create App: Site Notifications
Slug: site-notification
(if you want to use another app, do it from Step 2.)

Step 2

Create Module: send-notifications
Module Name and Slug: send-notifications

Step 3

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.

Step 4

Search for the Common Service, If present then skip Step#5

Step 5

Go to Awesome Enterprise > Awesome Core
Open module “services”
Add the following code:

Step 6

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.

Step 7

We need a plugin to create the CPT: Plugin name: CPT UI
If not present install it.

Step 8

Find the menu “CPT UI” in the left side bar
Select menu “Add/Edit Post Types”

  1. Post Type Slug *: notification_tpl
  2. Plural Label *: Notification Templates
  3. Singular Label *: Notification Template
  4. Public: False
  5. Supports: Check the checkboxes for “Title” and “Editor”

Click on the button “Add Post Type” to save

Step 9

We need a plugin “Custom Fields”
If not present install it.

Step 10

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:

  1. Field Label: Subject
  2. Field Name: subject
  3. Field Type: Text
  4. Required?: Yes
  5. Rules: Post Type > is equal to > Notification Template

Step 11

Find the “Notification Template” in the left sidebar
Create a mail template > Click button “Add new”
Give it a title – my-first-email-template
Slug: my-first-email-template

  1. Add your mail HTML  in to the template Editor
  2. 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.

Step 12

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.

Step 13

Download Notification Service (If not present)
URL-  https://github.com/WPoets/awesome-apps/tree/main/services/notification_service

Follow import instructions:
https://github.com/WPoets/awesome-apps/blob/main/services/notification_service/setup.txt

Step 14

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”).

Step 15

Add a record in to the mailing_queue table
Insert data into the mailing_queue, Two ways to do this

  1. Create a module and write logic to add the data in the mailing queue.
  2. Insert query result into the mailing_queue table using: INSERT INTO mailing_queue SELECT * FROM…..

Example query:

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:

  1. Schedule emails to send on the specific date (We will update)
Categories
Most Popular

Leave a Reply

Your email address will not be published. Required fields are marked *