Jump to content

If & date comparasion


_tina_

Recommended Posts

I have a script which sends out emails every night at 12am.

The problem is that the first and second email are going out together, rather than the first one day and the second the next day.

 

Loop
if(id == 1)
	send email
	increment id by 1
	update database
endif
if(id == 2)
	send email
	increment id by 1
	update database
endif
End Loop	

 

This is obviously going to send both the same night as the first if is incrementing the id so the 2nd if will be true.

 

Is there a way around this?  I have a "date_modified" field in a MySQL database which is updated after each email is sent.  Could I use this somehow to help here?

For instance:

if (id == 1 & date_modified !> ... ? )

 

Any help would be appreciated.

 

Thanks in advance.

Link to comment
Share on other sites

You say this is supposed to send out two e-mails. So you want like this... on a two day cycle you send email 1 the first day and email 2 the 2nd day. Then the next two days you do the same thing with the same e-mails? And so on and so on? If that loop is going through the people you are sending the email to then yes you will have to store a variable in your database to say which needs to be sent next. If the loop is for the days you can use a mod operator with a count variable like this

 

if(count % 2){
  send email2
}else{
  send email1
}

Link to comment
Share on other sites

How are you executing the script? Cron jobs?

 

If the script is executed at exactly midnight, and ytou only want to send a specific amount of emails in that one execution. Thyen logic like this should suffice:

 

Loop (while $loop_count is less than $loop_max)
Get Next Row From Mysql (that "Sent" = false)
Send Email
On Successfull Send, Set Row Field "Sent" = true
end Loop

Link to comment
Share on other sites

Thanks for the replies. 

 

Yes, I'm sending via a cron job.

 

The problem with a $loop_max variable is I have to send this to thousands of users so I don't know in advance what the max will be.

 

Thanks again for the reply.

 

How are you executing the script? Cron jobs?

 

If the script is executed at exactly midnight, and ytou only want to send a specific amount of emails in that one execution. Thyen logic like this should suffice:

 

Loop (while $loop_count is less than $loop_max)
Get Next Row From Mysql (that "Sent" = false)
Send Email
On Successfull Send, Set Row Field "Sent" = true
end Loop

Link to comment
Share on other sites

loop_max will be the number of emails you want to send that night, you can use a mysql query to count the amount of users if u want to send an email to everyone., but loop_max lets you put a maximum amount of emails to be sent (so u dont get IP banned or suspended or flooding servers or overloading your host.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.