python 七种邮件内容发送方法实例

一、文件形式的邮件

[python]
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msg = MIMEText(‘你好’,’text’,’utf-8′)#中文需参数‘utf-8’,单字节字符不需要
msg[‘Subject’] = Header(subject, ‘utf-8’)
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
[/python]

二、HTML形式的邮件

[python]    
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msg = MIMEText(‘</pre>
<h1>你好</h1>
<pre>’,’html’,’utf-8′)
msg[‘Subject’] = subject
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
[/python]

三、带图片的HTML邮件

[python]    
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msgRoot = MIMEMultipart(‘related’)
msgRoot[‘Subject’] = ‘test message’
msgText = MIMEText(‘<b>Some <i>HTML</i> text</b> and an image.
<img alt="" src="cid:image1" />
good!’,’html’,’utf-8′)
msgRoot.attach(msgText)
fp = open(‘h:\\python\\1.jpg’, ‘rb’)
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header(‘Content-ID’, ”)
msgRoot.attach(msgImage)
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
[/python]

四、带附件的邮件

[python]   
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msgRoot = MIMEMultipart(‘related’)
msgRoot[‘Subject’] = ‘test message’
#构造附件
att = MIMEText(open(‘h:\\python\\1.jpg’, ‘rb’).read(), ‘base64’, ‘utf-8’)
att["Content-Type"] = ‘application/octet-stream’
att["Content-Disposition"] = ‘attachment; filename="1.jpg"’
msgRoot.attach(att)
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
[/oython]

五、群邮件

[python]    
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
sender = ‘***’
receiver = [‘***’,’****’,……]
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msg = MIMEText(‘你好’,’text’,’utf-8′)
msg[‘Subject’] = subject
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
[/python]

六、各种元素都包含的邮件

[python]   
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
# Create message container – the correct MIME type is multipart/alternative.
msg = MIMEMultipart(‘alternative’)
msg[‘Subject’] = "Link"
# Create the body of the 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 <a href="http://www.python.org">link</a> 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(‘h:\\python\\1.jpg’, ‘rb’).read(), ‘base64’, ‘utf-8’)
att["Content-Type"] = ‘application/octet-stream’
att["Content-Disposition"] = ‘attachment; filename="1.jpg"’
msg.attach(att)
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
[/python]

七、基于SSL的邮件

[python]    
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msg = MIMEText(‘你好’,’text’,’utf-8′)#中文需参数‘utf-8’,单字节字符不需要
msg[‘Subject’] = Header(subject, ‘utf-8’)
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
[/python]

 

原创文章,作者:stanley,如若转载,请注明出处:http://www.178linux.com/276

(0)
stanleystanley
上一篇 2015-01-16
下一篇 2015-02-07

相关推荐

  • Centtos7搭建ftp服务

    Centtos7搭建ftp服务 下载安装软件包 yum -y install vsftpd   开启启用ftp服务 systemctl start vsftpd    #设置立即启用该服务 systemctl status vsftpd   #查看该服务当前运行状态 systemctl enable vsftpd   #设置开机自动启用该服务 systemc…

    Python笔记 2018-07-07
  • 制作python模块安装包[原创]

     python的第三方模块越来越丰富,涉及的领域也非常广,如科学计算、图片处理、web应用、GUI开发等。当然也可以将自己写的模块进行打包或发布。一简单的方法是将你的类包直接copy到python的lib目录,但此方式不便于管理与维护,存在多个python版本时会非常混乱。现介绍如何编写setup.py来对一个简单的python模块进行打包。 一、…

    Linux干货 2015-03-27
  • Python 部分知识点总结(六)

    此篇博客只是记录第八周未掌握或不熟悉的知识点,用来加深印象。

    Python笔记 2018-05-02
  • 内置数据结构

    list,tuple,str,bytes,bytearray,set,切片,分装解构,冒泡法

    2018-03-31
  • Python模块及详解(2)

    目录: 一、报表之Excel操作XlsxWriter模块 二、Python与rrdtool的结合模块 三、构建集中式的病毒扫描机制 四、系统批量运维管理器paramiko详解 五、系统批量运维管理器Fabric详解 一、报表之Excel操作XlsxWriter模块 Excel是当今最流行的电子表格处理软件,支持丰富的计算函数及图表,在系统运营方面广泛用于运营…

    2018-01-17
  • Python 部分知识点总结(二)

    此篇博客只是记录第四周未掌握或不熟悉的知识点,用来加深印象。

    Python笔记 2018-03-30

评论列表(2条)

  • Gavin
    Gavin 2015-02-09 10:04

    实用,手工赞!

  • jie7832
    jie7832 2015-02-09 15:08

    很实用。。。谢谢楼主分享