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笔记 2018-05-03
  • python学习总结

    第一个项目日志分析。(存在不足)

    Python笔记 2018-05-06
  • 几个python运维面试题及答案

    1.生成随机的20个ID ID格式要求: 时间戳_三位随机数字号码_8位随机小写字母 1506571959_089_xxkeabef datetime.datetime.now().timestamp() 返回的是时间戳,但是带微秒 2.判断密码强弱 要求密码必须由 10-15位 指定字符组成: 十进制数字 大写字母 小写字母 下划线 要求四种类型的字符都要…

    2017-10-02
  • Python面向对象基础

    语言分类 面向机器 抽象成机器指令,让机器容易理解 代表:汇编语言 面向过程 按照步骤一步一步走,若出现情况A做相应的处理,若出现情况B做相应的处理 问题规模小,可以步骤化,按部就班处理 代表:C 面向对象OOP 计算机需要处理的问题的规模越来越大,需要多人、多部门协作 代表:C++、Java、Python 面向对象 一种认识世界、分析世界的方法论。将万事万…

    2018-05-06
  • 元组冒泡法及字符串处理

    tuple buble string

    2017-09-23
  • Python基础练习之set/dict练习

    1.用户输入一个数字 打印每一位数字及其重复的次数 (1)字符串练习2用的方法 while True: num = input().strip().lstrip(‘0’) if num.isdigit(): break count = [0] * 10 for j in num: x = int(j) if count[x] == 0: count[x] =…

    2017-10-09

评论列表(2条)

  • Gavin
    Gavin 2015-02-09 10:04

    实用,手工赞!

  • jie7832
    jie7832 2015-02-09 15:08

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