python SendMail 发送邮件

发布时间:2017-2-21 4:07:23 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"python SendMail 发送邮件",主要涉及到python SendMail 发送邮件方面的内容,对于python SendMail 发送邮件感兴趣的同学可以参考一下。

python SendMail 发送邮件

  最近在学习python 时,用到了发送邮件的操作,通过整理总结如下:

1、普通文本邮件

  普通文本邮件发送的实现,关键是要将MIMEText中_subtype设置为plain,首先导入smtplib和mimetext,创建smtplib.smtp实例,connect邮件smtp服务器,login后发送,具体代码如下:(python2.7下实现)

具体代码如下:

# 发送普通邮件
# mail_to:接收者邮件,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
# mail_subject: 邮件主题:
# mail_body:邮件体内容
# mail_cc:抄送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
# mail_bcc:密送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
def SendMail(self,mail_to,mail_subject,mail_body,mail_cc="",mail_bcc=""):

message = MIMEText(mail_body, _subtype='plain', _charset='utf-8')
message['Subject'] = mail_subject
message['From'] = self.mail_sender
if len(mail_cc) > 0:
message['Cc'] = mail_cc
if len(mail_bcc) > 0:
message['Bcc'] = mail_bcc

try:
smtpObj = smtplib.SMTP()
smtpObj.connect(self.mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(self.mail_user,self.mail_pass)
smtpObj.sendmail(self.mail_sender, mail_to, message.as_string())
smtpObj.close()
print "邮件发送成功"
except smtplib.SMTPException, e:
print "Error: 无法发送邮件", e


2、html邮件的发送

    与text邮件不同之处就是将将MIMEText中_subtype设置为html。具体代码如下:(python2.7下实现)

# 发送Html邮件
# mail_to:接收者邮件,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
# mail_subject: 邮件主题:
# mail_body:邮件体内容
# mail_cc:抄送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
# mail_bcc:密送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
def SendHtmlMail(self,mail_to,mail_subject,mail_body,mail_cc="",mail_bcc=""):
message = MIMEText(mail_body, _subtype='html', _charset='gb2312')
message['Subject'] = mail_subject
message['From'] = self.mail_sender
if len(mail_cc) > 0:
message['Cc'] = mail_cc
if len(mail_bcc) > 0:
message['Bcc'] = mail_bcc

try:
smtpObj = smtplib.SMTP()
smtpObj.connect(self.mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(self.mail_user, self.mail_pass)
smtpObj.sendmail(self.mail_sender, mail_to, message.as_string())
smtpObj.close()
print "邮件发送成功"
except smtplib.SMTPException, e:
print "Error: 无法发送邮件", e

  3、发送带附件的邮件

    发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后利用smtplib.smtp发送。

  

#发送带附件的邮件
#mail_to:接收者邮件,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
#mail_subject: 邮件主题:
#mail_body:邮件体内容
#fileList:附件列表,就文件名列表(包含路径)
#mail_cc:抄送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
#mail_bcc:密送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
def SendMailAttach(self,mail_to,mail_subject,mail_body,fileList,mail_cc="",mail_bcc=""):
msg = MIMEMultipart()
message = MIMEText(mail_body, _subtype='plain', _charset='utf-8')
msg.attach(message)

# 构造附件
for f in fileList:
if os.path.isfile(f):
att = MIMEText(open(f, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment;filename=' + os.path.basename(f)
msg.attach(att)

msg['Subject'] = mail_subject
msg['From'] = self.mail_sender
if len(mail_cc) > 0:
msg['Cc'] = mail_cc
if len(mail_bcc) > 0:
msg['Bcc'] = mail_bcc

try:
smtpObj = smtplib.SMTP()
smtpObj.connect(self.mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(self.mail_user, self.mail_pass)
smtpObj.sendmail(self.mail_sender, mail_to, msg.as_string())
smtpObj.close()
print "邮件发送成功"
except smtplib.SMTPException, e:
print "Error: 无法发送邮件", e

4、利用MIMEimage发送图片
def SendMailImage(self, mail_to, mail_subject, mail_body, mail_cc="", mail_bcc=""):
msg = MIMEMultipart()
message = MIMEText(mail_body, _subtype='plain', _charset='utf-8')
msg.attach(message)

# 构造附件
file1 = r"D:\AAA.png"
if os.path.isfile(file1):
image = MIMEImage(open(file1, 'rb').read())
image["Content-Type"] = 'application/octet-stream'
image["Content-Disposition"] = 'attachment;filename=AAA.png'
image.add_header('Content-ID', '<image1>')
msg.attach(image)

msg['Subject'] = mail_subject
msg['From'] = self.mail_sender
if len(mail_cc) > 0:
msg['Cc'] = mail_cc
if len(mail_bcc) > 0:
msg['Bcc'] = mail_bcc

try:
smtpObj = smtplib.SMTP()
smtpObj.connect(self.mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(self.mail_user, self.mail_pass)
smtpObj.sendmail(self.mail_sender, mail_to, msg.as_string())
smtpObj.close()
print "邮件发送成功"
except smtplib.SMTPException, e:
print "Error: 无法发送邮件",

完整的代码如下:
# -*- coding: UTF-8 -*-
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

class Mail:
'''
self.mail_host = "smtp.sina.com" # 设置服务器
self.mail_user = "xiaowang" # 用户名
self.mail_pass = "XXXXX" # 口令
self.mail_sender = 'xiaowang@sina.com' # 发送者
'''
def __init__(self,mail_host,mail_user,mail_pass,mail_sender):
# 第三方 SMTP 服务
self.mail_host = mail_host
self.mail_user = mail_user
self.mail_pass = mail_pass
self.mail_sender = mail_sender

# 发送普通邮件
# mail_to:接收者邮件,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
# mail_subject: 邮件主题:
# mail_body:邮件体内容
# mail_cc:抄送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
# mail_bcc:密送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
def SendMail(self,mail_to,mail_subject,mail_body,mail_cc="",mail_bcc=""):

message = MIMEText(mail_body, _subtype='plain', _charset='utf-8')
message['Subject'] = mail_subject
message['From'] = self.mail_sender
if len(mail_cc) > 0:
message['Cc'] = mail_cc
if len(mail_bcc) > 0:
message['Bcc'] = mail_bcc

try:
smtpObj = smtplib.SMTP()
smtpObj.connect(self.mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(self.mail_user,self.mail_pass)
smtpObj.sendmail(self.mail_sender, mail_to, message.as_string())
smtpObj.close()
print "邮件发送成功"
except smtplib.SMTPException, e:
print "Error: 无法发送邮件", e

# 发送Html邮件
# mail_to:接收者邮件,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
# mail_subject: 邮件主题:
# mail_body:邮件体内容
# mail_cc:抄送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
# mail_bcc:密送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
def SendHtmlMail(self,mail_to,mail_subject,mail_body,mail_cc="",mail_bcc=""):
message = MIMEText(mail_body, _subtype='html', _charset='gb2312')
message['Subject'] = mail_subject
message['From'] = self.mail_sender
if len(mail_cc) > 0:
message['Cc'] = mail_cc
if len(mail_bcc) > 0:
message['Bcc'] = mail_bcc

try:
smtpObj = smtplib.SMTP()
smtpObj.connect(self.mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(self.mail_user, self.mail_pass)
smtpObj.sendmail(self.mail_sender, mail_to, message.as_string())
smtpObj.close()
print "邮件发送成功"
except smtplib.SMTPException, e:
print "Error: 无法发送邮件", e


#发送带附件的邮件
#mail_to:接收者邮件,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
#mail_subject: 邮件主题:
#mail_body:邮件体内容
#fileList:附件列表,就文件名列表(包含路径)
#mail_cc:抄送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
#mail_bcc:密送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
def SendMailAttach(self,mail_to,mail_subject,mail_body,fileList,mail_cc="",mail_bcc=""):
msg = MIMEMultipart()
message = MIMEText(mail_body, _subtype='plain', _charset='utf-8')
msg.attach(message)

# 构造附件
for f in fileList:
if os.path.isfile(f):
att = MIMEText(open(f, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment;filename=' + os.path.basename(f)
msg.attach(att)

msg['Subject'] = mail_subject
msg['From'] = self.mail_sender
if len(mail_cc) > 0:
msg['Cc'] = mail_cc
if len(mail_bcc) > 0:
msg['Bcc'] = mail_bcc

try:
smtpObj = smtplib.SMTP()
smtpObj.connect(self.mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(self.mail_user, self.mail_pass)
smtpObj.sendmail(self.mail_sender, mail_to, msg.as_string())
smtpObj.close()
print "邮件发送成功"
except smtplib.SMTPException, e:
print "Error: 无法发送邮件", e
  
  #发送带图片附件的邮件
#mail_to:接收者邮件,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
#mail_subject: 邮件主题:
#mail_body:邮件体内容
#mail_cc:抄送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
#mail_bcc:密送邮件,默认为空,多个接受者中间以分号隔开,如:'xiaoli@sina.com;xiaoMa@qq.com'
    def SendMailImage(self, mail_to, mail_subject, mail_body, mail_cc="", mail_bcc=""):
msg = MIMEMultipart()
message = MIMEText(mail_body, _subtype='plain', _charset='utf-8')
msg.attach(message)

# 构造附件
file1 = r"D:\AAA.png"
if os.path.isfile(file1):
image = MIMEImage(open(file1, 'rb').read())
image["Content-Type"] = 'application/octet-stream'
image["Content-Disposition"] = 'attachment;filename=AAA.png'
image.add_header('Content-ID', '<image1>')
msg.attach(image)

msg['Subject'] = mail_subject
msg['From'] = self.mail_sender
if len(mail_cc) > 0:
msg['Cc'] = mail_cc
if len(mail_bcc) > 0:
msg['Bcc'] = mail_bcc

try:
smtpObj = smtplib.SMTP()
smtpObj.connect(self.mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(self.mail_user, self.mail_pass)
smtpObj.sendmail(self.mail_sender, mail_to, msg.as_string())
smtpObj.close()
print "邮件发送成功"
except smtplib.SMTPException, e:
print "Error: 无法发送邮件", e

def main():
mail_host = "smtp.sina.com" # 设置服务器
mail_user = "xiaowang" # 用户名
mail_pass = "XXXXX" # 口令
mail_sender = 'xiaowang@sina.com' # 发送者

mm = Mail(mail_host, mail_user, mail_pass, mail_sender)

fileList = []
fileList.append(r'd:\myapp.txt')
fileList.append(r'd:\test.txt')

mail_to = 'xiaoli@sina.com;310648026@qq.com'
mail_cc = 'ma@163.com'

# mail_bcc = 'wan@163.com'
mail_subject = "内容"
mail_body = '邮件体111'

mm.SendMail(mail_to, mail_subject, mail_body,mail_cc)
# mm.SendMailImage(mail_to, mail_subject, mail_body,mail_cc)
# mm.SendMailAttach(mail_to, mail_subject, mail_body, fileList,mail_cc)

if __name__== '__main__':
main()

上一篇:OperateLoger
下一篇:在linux下使用curl访问 多参数url GET参数问题

相关文章

相关评论