使用python发邮件
来源:互联网 发布:网络语铲屎是什么意思 编辑:程序博客网 时间:2024/05/16 00:36
下面简单介绍下如何使用python发送邮件,包括普通文本内容,也可以带附件,或者HTML内容的邮件。可以说有了python,一切都变得非常的容易。
smtplib 模块是用来发送email的标准module,另外配合email.mime内的几个module实现起来就非常的简单。
- def sendmail(message):
- try:
- smtp = smtplib.SMTP(EMAIL_HOST)
- smtp.login(EMAIL_USER, EMAIL_PWD)
- smtp.sendmail(EMAIL_USER+"@"+EMAIL_POSTFIX, TO_EMAIL, message)
- smtp.quit()
- print 'email send success.'
- except Exception ,e:
- print e
- print 'email send failed.'
def sendmail(message): try: smtp = smtplib.SMTP(EMAIL_HOST) smtp.login(EMAIL_USER, EMAIL_PWD) smtp.sendmail(EMAIL_USER+"@"+EMAIL_POSTFIX, TO_EMAIL, message) smtp.quit() print 'email send success.' except Exception ,e: print e print 'email send failed.'首先要导入smtplib模块,加入import smtplib,初始化的时候直接连上邮箱服务器,也就是上面的EMAIL_HOST(我使用的是163邮箱,所以定义EMAIL_HOST = 'smtp.163.com'),由于目前的邮箱基本都需要登录才可以使用,所以要调用smtp.login()函数,输入用户和密码。
- def sendwithoutattachment():
- msg = MIMEText(getcontent(), 'plain','utf-8')
- getheader(msg)
- sendmail(msg.as_string())
def sendwithoutattachment(): msg = MIMEText(getcontent(), 'plain','utf-8') getheader(msg) sendmail(msg.as_string())
我将发送简单文本内容的邮件单独独立为一个函数,使用MIMEText生成,注意这里用到了utf-8,因为内容有可能是中文,所以要特别指定。如果要发送html内容,则讲plain更改为html即可。
- def getheader(msg):
- msg['From'] = ME
- msg['To'] = ";".join(TO_EMAIL)
- msg['Subject'] = EMAIL_HEADER
- msg['Date'] = formatdate(localtime=True)
def getheader(msg): msg['From'] = ME msg['To'] = ";".join(TO_EMAIL) msg['Subject'] = EMAIL_HEADER msg['Date'] = formatdate(localtime=True)
getheader函数是用来设置发送者,接受者,主题和发送时间的。
- def getcontent():
- path = os.getcwd()
- file = os.path.join(path, CONTENT_FILE_NAME)
- content = open(file, 'rb')
- data = content.read()
- try:
- data = data.decode('gbk')
- except :
- data = data.decode('gbk','ignore')
- content.close()
- return data
def getcontent(): path = os.getcwd() file = os.path.join(path, CONTENT_FILE_NAME) content = open(file, 'rb') data = content.read() try: data = data.decode('gbk') except : data = data.decode('gbk','ignore') content.close() return data至于邮件正文,我是事先写到一个TXT文档中,读取出来。这样也比较隐蔽。:)要主意中文编码。
- def getattachment(msg):
- ctype, encoding = mimetypes.guess_type(ACCESSORY_FULLPATH)
- if ctype is None or encoding is not None:
- ctype = 'application/octet-stream'
- maintype, subtype = ctype.split('/', 1)
- #Formating accessory data
- data = open(ACCESSORY_FULLPATH, 'rb')
- file_msg = MIMEBase(maintype, subtype)
- file_msg.set_payload(data.read( ))
- data.close( )
- encode_base64(file_msg)
- #file_msg["Content-Type"] = ctype # if add type then return error 10054
- file_msg.add_header('Content-Disposition', 'attachment', filename = ACCESSORY_NAME)
- msg.attach(file_msg)
def getattachment(msg): ctype, encoding = mimetypes.guess_type(ACCESSORY_FULLPATH) if ctype is None or encoding is not None: ctype = 'application/octet-stream' maintype, subtype = ctype.split('/', 1) #Formating accessory data data = open(ACCESSORY_FULLPATH, 'rb') file_msg = MIMEBase(maintype, subtype) file_msg.set_payload(data.read( )) data.close( ) encode_base64(file_msg) #file_msg["Content-Type"] = ctype # if add type then return error 10054 file_msg.add_header('Content-Disposition', 'attachment', filename = ACCESSORY_NAME) msg.attach(file_msg)附件同样独立为一个函数来创建,这里要注意的是不要指定“Content-Type”类型,否则无法发送邮件。这个问题还没有解决。
以上基本包括了发送邮件的主要几个函数,具体smtplib模块和MIME等内容,资料很多,这里就不详细讲解了,有感兴趣的可以上网search。
- 使用python发邮件
- 使用python 发邮件
- 使用python偷偷发邮件
- python使用qq发邮件
- 企业邮箱使用python发邮件
- 使用python smtp发邮件代码
- python 不使用密码发邮件
- Python 使用SMTP 实现发邮件功能
- python,java使用163邮箱发邮件
- python使用qq邮箱发邮件
- Python使用网易邮箱发邮件
- python 发邮件
- Python 发邮件
- python发邮件
- Python发邮件
- python发邮件实例
- python 发邮件
- 【python】发邮件
- Unity3d 更改鼠标样式
- 几种js实现的动态多文件上传
- 入站第一天,庆祝一下
- 剪贴板监控--C++实现
- 信息安全认证
- 使用python发邮件
- 热帖:加入了美籍以后才知道自己这么中国!
- 进程与线程的一个简单解释
- Create Index语句的Include作用
- JS常用方法
- Cocos2d-x 声音SimpleAudioEngine & 游戏存档 CCUserDefault
- 不使用 +-×÷ 运算符来实现 加减乘除 四项运算
- 认识Warden中的性能指标
- 【树形DP】 HDU 4616 Game