2
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
from Tkinter import *
import tkMessageBox
import smtplib
import sys
from email.mime.multipart
import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header

def Call():
  global root
  sender = 'Nome 1 <email1@exemplo.com>'
  receivers = ['Nome 2 <email2@exemplo.com>',
               'Nome 3 <email3@exemplo.com>',
               'Nome 4 <email4@exemplo.com>']

  msg = MIMEMultipart('alternative')
  msg['Subject'] = Header("Envio de relatório diário", 'utf-8')
  msg['From'] = sender
  msg['To'] = ", ".join(receivers)
  text = "A actividade de hoje foi:\n\n\n\n" + tb.get("1.0",'end-1c')

  msg.attach(MIMEText(text.encode('utf-8'), 'plain', 'utf-8'))    

  try:
    smtpObj = smtplib.SMTP('smtp.office365.com',587)
    smtpObj.ehlo()
    smtpObj.starttls()
    smtpObj.ehlo()
    smtpObj.login('emailAqui','passwordAqui')
    smtpObj.sendmail(sender, receivers, msg.as_string())
    smtpObj.quit()
    tkMessageBox.showinfo("Sucesso", "A sua mensagem foi enviada com sucesso.")
    root.quit()
  except SMTPException:
    tkMessageBox.showinfo("Erro", "Ocorreu um erro a enviar a mensagem")

os.environ["DISPLAY"] = ":0.0"
root = Tk()
root.wm_title("Report sender")
root.geometry('600x675')
tb = Text(root, height = 40)
tb.pack(side=TOP,pady=10)
button = Button(root, text = 'Send report!', command = Call)
button.pack(side=TOP,pady=10)

root.mainloop()
root.destroy()

I have a python script with the code above. It works as expected if I call it using the absolute path:

/usr/local/bin/reportSender.py

But when I place it in the crontab it doesn't appear any window, as it does when I execute it using it's absolute path.

My crontab, for testing purposes, looks like this:

30 10 * * * /usr/local/bin/reportSender.py
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
rafaelcpalmeida
  • 874
  • 1
  • 9
  • 28
  • how do you set up your crontab? – fedorqui May 05 '16 at 09:47
  • OK the syntax seems to be fine. Try checking [debugging crontab](http://stackoverflow.com/tags/crontab/info). Also, should it really work? From what I read in [Trying to run a python script from ubuntu crontab](http://stackoverflow.com/q/17518073/1983854) _The cronjob won't have access to a display where it can display the GUI_. – fedorqui May 05 '16 at 09:59
  • It should work because I'm telling the script to use the display I have in the computer, with this line: `os.environ["DISPLAY"] = ":0.0"` – rafaelcpalmeida May 05 '16 at 10:01

1 Answers1

0

Provide the full path to python, or simply python, if it's already included on user path, i.e.:

30 10 * * * /full/path/to/python /usr/local/bin/reportSender.py

Or

30 10 * * * python /usr/local/bin/reportSender.py
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268