Contact form 7 - automatic email delay

I would like to ask if there is a way to automatically delay emails after completion?

I want these emails to arrive 1-2 hours later.

Is there a snippet of code for this?

Thank you!

Topic plugin-contact-form-7 Wordpress

Category Web


I don't know CF7 well enough to give you specifics, but I think you'll need to

  1. Write the form data to the database so you can read it back later, when you want to send the email. I don't believe CF does this automatically (although e.g. Gravity forms do)
  2. On submission
    1. Cancel the existing email send: I'd guess there's an event you can hook and return false that'll do this
    2. Either schedule a one-off event for two hours time using wp_schedule_single_event() and the saved data's database ID, and in that event handler call back to the CF7 with the data to send the email, and then either deletes the saved form data from the database or otherwise marks it as sent
    3. Or set up a regular scheduled job with wp_schedule_event() that runs every five minutes or so and checks if it needs to send any emails, e.g. there is a new saved form data record from >2 hours ago that is not already marked as sent in the database, and if so sends the email and marks that form sent
  3. Depending on your traffic levels you may want to set up an alternative cron mechanism to ensure these jobs run on time: Hooking WP-Cron into the system scheduler

Alternatively depending on how you're sending emails you may be able to generate them on the initial form submission to be sent later, e.g. if you're sending them through Outlook.com or if your wp_mail hander has built-in retry and scheduling (as I'd guess WP Offload SES might?)


In some cases, if you are not logged in on your site your form doesn't get submitted. Try submitting the form being logged in on site.


not tested but maybe you could simply use sleep using wpcf7_before_send_email

something like this:

add_action("wpcf7_before_send_mail", "wpcf7_pause_send");  
function wpcf7_pause_send($cf7) {
    
    sleep(3600);
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.