码迷,mamicode.com
首页 > 编程语言 > 详细

python实现发邮件(sendmail.py )

时间:2014-07-17 09:45:23      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:python发邮件脚本

#!/usr/bin/python

#-*-coding:utf-8-*- 

#-*-coding:utf-8-*-

#filename :sendmail.py

import sys 

import smtplib

from email.mime.text import MIMEText

from email.header import Header

from email.mime.multipart import MIMEMultipart

from email import Utils

import getopt

import base64



smtp_server = ‘mail.yy.com‘

mail_postfix=‘@yy.com‘

username = base64.decodestring(‘Y2hlbndlaWJl5LmNvbQ==\n‘)

password = base64.decodestring(‘d2I4NDTIwOA==\n‘)

flag_att=0

def usage():

print ‘‘‘ 

-f or --from   Mail Sender

-r or --recipients   Mail Recipients

-s or --subject  Mail Subject

-c or --content  Mail Content file path

[-i] or --iattachment Mail Attached file path, support asterisk wildcard

[-u] or --user Auth user

    [-p] or --password Auth pass

Examples:

send mail:

sendmail.py -f chenweibo -r chenweo,caog -s mysubject -c "test content"  -i ‘./*.gif‘

send attachment mail:

sendmail.py -f chenweibo -r chenweo,caog -s mysubject -c "aaa content"  -i ‘./*.gif‘

send mail with tar.gz attach:

sendmail.py -f chenweibo -r chenweo,zhouda -s mysubject -c "hello world" ‘./apache.tar.gz‘

‘‘‘

exit(0)




def send_mail(sender,to_list,sub,content,user,passw):

msg = MIMEText(content,_subtype=‘plain‘,_charset=‘gb2312‘)  

msg [‘Subject‘] = Header(sub,‘utf-8‘)

msg[‘From‘] = sender   

msg[‘To‘] = to_list  

msg[‘Date‘] = Utils.formatdate(localtime = 1)

print msg[‘Date‘]

smtp = smtplib.SMTP()

smtp.connect(smtp_server)

smtp.login(user,passw)

for recipient in to_list.split(‘,‘):

smtp.sendmail(sender,recipient, msg.as_string())

smtp.quit



def send_att_mail(sender,to_list,sub,content,att,user,passw):


msg = MIMEMultipart()

msg[‘Subject‘] = Header(sub,‘utf-8‘)

msg[‘From‘] = sender  

msg[‘To‘] = to_list  

msg[‘Date‘] = Utils.formatdate(localtime = 1)

print msg[‘Date‘]

attachment_file = att 

att1 = MIMEText(open(attachment_file, ‘rb‘).read(), ‘base64‘, ‘gb2312‘)

att1["Content-Type"] = ‘application/octet-stream‘

att1["Content-Disposition"] = ‘attachment; filename=‘+attachment_file.split("/")[-1]

msg.attach(att1)

#mail_context=MIMEText(content,‘text‘,‘utf-8‘)

mail_context = MIMEText(content,_subtype=‘plain‘,_charset=‘gb2312‘)  

msg.attach(mail_context)

smtp = smtplib.SMTP()

smtp.connect(smtp_server)

smtp.login(user,passw)

for recipient in to_list.split(‘,‘):

smtp.sendmail(sender,recipient, msg.as_string())

smtp.quit

print sys.argv[1:]

if ( len( sys.argv ) == 1 ):

print ‘-h or --help for detail‘

sys.exit(1)

shortargs = ‘hf:r:s:c:i:u:p:‘

longargs = [‘help‘, ‘from=‘, ‘recipients=‘, ‘subject=‘, ‘content=‘, ‘attachment=‘,‘user=‘,‘password=‘]

opts, args = getopt.getopt( sys.argv[1:], shortargs, longargs )


print args

if args:

print ‘-h or --help for detail‘

sys.exit(1)

for opt,val in opts:

if opt in ( ‘-h‘, ‘--help‘ ):

usage()

continue

if opt in ( ‘-f‘, ‘--from‘ ):

sender=val+mail_postfix

continue

if opt in ( ‘-r‘, ‘--recipients‘ ):

receiver = val

continue

if opt in ( ‘-s‘, ‘--subject‘ ):

subject=val

continue

if opt in (‘-c‘,‘--context‘):

content=val

continue

if opt in (‘-i‘,‘--iattachment‘):

attachment=val

flag_att=1

continue

if opt in (‘-u‘,‘--user‘):

username=val+mail_postfix

continue

if opt in (‘-p‘,‘--password‘):

password=val

continue


print flag_att

if flag_att==1:

send_att_mail(sender,receiver,subject,content,attachment,username,password)

else:

send_mail(sender,receiver,subject,content,username,password)


本文出自 “漫漫长路” 博客,谢绝转载!

python实现发邮件(sendmail.py ),布布扣,bubuko.com

python实现发邮件(sendmail.py )

标签:python发邮件脚本

原文地址:http://ithelp.blog.51cto.com/4501237/1439109

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!