未加星标

python发送邮件脚本

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

本文实例为大家分享了python发送邮件的具体代码,供大家参考,具体内容如下

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
import sys
from email.mime.text import MIMEText
import linecache
import email
import os


#####################
# set email service host,user,pass word,postfix
mail_host="smtp.exmail.qq.com"
mail_user="username"
mail_pass="password"
mail_postfix="qq.com"
######################

def transfer_utf8_to_gb2312(file_name):
f=open(file_name)
s=f.read()
f.close()
u=s.decode("utf-8")
s=u.encode("gb2312")
f=open(file_name,"w");
f.write(s)


def send_mail(to_list,sub,content_file_name):
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = email.MIMEMultipart.MIMEMultipart()
content = open(content_file_name.encode("utf-8"), 'rb')
content_msg = MIMEText(content.read(),"plain","utf-8")
msg.attach(content_msg)
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user+"@"+mail_postfix,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print "error:",str(e)
return False

def send_mail_with_attachment(to_list,sub,content_file_name,attachment_file_name):
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = email.MIMEMultipart.MIMEMultipart()
content = open(content_file_name.encode("utf-8"), 'rb')
content_msg = MIMEText(content.read(),"plain","utf-8")
msg.attach(content_msg)
for tmp_attachment_file_name in attachment_file_name.split(","):
contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)
file_data = open(tmp_attachment_file_name.encode("utf-8"), 'rb')
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)
file_msg.set_payload(file_data.read())
file_data.close( )
email.Encoders.encode_base64(file_msg)
basename = os.path.basename(tmp_attachment_file_name)
file_msg.add_header('Content-Disposition', 'attachment', filename = basename.encode("utf-8"))
msg.attach(file_msg)
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user+"@"+mail_postfix,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print "error:",str(e)
return False

def print_usage():
print "Usage: "
print " %s email_send_list([email protected],[email protected],...) subject content_file_name" % (sys.argv[0])
print " %s email_send_list([email protected],[email protected],...) subject content_file_name attachment_file_name(file_name1,file_name2,...) if_transform_attachment_to_gb2312(yes or not)" % (sys.argv[0])

######Start from here#########
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('utf8')
if len(sys.argv) == 6:
send_list = sys.argv[1].split(",")
subject = unicode(sys.argv[2],"utf-8")
content_file_name = unicode(sys.argv[3],"utf-8")
attachment_file_name = unicode(sys.argv[4],"utf-8")
if(sys.argv[5] == "yes"):
transfer_utf8_to_gb2312(attachment_file_name.decode("utf-8"))
elif(sys.argv[5] == "not"):
pass
else:
print_usage()

if send_mail_with_attachment(send_list,subject,content_file_name,attachment_file_name):
print "Send email success!"
else:
print "Send email fail!"
sys.exit(1)
elif len(sys.argv) == 4:
send_list = sys.argv[1].split(",")
subject = unicode(sys.argv[2],"utf-8")
content_file_name = unicode(sys.argv[3],"utf-8")

if send_mail(send_list,subject,content_file_name):
print "Send email success!"
else:
print "Send email fail!"
sys.exit(1)
else:
print_usage()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。


您可能感兴趣的文章:python3.x实现发送邮件功能python3实现163邮箱SMTP发送邮件python3模块smtplib实现发送邮件功能python3使用smtplib实现发送邮件功能python3.5 email实现发送邮件功能Python smtplib实现发送邮件功能linux下python使用sendmail发送邮件python发送邮件的实例代码(支持html、图片、附件)python中使用smtplib和email模块发送邮件实例Python实现SMTP发送邮件详细教程

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: Python
tags: file,name,msg,content,sys,mail,email,utf,attachment,argv,list,print,send,邮件
分页:12
转载请注明
本文标题:python发送邮件脚本
本站链接:http://www.codesec.net/view/576946.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(259)