Skip Navigation

[Closed] CRON event wp_mail function executes twice instead of one time

This support ticket is created 8 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 7 replies, has 3 voices.

Last updated by Luo Yang 8 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#364334

Following up on this thread which is marked as resolved:
https://toolset.com/forums/topic/notification-setup-for-dynamic-expiration-date/

I am using a CRON event to send a notification email to the author of the post when the post expiration time is less than 10 days.

// Send 10 days expiry notification email
add_action( 'ten_day_poster_expire_notification_hook', 'ten_day_poster_expire_notification_function' );

function ten_day_poster_expire_notification_function() {
	$args = array( 
		'post_status' => 'publish',
		'posts_per_page' => -1
	);
    $published_posts = get_posts( $args );
	$subject = 'Your poster subscription is running out';
	$site_link = get_site_url();
	$site_name = get_bloginfo();
	foreach( $published_posts as $post ) : setup_postdata($post);
		$post_title = get_the_title($post->ID);
		$post_links = get_permalink($post->ID);
    	$expiry_time = get_post_meta( $post->ID, '_cred_post_expiration_time', true);
		$expiry_time_formatted = date("F j, Y, g:i a", $expiry_time);
		$author_email = get_the_author_meta( 'user_email', $post->post_author );
		$body = '<p>Your subscription for the <a href="' . $post_links . '">' . $post_title .'</a> poster ends on <strong>' . $expiry_time_formatted . '</strong>. ' . 'Click <a href="' . $post_links . '">here</a> to renew it.</p><p>Kind Regards</p><p><strong>The <a href="' . $site_link .'">' . $site_name .'</a> team.</strong></p> ' ;
		$headers = array('Content-Type: text/html; charset=UTF-8');
		$email_sent_status = get_post_meta( $post->ID, 'wpcf-ten-days-expiration-email-status', true);
		if ( (time() > $expiry_time - 60*60*240) && ($email_sent_status != "email sent") ) {	
				wp_mail( $author_email, $subject, $body, $headers );
				update_post_meta( $post->ID, 'wpcf-ten-days-expiration-email-status', "email sent");
		}
	endforeach; 
	wp_reset_postdata();
}

I use this code and not the CRED expiry functionality because my expiration date '_cred_post_expiration_time' is depended on a custom field that the user will select to extend his/her subscription. Tha also means that I have completely deactivated the "Enable Automatic Expiration of Post options for CRED Post Forms" from CRED settings.
I have a custom field 'wpcf-ten-days-expiration-email-status' set to "email not sent". Whenever I fire up the CRON event(using the "WP Crontrol" plugin) the value of that custom field is set to "email sent" and the notification email reaches teh author. The problem I have is that around 5 minutes after the first email, an identical email also reached the author(with the same timestamp as the first email) and I don't understand why that is happening since the 'wpcf-ten-days-expiration-email-status' custom field has been set to "email sent". After the second email is sent, behaviour is as expected, no other email is sent after the CRON event is fired.

#364445

Thank you for contacting Toolset support, I'd be delighted to assist!

There may be an issue regarding Toolset's caching which could be causing CRED notifications to not send. As I don't want to run you through troubleshooting until we diagnose the caching issue, please await an update from me by tomorrow evening on the Caching issue - we should have a determination of if this is causing notifications not to fire.

#364460

I don't think that has anything to do with it. I create my own notification and not depend on the CRED ones. Also it's sending the notifications fine, the problem is that it sends it twice and not once as per code.

#364838

Hello George!

As I am not a PHP developer myself and can only assist with stock Toolset functionality, I would kindly ask you to get in touch with one of our consultants: https://toolset.com/consultant/

I do realize that Lou may of provided custom code examples and there is is a grey area regarding this as Toolset is a complex suite of plugins.

If you'd like I can reassign this ticket back to Lou to see if he could efficiently resume assisting you in this matter but please know regardless of supporter, custom code is a best effort basis and technically isn't in the scope of support - especially considering the use of internal Toolset APIs.

I appreciate your understanding!

#364856

Hi Ross, yes, could you please give Lou a shout, he might be able to see something in the code I am not seeing.

Thanks!

#365016

No worries, I have reassigned the ticket to Lou!

#368851

I am sorry but I got an automated reply for this threat if I still wanted any help. I still want help and I thought Rosss had reassigned the ticket to Lou.

#369009

Dear George,

Could you describe the question with more details?
Is the action hook "ten_day_poster_expire_notification_hook" from another plugin/theme?
Since it is a custom PHP codes problem, please duplicate same problem in a test site, fill the login details and ftp access in below private detail box, also point out the problem page URL and CRED form, and where I can edit your PHP codes.

The topic ‘[Closed] CRON event wp_mail function executes twice instead of one time’ is closed to new replies.