0

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!

3 Answers 3

0

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);
}
2
  • 1
    This will most likely crash the server ("Maximum execution time of XX seconds exceeded") and/or not even matter if the user closes therir window.
    – kero
    Mar 3, 2022 at 13:34
  • Thank you! I appreciate your help! I tried the delayed message works! But unfortunately, at the time of submission, the cf7 form freezes. You will not receive a thank you message and will not be redirected to the thank you page. Maybe you have an idea? Thank you!
    – lcsdv
    Mar 4, 2022 at 9:40
0

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?)

1
  • Thanks for the help! Then I'll try along these.
    – lcsdv
    Mar 8, 2022 at 13:44
-1

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.

2
  • I don't think that answers the question, which is to deliberately delay the emails generated.
    – Rup
    Mar 6, 2022 at 0:17
  • There was no access to reply to 2nd comment so I had written it as an answer.
    – Nabeen
    Mar 7, 2022 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.