def process(options, args):
config = get_config(options)
# Write the email
msg = MIMEMultipart()
msg['From'] = config['fromaddr']
msg['To'] = config['toaddrs']
msg['Subject'] = options.subject
body = options.body
msg.attach(MIMEText(body, 'plain'))
# Attach image
if options.image_file:
try:
filename = open(options.image_file, "rb")
attach_image = MIMEImage(filename.read())
attach_image.add_header('Content-Disposition',
'attachment; filename = %s'%options.image_file)
msg.attach(attach_image)
filename.close()
except:
msg.attach(MIMEText('Image attachment error', 'plain'))
# Converting email to text
text = msg.as_string()
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(config['username'],config['password'])
server.sendmail(config['fromaddr'], config['toaddrs'], text)
server.quit()
评论列表
文章目录