Initial scenario: I have PHP scripts in Wordpress that stores the entries of a multi step form post in MySQL DB and using PHPMailer emails the form entries to multiple emails (retrieved from DB and set in an array) in a loop.
Issue: The form post takes time to complete as it stores the entries in DB and sends out emails to multiple addresses. As it takes much time (more than 1min) for the form post, user would quit the form, affecting the lead conversion.
Proposed design: After form post, just store the form's entries in the DB table and so that form submission is completed, dispatch the function for email submissions in a loop till later using scheduled task or cron job. I imagine this reduces form submission time as the email submissions will just be a background process.
If this is a good design, can I use cron job? I was thinking of using https://www.sitepoint.com/introducing-cron/ But unsure how I can actually trigger this after a form submission is done. In below code snippet I have a function which handles what happens after form submission but only after send_notification is completed/returned the form submission is complete.
add_action('gform_after_submission_19', 'send_custom_notification', 10, 2);
function send_custom_notification($entry, $form) {
send_notification("form_submission", $form, $entry);
/*Is there a way to dispatch send_notification as a schedule task
or cron job so that form submission is finished but notification
is background process?*/
}
Instead I need a way to trigger the send_notification after form completion. Can one dispatch or trigger cron job or scheduled task after a function is completed?