Web2py tool : Send emails to multiple user on schedule basis

221 views
Skip to first unread message

madhu nomula

unread,
Aug 2, 2016, 1:51:36 PM8/2/16
to web2py-users
Hi All,

I need help on below requirement.

We have implemented Time sheet  logging tool for users in web2py tool.

Now the requirement is to send email notifications to the users of tool, to enter time sheet and submit.
This email has to be sent for all users from 1 to 15 days of month and 16 to 30 days of month till the user submits time sheets.

Please suggest, how can I schedule this sending emails option in Web2Py tool.

Thank you and appreciate your help in advance.

Regards,
Madhu Chandar Nomula.

Dave S

unread,
Aug 2, 2016, 2:58:19 PM8/2/16
to web2py-users


On Tuesday, August 2, 2016 at 10:51:36 AM UTC-7, madhu nomula wrote:
Hi All,

I need help on below requirement.

We have implemented Time sheet  logging tool for users in web2py tool.

Now the requirement is to send email notifications to the users of tool, to enter time sheet and submit.
This email has to be sent for all users from 1 to 15 days of month and 16 to 30 days of month till the user submits time sheets.

I am not sure what you mean here ... That twice a month you start nagging the users?  That's the easiest interpretation.  Also, I think, easy to do ... see below.
 

Please suggest, how can I schedule this sending emails option in Web2Py tool.

I recommend using the built-in scheduler.  This looks something like:
(read about it at <URL:http://web2py.com/books/default/chapter/29/04/the-core#web2py-Scheduler>)
(and note that this is untested, but based on my own scheduler usage and comments in this forum by Niphlod)

in your model file (scheduler.py is a good name)
# define scheduler task
   
def send_nags(first_reminder = False):
     
if first_reminder or now.date in [1, 16]:
        db
(db.staff.ALL).update(curts_recd = False)
      mail
= auth.settings.mailer        
      query
= db.staff.curts_recd == False
      rows
= db(query)
     
for row in rows:
        msg
= compose_reminder(row.id, ...)
        mail
.send(to=row.email, reply_to="payroll@mybiz", message = msg)

# instantiate Scheduler class
from gluon.scheduler.import Scheduler
scheduler
= Scheduler(db)


In one of your controller files:
# schedule recurring tasks:
def sched_tasks(arg1=""):
 
# arg1 keeps this from being expose as a web page
 
# instantiate Scheduler class
 
from gluon.scheduler.import Scheduler
  scheduler
= Scheduler(db)
  task
= scheduler.queue_task("send_nags", period = 86400, repeats=1)
 
# show on console, or add to log
 
print task
  db
.commit()

# also add as needed
def stop_tasks(arg1=""):
 
# arg1 keeps this from being expose as a web page
 
# instantiate Scheduler class
 
from gluon.scheduler.import Scheduler
  scheduler
= Scheduler(db)
  task
= scheduler.task_status(db.scheduler_task.task_name == 'send_nags')
 
if hasattr(task, "status") and task.status in ["QUEUED", "RUNNING"]:
     stop
= scehduler.stop_task(task.uuid)
     
# show on console or add to log
     
print cue
     db
.commit()




Use  these from the command line like this:
web2py.py -M -S uploader/default/sched_tasks
or
web2py.py -M -S uploader/default/stop_tasks

(and of course, you've used the -K option earlier to start the scheduler ... you can add this to the startup sequence for your webserver)

 See
   <URL:https://groups.google.com/d/msg/web2py/9WrqIxi75JY/UHxICIC-BQAJ>
for more details.


madhu nomula

unread,
Aug 16, 2016, 7:26:33 AM8/16/16
to web...@googlegroups.com
Thanks Dave.

I wrote code like below: But I am not sure how to schedule this. Could
you please guide me to configure this Scheduler (need bit more
elaborated steps). Thank you appreciate your help:
______________________
In Controller: SendEmail.py:
# -*- coding: utf-8 -*-
# try something like
from gluon.tools import Mail
def send_email():
mail = Mail()
#mail.settings.server = 'smtp.hitachiconsulting.com'
mail.settings.server = 'smtp.gmail.com:587'
mail.settings.sender = 'a...@example.com'
mail.settings.login = 'myto...@gmail.com:123'
x = mail.send(
to = ['m...@gmail.com'], subject = 'hello',
reply_to = 'a...@example.com',
message = 'Hello ! How are you?'
)
if x:
nm="Sent Successfully";
else:
nm="failed";
_____________
In Controller: scheduler.py
from gluon.scheduler import Scheduler
# schedule recurring tasks:
def sched_tasks(arg1=""):
# arg1 keeps this from being expose as a web page
# instantiate Scheduler class
scheduler = Scheduler(db)
task = scheduler.queue_task("send_email", period = 86400, repeats=1)
# show on console, or add to log
print task
db.commit()

def stop_tasks(arg1=""):
# arg1 keeps this from being expose as a web page
# instantiate Scheduler class
scheduler = Scheduler(db)
task = scheduler.task_status(db.scheduler_task.task_name == 'send_email')
if hasattr(task, "status") and task.status in ["QUEUED", "RUNNING"]:
stop = scehduler.stop_task(task.uuid)
# show on console or add to log
print cue
db.commit()
_____________________________


Please suggest me, next what I have to do , to schedule sending emails
1 to 15 days and 16 to 30 days .

Regards,
MC
> --
> Resources:
> - http://web2py.com
> - http://web2py.com/book (Documentation)
> - http://github.com/web2py/web2py (Source code)
> - https://code.google.com/p/web2py/issues/list (Report Issues)
> ---
> You received this message because you are subscribed to the Google Groups
> "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to web2py+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

黄祥

unread,
Aug 16, 2016, 7:49:05 AM8/16/16
to web2py-users
i think the sendemail.py should be in models
models/SendEmail.py: 
# -*- coding: utf-8 -*- 
# try something like 
from gluon.tools import Mail 
def send_email(): 
  mail = Mail() 
  #mail.settings.server = 'smtp.hitachiconsulting.com
  mail.settings.server = 'smtp.gmail.com:587
  mail.settings.sender = 'a...@example.com
  mail.settings.login = 'myto...@gmail.com:123
  x = mail.send( 
  to = ['m...@gmail.com'], subject = 'hello', 
  reply_to = 'a...@example.com', 
  message = 'Hello ! How are you?' 
  ) 
  if x: 
    nm="Sent Successfully"; 
  else: 
    nm="failed"; 

not sure what do you mean with to schedule sending emails 1 to 15 days and 16 to 30 days, if you want to send email which is date is 1-15, i think you can use conditional if :
if request.now.day >= 1 and request.now.day <= 15:

best regards,
stifan

madhu nomula

unread,
Aug 16, 2016, 7:58:11 AM8/16/16
to web...@googlegroups.com
Thank you.
I moved the code to Model. But could you please share me steps that
how to schedule(trigger) it send emails.

Dave S

unread,
Aug 16, 2016, 2:00:11 PM8/16/16
to web2py-users


On Tuesday, August 16, 2016 at 4:58:11 AM UTC-7, madhu nomula wrote:
Thank you.
I moved the code to Model. But could you please share me steps that
how to schedule(trigger) it send emails.


I thought my suggested code was pretty clear.  sched_tasks() sets up the task to run every day.  The task then checks to see if it needs to send mail to everybody or just to the slow responders.

If you want to schedule the task to run just twice a month, then you change the "86400" to (approximately) 1209600.  I say "approximately" because that value if for "every two weeks".  To do the 1st and 16th, you need to figure out how much time there is from "now" to the 1st or 16th.  You'd use a standard Python datetime.timedelta to figure that out.  You can make sched_tasks() start just the first run, and have the task itself schedule itself (actually, another task instance running the same code); in that case, the call to scheduler.queue_task() will set repeats=0.

Please read
<URL:http://web2py.com/books/default/chapter/29/04/the-core#web2py-Scheduler>
for details about the scheduler.

/dps


 

Dave S

unread,
Oct 26, 2016, 4:17:44 AM10/26/16
to web2py-users


On Tuesday, August 16, 2016 at 11:00:11 AM UTC-7, Dave S wrote:


On Tuesday, August 16, 2016 at 4:58:11 AM UTC-7, madhu nomula wrote:
Thank you.
I moved the code to Model. But could you please share me steps that
how to schedule(trigger) it send emails.


I thought my suggested code was pretty clear.  sched_tasks() sets up the task to run every day.  The task then checks to see if it needs to send mail to everybody or just to the slow responders.

If you want to schedule the task to run just twice a month, then you change the "86400" to (approximately) 1209600.  I say "approximately" because that value if for "every two weeks".  To do the 1st and 16th, you need to figure out how much time there is from "now" to the 1st or 16th.  You'd use a standard Python datetime.timedelta to figure that out. 

As I figured out more recently (like in an answer I posted a few hours ago), you don't really need a time delta, you just need to figure out if the next run is "this month" or "next month' (1-12, not 0-11), and use next_run_time.
Reply all
Reply to author
Forward
0 new messages