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

相关推荐

  • Python文件操作

    计算机体系架构 运算器:完成各种算术运算、逻辑运算、出具传输等数据加工处理 控制器:控制程序的执行 CPU = 运算器 + 控制器 存储器:用于记忆程序的数据,内存 输入设备:将数据或程序输入到计算机中 输出设备:将数据或程序的处理结果展示给用户 文件IO常用操作 open 打开 read 读取 write 写入 close 关闭 readline 行读取 …

    Python笔记 2018-05-02
  • python快速入门之数据类型

        Python 是 90 年代初由 Guido Van Rossum 为了打发圣诞节而创建的语言。如今它已是当前最流行的程序语言之一.     Python的关键要素有以下几点:1.基本数据类型;2.对象引用;3.组合数据类型;4.逻辑操作符;5.控制流语句;6.算数操作符…

    Python干货 2015-12-10
  • Python内置数据结构——字符串

    知识结构图 学习笔记 字符串 字符组成的有序序列,字符的集合 使用单引号、双引号、三引号引起来的字符序列 不可变对象 Unicode类型 定义 单引号、双引号、三引号 r、R:引号内的字符原样输出 元素访问 索引访问 可迭代 join连接 “string“.join(iteratable) 使用string作为分隔符将可迭代对象连接起…

    2018-03-31
  • Python函数

    函数 数学函数 Python函数 若干语句块、函数名称、参数列表构成,组织代码的最小单元 完成一定的功能 作用 结构化编程对代码的最基本的封装,一般按照功能组织一段代码 复用,减少冗余代码 简洁美观,可读易懂 函数分类 内建函数,max()、reversed() 库函数,math.ceil() 函数定义、调用 def语句定义函数 def 函数名(参数列表):…

    2018-04-16
  • python基础总结

    编程基础 一组能让计算机识别和执行的指令   电子计算机 能够执行程序的机器   现代计算机 英国数学家、逻辑学家,艾伦.麦席森.图灵被称为计算机科学之父,人工智能之父。图灵提出的著名的图灵模型为现代计算机的逻辑工作方式奠定了基础   图灵机,又称图灵计算机、图灵计算机,由图灵提出的一种抽象计算机模型,即将人们使用纸笔进行数学运…

    2017-09-17
  • 类的继承

    Edit 类的继承 基本概念 面向对象三要素之一,继承Inheritance 举例: 人类和猫类都继承自动物类 个体继承自父母,继承了父母的一部分特征,但也可以有自己的个性 在面向对象的世界中,以父类继承,就可以直接拥有父类的属性和方法,这样可以减少代码、多复用。子类可以定义自己的属性和方法 class Animal: def shout(self): pr…

    2017-11-15

评论列表(2条)

  • Gavin
    Gavin 2015-02-09 10:04

    实用,手工赞!

  • jie7832
    jie7832 2015-02-09 15:08

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