FrankI use this function to send emails with errors or logs.Just pass in an email, subject and body text....also set the SMTP server-Pat#==========================================================================
def emailThis(to, subject="",body="",files=[]):
try:
fro = "from@gmail.com"
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
msg = MIMEMultipart()
msg['From'] = fro
msg['To'] = to
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = subject
msg.attach(MIMEText(body))
for file in files:
part = MIMEBase('application',"octet-stream")
part.set_payload(open(file,"rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment; filename="%s"' %os.path.basename(file))
msg.attach(part)
smtp = smtplib.SMTP("some.smpt.server.local")
smtp.sendmail(fro,to,msg.as_string())
smtp.close()
return True
except:
print(" error")
return False
return 1