# coding: utf-8 import email import email.mime import email.mime.text import email.mime.multipart import os import smtplib smtpPort = '465' smtpSrv = 'smtp.gmail.com' smtpUser = '' # TODO: Set username smtpPass = '' # TODO: Set password def sendMail (fromAddr, toAddr, subject, body = '', attachment = ''): message = email.mime.multipart.MIMEMultipart() message.add_header('From',fromAddr) message.add_header('To',toAddr) message['Subject'] = email.header.Header(subject,'utf-8') if (body != ''): msgPart = email.mime.text.MIMEText(body,'plain','utf-8') message.attach(msgPart) if (attachment != ''): if os.path.exists(attachment) == True: filename = attachment.rpartition(os.sep)[2] fp = open(attachment,'rb') msgPart = email.mime.base.MIMEBase('application','octet-stream') msgPart.set_payload(fp.read()) fp.close() email.encoders.encode_base64(msgPart) msgPart.add_header('Content-Disposition','attachment',filename=filename) message.attach(msgPart) if smtpPort == 25: smtpCon = smtplib.SMTP(smtpSrv,smtpPort) else: smtpCon = smtplib.SMTP_SSL(smtpSrv,smtpPort) if (smtpUser != '') and (smtpPass != ''): smtpCon.login(smtpUser,smtpPass) smtpCon.send_message(message,mail_options=['UTF8SMTP','8BITMIME']) smtpCon.quit() if __name__ == "__main__": sendMail('','','žluťoučký kůň','úpěl ďábelské ódy') # TODO: Set sender and receiver email