0

I have a python script test.py that sends email with an excel attachment every 24 hours. But, is there a way to make to automate this more i.e. to make this process run in the background at all times even if I close the terminal so that I don't have to start it everyday thus negating the whole purpose of automating it?

import smtplib,email,email.encoders,email.mime.text,email.mime.base
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import schedule
import time

def job():
    msg = MIMEMultipart()
    # me == my email address
    # you == recipient's email address
    me = "abc@gmail.com"
    you = "xyz@gmail.com"
    password ='passwork'


    # Create message container - the correct MIME type is multipart/alternative.
    msg = MIMEMultipart('mixed')
    msg['Subject'] = "Automation Testing"
    msg['From'] = me
    msg['To'] = you

    # Create the body of the message (a plain-text and an HTML version).
    text = "Hi\n\nPlease find attached the weekly report."


    part1 = MIMEText(text, 'plain')

    #attach an excel file:
    fp = open('/Users/excelsheet.xlsm', 'rb')
    file1=email.mime.base.MIMEBase('application','vnd.ms-excel')
    file1.set_payload(fp.read())
    fp.close()
    email.encoders.encode_base64(file1)
    file1.add_header('Content-Disposition','attachment;filename=anExcelFile.xlsx')

    # Attach parts into message container.
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.

    msg.attach(part1)
    msg.attach(file1)

    composed = msg.as_string()

    fp = open('msgtest.txt', 'w')
    fp.write(composed)

    # Credentials (if needed)  
    # The actual mail send  
    server = smtplib.SMTP('smtp.gmail.com', 587)  
    server.starttls()  
    server.login('abc@gmail.com', password)
    server.sendmail(me, you, composed)  
    server.quit()  
    fp.close()

schedule.every().minutes.do(job)
schedule.every(24).hour.do(job)
schedule.every().day.at("10:30").do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)
martineau
  • 119,623
  • 25
  • 170
  • 301
Reshma
  • 21
  • 1
  • 4
  • 2
    ... Cron job? I don't think there's a Python-specific answer to this. Whatever scheduled task functionality your OS provides, you can use that. – Kevin Mar 28 '17 at 17:52
  • 2
    Don't try to make the script run continuously in the background. Use `cron` (if on Mac OS X or Linux) or Task Scheduler (Windows) to run it at a specified time each day. Exactly how to do this is out of scope for Stack Overflow, as it is not a programming topic. Try [SuperUser](https://superuser.com/). – kindall Mar 28 '17 at 17:59
  • All right, I'll look into cron as I'm on Mac – Reshma Mar 28 '17 at 18:02
  • Agree - use cron on MAC. I use cron on my mac to run a report once a week. Of course, my computer has to be on ( and VPN'd ). If I needed more reliability, I would switch to a linux host in a data center. – Mark Mar 28 '17 at 18:07

0 Answers0