#! /usr/bin/env python2.6
# encoding: utf-8
# 2013-1-21
# written by Tony.yang
#
import smtplib, mimetypes
from email.mime.text import MIMEText
from email.mime.p_w_picpath import MIMEImage
from email.mime.multipart import MIMEMultipart

# From email important
username = 'youremail@163.com'                  # define(alter) from email-address
password = 'yourpassword'                        # define(alter) from email-password
# To email important
Toemail = 'toemail@xxx.com'                        # define(alter) to email-address

# SMTP important
SMTPServer = 'smtp.163.com'                          # define(alter) SMTP server
# To email important and structure root container
msg = MIMEMultipart()
msg["From"] = username
msg["To"] = Toemail
msg["Subject"] = "Donglong-netword-flow by python"

# structure content of root
text = MIMEText("This is Donglong-netword-flow:")
msg.attach(text)

#
filename = r'/tmp/picture/picture.jpg'
ctype, encoding = mimetypes.guess_type(filename)
if ctype is None or encoding is not None:
    ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)

att = MIMEImage(open(filename, 'rb').read(), subtype)
att["Content-Disposition"] = 'p_w_upload;filename="picture.jpg"'
msg.attach(att)

# send email(send / to xxx@xx.com)
smtp = smtplib.SMTP()
smtp.connect(SMTPServer)
smtp.login(username, password)
smtp.sendmail(msg["From"], msg["To"], msg.as_string())
smtp.quit()