How to send the bulk mail
This tutorial helps you to send bulk email and notification to the many profiles at a time .
Contents:
- 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
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[templates.add main] [query.get_results set='template.mail_que_result'] SELECT * FROM mailing_queue WHERE status = 'pending' LIMIT 10 //** Change the limit as per your requirement **// [/query.get_results] [php.site_url set='module.site_url' /] [loop.@mailRow template.mail_que_result] [template.set args='' /] [template.set args.activity="email" /] [template.set args.name='{@mailRow.item.name}' /] [template.set args.email='{@mailRow.item.email}' /] [template.set args.mail_template='{@mailRow.item.notification_template}' /] //** Dynamic template calling **// [templates.run main='{@mailRow.item.notification_template}' args='{template.args}' candidate='{@mailRow.item}' /] [query.query] UPDATE mailing_queue SET status='success' where ID = '[aw2.get @mailRow.item.ID /]' [/query.query] [/loop.@mailRow] [/templates.add] |
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:
1 |
[services.add common service_label='Common Service' post_type='common_service' desc='Common Service' /] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
[templates.add main] //**Setup notify_data and send notification**// [do.notify_data] //**We merge request data with notify_data always**// [if.not_empty module.args] [module.args merge_with="notify_data" /] [/if.not_empty] [if.else] [request.get merge_with="notify_data" /] [/if.else] //**We set object_type=lead & object_id={email} as default**// [str.create lead o.set="notify_data.obj.type" /] [env.set notify_data.obj.id="{env.notify_data.email}" /] [module.set template_post_type='notification_tpl' /] //****Get the field values from mail template post****// [query.get_post post_slug='{notify_data.mail_template}' post_type='{module.template_post_type}' o.set=module.mail_post /] [if.empty module.mail_post.ID] [env.set_array notify_data.ack status="error" reason="Mail Template Not Found" /] [template.return notify_data.ack /] [/if.empty] //**Run the validations and return if error**// [code.run module.mail_post.meta.validations /] [template.return notify_data.ack c.cond='notify_data.ack.status' c.eq='s:error' /] //** set up the subject, message & sms**// [code.run module.mail_post.meta.subject c.empty='notify_data.subject' o.set='notify_data.subject' /] [code.run module.mail_post.parse_content c.empty='notify_data.message' o.set='notify_data.message' /] [code.run module.mail_post.meta.attachment c.empty='notify_data.attachments' o.set='notify_data.attachments' /] //**if activity contains 'email', send email**// [templates.send_email c.contains='s:email' c.haystack='notify_data.activity' o.set="notify_data.ack" /] //**Return ack**// [template.return notify_data.ack /] [/do.notify_data] [/templates.add] //**Send Email**// [templates.add send_email] //**if activity contains 'email', we setup email_data**// [if.contains needle='email' haystack='{notify_data.activity}'] //**If no notify_data.email and notify_data.lapp.personal_email found, return with error**// [env.set_array notify_data.ack status="error" reason="Email not found" int_reason="Email not found" /] [template.return notify_data.ack c.empty="notify_data.email" and.empty="notify_data.lead.email" /] //**Setup to_email**// [env.set notify_data.to_email="{notify_data.email}" /] [env.set notify_data.to_email="{notify_data.lead.email}" c.empty="notify_data.email" /] [/if.contains] //**Setup email variables**// [template.set_array email_data] [from name='{env.settings.from_email_name}' email_id='{env.settings.from_email}' /] [to email_id='{notify_data.to_email}' /] [atts subject='{notify_data.subject}' message='{notify_data.message}' /] [attachments] [file new][aw2.get notify_data.attachments /][/file] [/attachments] [/template.set_array] //**Send Email**// [notification_service.send email="{template.email_data}" template_slug="{notify_data.mail_template}" notification_object_type="{notify_data.obj.type}" notification_object_id="{notify_data.obj.id}" o.set="template.ack" /] [template.return template.ack /] [/templates.add] |
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”
- 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
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:
- Field Label: Subject
- Field Name: subject
- Field Type: Text
- Required?: Yes
- 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
- 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.
1 2 3 4 |
//** You will get the values in the notify_data [aw2.get notify_data.name /] [aw2.get notify_data.any_variable_1 /] Check the Step#12 **// |
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.
1 2 3 4 5 6 7 |
[template.add my-first-email-template] //** If you want to pass any custom value write like this [template.set args.any_variable_1="Custom Value1" /] [template.set args.any_variable_2="Custom Value 2" /] **// [common.send-notifications args={template.args} o.set=template.email_res /] [/template.add] |
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”).
1 2 3 4 5 6 7 8 9 10 11 |
CREATE TABLE `mailing_queue` ( `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `stamp` datetime NOT NULL DEFAULT current_timestamp(), `custom_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, /* If needed, or any column to get more info */ `name` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `email` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `notification_template` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `status` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (`ID`), KEY `status` (`status`) ) ENGINE=InnoDB AUTO_INCREMENT=2196088 DEFAULT CHARSET=latin1 |
Step 15
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…..
Example query:
Table to table insertion from DB (All active profiles)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
INSERT INTO mailing_queue SELECT NULL, now(), object_id, CONCAT((SELECT meta_value FROM profile_meta C2 WHERE C1.object_id=C2.object_id AND meta_key="first_name"), ' ',(SELECT meta_value FROM profile_meta C2 WHERE C1.object_id=C2.object_id AND meta_key="last_name")) as full_name, (SELECT meta_value FROM profile_meta C2 WHERE C1.object_id=C2.object_id AND meta_key="email") as email, '', '', 'profile-status', 'pending' FROM profile_meta C1 WHERE coll_id='profile' AND meta_key="profile_status" AND meta_value="active" LIMIT 1 |
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)