发送邮件分类

来源:互联网 发布:python嵌套shell命令 编辑:程序博客网 时间:2024/05/22 06:42

1.文本文件的发送

#coding=utf-8__author__ = 'mac'#smtplib是python用来发送邮件的模块,email是用来处理邮件消息import smtplibfrom email.mime.text import MIMETextfrom email.header import Header#发送方sender='yangxxxxx@163.com'#接收方receiver='xxxxx@qq.com'#subject邮件主题subject=u'放假通知'#邮件服务器smtpserver='smtp.163.com'username='yangxxxxx'password='xxxxxx'#邮件的内容#MIMEText是邮件内容以文本形式写入,msg=MIMEText(u'你好,请大家关好窗户','plain','utf-8')#邮件主题msg['Subject']=Header(subject,'utf-8')#发送邮件中的发件人msg['From']='Joling<yangxxxxx@163.com>'#发送邮件中的收件人msg['To']="xxxxxx@qq.com"smtp=smtplib.SMTP()smtp.connect(smtpserver)smtp.login(username,password)smtp.sendmail(sender,receiver,msg.as_string())smtp.quit()
2.html文件的发送

#coding=utf-8__author__ = 'mac'#发送HTML形式的邮件,需要emai.mine.text中的MIMEText中的_subtype设置为html,并且_text的内容应该为HTML形式。import smtplibfrom email.mime.text import MIMETextsender='yangxxxxx@163.com'receiver='xxxxx@qq.com'subject=u'html放假了'smtpserver='smtp.163.com'username='yangxxxxx@163.com'password='xxxxxx'#html形式与text形式不一样的就是下面的MIMEText中'plain'改成'html',其它都一样msg=MIMEText(u'''小朋友要出去玩了''','html','utf-8')msg['subject']=subjectmsg['From']='Joling<yangxxxxx@163.com>'msg['To']='xxxxxx@qq.com'smtp=smtplib.SMTP()smtp.connect(smtpserver)smtp.login(username,password)smtp.sendmail(sender,receiver,msg.as_string())smtp.quit()
3.带图片的邮件发送
#coding=utf-8__author__ = 'mac'#发送带图片的邮件是利用email.mine.multipart的MIMEMultipart以及email.mime.image的MIMEImage;import smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImagesender='yangxxxxx@163.com'receiver='xxxxx@qq.com'subject=u'带图片的html邮件'smtpserver='smtp.163.com'username='yangxxxx@163.com'password='xxxxxx'msgRoot=MIMEMultipart('related') #采用related定义内嵌资源的邮件体msgRoot['Subject']=subjectmsgRoot['From']='Joling<yangxxxxx@163.com>'msgRoot['To']='xxxxx@qq.com'msgText=MIMEText('''some Html text and an image.good!''','html','utf-8')msgRoot.attach(msgText)fp=open('/Users/mac/Downloads/fulljoin.png','rb')msgImage=MIMEImage(fp.read())fp.close()msgImage.add_header('Content-ID','')msgRoot.attach(msgImage)smtp=smtplib.SMTP()smtp.connect(smtpserver)smtp.login(username,password)smtp.sendmail(sender,receiver,msgRoot.as_string())smtp.quit()

4.带附件邮件的发送

#coding=utf-8__author__ = 'mac'#发送带附件的邮件是利用email.mime.multipart 的MIMEMultipart 以及 email.mime.image的MIMEImage,#重点是构造邮件头信息import smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextsender='yangxxxxx@163.com'receiver='xxxxx@qq.com'subject=u'带附件邮件发送'smtpserver='smtp.163.com'username='yangxxxxx@163.com'password='xxxxxx'msgRoot=MIMEMultipart('mixed')msgRoot['Subject']=subjectmsgRoot['From']='Joling<yangxxxxxx@163.com>'msgRoot['To']='xxxxxx@qq.com'#构造附件att=MIMEText(open('/Users/mac/Desktop/病假单.pdf','rb').read(),'base64','utf-8')
#在附件上传的成功后,查看请求的headers中的content-type是下面的内容att["Content-Type"]='application/octet-stream'att["Content-Disposition"]='attachment;filename="病假单.pdf"'msgRoot.attach(att)smtp=smtplib.SMTP()smtp.connect(smtpserver)smtp.login(username,password)smtp.sendmail(sender,receiver,msgRoot.as_string())smtp.quit()
5.批量发送邮件

#coding=utf-8__author__ = 'mac'#smtplib是python用来发邮件的模块,email是用来处理邮件消息。#君发邮件的时候需要注意收件人(receiver)的值,它为列表形式:import smtplibfrom email.mime.text import MIMETextsender='yangxxxxx@163.com'receiver=['xxxx@qq.com','xxxx@kii.com','xxxxxx@sina.com']subject=u'批量发送邮件'smtpserver='smtp.163.com'username='yangxxxxx@163.com'password='xxxxx'msg=MIMEText('批量发送邮件','plain','utf-8')msg['Subject']=subjectmsg['From']='xxxxxx@163.com'msg['To']='xxxxxx@qq.com'smtp=smtplib.SMTP()smtp.connect(smtpserver)smtp.login(username,password)smtp.sendmail(sender,receiver,msg.as_string())smtp.quit()

6,发送混合邮件,即有附件,又有文本,又有Html

#coding=utf-8__author__ = 'mac'#smtplib是python用来发送邮件的模块,email是用来处理邮件的消息#混合邮件:里面包含附件,Html形式,不同文本import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartsender='xxxxx@163.com'receiver='xxxxx@qq.com'smtpserver='smtp.163.com'username='yangxxxxxx@163.com'password='xxxxxx'#create message container-the correct MIME type is multipart/alternativemsg=MIMEMultipart('mixed')msg['Subject']=u"混合邮件发送"msg['From']='Jolingaa<xxxxx@163.com>'msg['To']='xxxxx@qq.com'#create the body of message(a plain-text and an HTML version)text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"html = """\Hi! How are you? Here is the link you wanted."""# Record the MIME types of both parts - text/plain and text/html.part1=MIMEText(text,'plain')part2=MIMEText(html,'html')# Attach parts into message container.# According to RFC 2046, the last part of a multipart message, in this case# the HTML message, is best and preferred.msg.attach(part1)msg.attach(part2)#构造附件att=MIMEText(open('/Users/mac/Desktop/病假单.pdf','rb').read(),'base64','utf-8')#查看163邮箱请求,当附件上传成功的是时候headers中Content-Type:application/octet-streamatt["Content-Type"]='application/octet-stream'att["Content-Disposition"]='attachment; filename="病假单.pdf"'msg.attach(att)smtp=smtplib.SMTP()smtp.connect(smtpserver)smtp.login(username,password)smtp.sendmail(sender,receiver,msg.as_string())smtp.quit()