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基础练习之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
  • python数据类型及其函数方法归纳总结

    数据类型的分类: ֺ数值型: int ,float, complex, bool     序列对象: 字符串,列表,元组tuple     键值型 集合set,字典dict 如果按照可hash性来分类 hashable(可哈希性) An object is hashable if it has a hash value which never changes…

    2017-10-09
  • Python第十二周学习总结

    并行,并发,多线程

    2018-05-27
  • 程序员如何在小公司成长和大公司学习-python

    这篇文章会带有普遍性,不见得适合所有人,或者文章所描述的也不见得是对的,只是根据我的经历和所见写成的一篇文章,仅供参考。 前言: 在软件行业工作有几个年头了,换过多个开发语言,跳过槽,也被猎头找过,经历了三五杆枪打天下和创业公司一起成长灭亡,也进入了大公司。在这些年的历练中,看见很多刚进入软件行业不久的新人总在抱怨,说学不到东西。其实不管你在大公司还是小公司…

    Python干货 2015-03-16
  • python基础总结

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

    2017-09-17

评论列表(2条)

  • Gavin
    Gavin 2015-02-09 10:04

    实用,手工赞!

  • jie7832
    jie7832 2015-02-09 15:08

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