Jump to content

Send Email only once with php


louisstephens
 Share

Recommended Posts

I have a foreach loop on a dashboard page where I sorting the pages titles to different columns based on a date field. Unfortunately, I am doing it with multiple different if statements for the sorting (with multiple foreach loops), as I needed something quickly for testing. I thought it would be nice to implement wireSMTP to send an email out to alert the users that created the pages, which I got working in no time. However, now I have hit a roadblock. The dashboard page has a javascript function that refreshes the page every few hours to get the changes in the date, which then triggers the $mail to fire the email out. 

I guess my question is there a way to limit the emails to be sent out only once the date/time has changed once instead of it firing every time the page refreshes?

Link to comment
Share on other sites

maybe you can store it in $session and check this before sending the emails?

if(true !== $session->emailsAlreadySent) {  // or: wire('session')->emailsAlreadySent
	// code to sent emails here
	...
	$session->emailsAlreadySent = true;
}

 

  • Like 1
Link to comment
Share on other sites

Thanks @horst, I actually figured out a way, though it might not be the prettiest/best solution. I ended up adding a checkbox into the templates called email_alert_recipients, and doing: 

 if ($today > $post_date && $testing->email_alert_recipients == 0) {
	$testing->setOutputFormatting(false);
	$testing->email_alert_recipients = true;
	$testing->save();

	// Email Config Below
	// ...
				
}

Thus far, I have not had any issues with it (fingers crossed).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...