#!/usr/bin/python
# -*- coding: utf-8 -*-
import smtplib
import sys, datetime
import email.mime.text
from email.mime.base import MIMEBase
from email.Header import Header
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import time, os
class EmailProcess(object):
@staticmethod
def SendEmail(head="", body="", attachment=""):
# my test mail
mail_username='user_name@163.com'
mail_password='password'
from_addr = mail_username
to_addrs=('to_user@163.com')
# HOST & PORT
HOST = 'smtp.163.com'
PORT = 25
# Create SMTP Object
smtp = smtplib.SMTP()
# print 'connecting ...'
# show the debug log
smtp.set_debuglevel(1)
# connet
try:
print smtp.connect(HOST,PORT)
except:
print 'CONNECT ERROR ****'
# gmail uses ssl
# smtp.starttls()
# login with username & password
try:
print 'loginning ...'
smtp.login(mail_username,mail_password)
except:
print 'LOGIN ERROR ****'
# fill content with MIMEText's object
msg = MIMEMultipart()
# add attachment
att_list = attachment.strip().split(',')
for att_item in att_list:
if att_item.strip() and os.path.exists(att_item.strip()):
att = email.mime.text.MIMEText(open(att_item.strip(),'rb').read(), 'base64', 'gb2312')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename=\"' + att_item.strip() + "\""
msg.attach(att)
msg['From'] = from_addr
msg['To'] = ';'.join(to_addrs)
msg['Subject']= Header(head, 'gb2312')
msg['Date'] = time.strftime("%a, %d %m %Y %H:%M:%S %z")
body = MIMEText(head, _charset='gb2312')
msg.attach(body)
smtp.sendmail(from_addr,to_addrs,msg.as_string())
smtp.quit()
if __name__ == '__main__':
head = "buyoffer stopped(" + str(datetime.datetime.now()) + "), max_aliid: "
print "len(sys.argv)", len(sys.argv)
if len(sys.argv) >= 2:
aliid = sys.argv[1]
else:
aliid = "none"
head = head + aliid
EmailProcess.SendEmail(head=head)