Skip to content

Instantly share code, notes, and snippets.

@rafaelcpalmeida
Last active April 19, 2016 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelcpalmeida/efbcb5ef55d38381ed530e7225234fc8 to your computer and use it in GitHub Desktop.
Save rafaelcpalmeida/efbcb5ef55d38381ed530e7225234fc8 to your computer and use it in GitHub Desktop.
Send emails with text from a textbox called from crontab
#!/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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment