Contact form 7 - automatic email delay

admin2025-01-08  4

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed last month.

Improve this question

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!

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed last month.

Improve this question

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!

Share Improve this question asked Mar 3, 2022 at 11:52 lcsdvlcsdv 12 bronze badges 1
  • Suffice to say, probably best to do simple search on the wordpress.org plugin directory for ones that have this functionality, as there are a few of them. See Delayed Notifications for Conatct Form 7 for example. I came to this thread a few weeks back trying to figure out the same thing, couldn't get any of the code snippets to work, then found a plugin already out there that basically did exactly what the OP was asking, and thought I'd share. – Vincent Commented Nov 21, 2024 at 18:45
Add a comment  | 

2 Answers 2

Reset to default 1

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

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);
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736268104a1248.html

最新回复(0)