Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script to send mail at specific time and day every month

I want to send mail every 2nd Friday in month at 4:00 p.m. How we will do in python. I know following logic is not efficient, is there other way to do this in python?

// Pseudocode for check 2nd Friday and time 4:00 p.m.

function(Day='Fri',Time='4:00 p.m.')
while(1){
    String current_date=new date();// This will in following formate.
   // Fri Aug 21 2015 16:00:00 GMT+0530 (IST). 
   // Here we can see that it is combination of Date, Day and time.
   //Now this is the current_date is string so search the sub string 'Fri'
  if(current_date.substring('Fri') && current_date.substring('16:00:00')){
      // Now search for this date is 2nd Friday or not,
      int day=current_date.getDay();
      if(day>7 && day<=13)
         start_script to send mail
  }
}
like image 847
geeks Avatar asked Aug 19 '15 12:08

geeks


People also ask

How do I run a Python script at a specific time?

Double-click on the Task Scheduler, and then choose the option to 'Create Basic Task…' Type a name for your task (you can also type a description if needed), and then press Next. For instance, let's name the task as: Run Hello World. Choose to start the task 'Daily' since we wish to run the Python script daily at 6am.

How do you automate a schedule in Python?

To schedule a Python script with Task scheduler, create an action and add the path to your Python executable file, add the path to the script in the “Start in” box and add the name of the Python file ase an argument. Then, create a trigger to schedule the execution of your script.

Can you automate emails with Python?

Sending emails manually is a time-consuming and error-prone task, but it's easy to automate with Python. In this tutorial you'll learn how to: Set up a secure connection using SMTP_SSL() and .


1 Answers

You can use the python-crontab module.

https://pypi.python.org/pypi/python-crontab

Here's how you can use it:

from crontab import CronTab
#Initialize Cron
cron   = CronTab()

#Add your jobs
job  = cron.new(command='/usr/bin/echo')

#Set period
job.hour.every(4)

Alternatively, you can use crontab -e and follow this link: http://adminschoice.com/crontab-quick-reference

like image 67
Abhyudit Jain Avatar answered Sep 23 '22 14:09

Abhyudit Jain