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()

二、HTML形式的邮件

#!/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()

三、带图片的HTML邮件

#!/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  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()

四、带附件的邮件

#!/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()

五、群邮件

#!/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()

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

#!/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()

七、基于SSL的邮件

#!/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()
时间: 2024-08-25 04:01:54

python 七种邮件内容发送方法实例的相关文章

Python发送邮件(常见四种邮件内容)

Python发送邮件(常见四种邮件内容) 转载 2017年03月03日 17:17:04 转自:http://lizhenliang.blog.51cto.com/7876557/1875330 在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下,Shell脚本发送邮件告警是件很简单的事,有现成的邮件服务软件或者调用运营商邮箱服务器. 对于Python来说,需要编写脚本调用邮件服务器来发送邮件,使用的协议是SMTP.

【转】【Python】Python发送邮件(常见四种邮件内容)

感谢:梦琪小生的<[转][Python]Python发送邮件(常见四种邮件内容)> 里面详细介绍了Python中发送邮件的方法,以供自己参考 原文地址:https://www.cnblogs.com/Owen-ET/p/8423168.html

【转】python 三种遍历list的方法

[转]python 三种遍历list的方法 #!/usr/bin/env python # -*- coding: utf-8 -*- if __name__ == '__main__': list = ['html', 'js', 'css', 'python'] # 方法1 print '遍历列表方法1:' for i in list: print ("序号:%s 值:%s" % (list.index(i) + 1, i)) print '\n遍历列表方法2:' # 方法2 fo

邮件内容加密方法-如何让你的邮件隐密邮

早期邮件在互联网上都是以明文传输的,很容易就被窃取,后来邮件通讯使用了SSL通道才保证了邮件在链路上的安全,但是邮件在邮件服务器上的安全一直以来都很难解决,即使是今天依然有***可以将用户的邮件从服务器上窃取出来.所以邮件内容的安全目前是邮件安全的重点,既然是内容安全就要使用加密技术了,如果邮件再服务器上是密文存储的那么即使邮件被窃取下来也不用太担心,前提是你使用的加密技术够强.那么目前都有哪些常用的加密技术用于邮件中呢? 第一种:利用对称加密算法加密邮件 对称加密算法是应用较早的加密算法,技术

JS_七种JAVASCRIPT加密/解密方法

本文一共介绍了七种JAVASCRIPT加密方法. 一:最简单的加密解密 二:转义字符的妙用 三:使用Microsoft出品的脚本编码器Script Encoder来进行编码 (自创简单解码) 四:任意添加NUL空字符(十六进制00H) (自创) 五:无用内容混乱以及换行空格TAB大法 六:自写解密函数法 七:错误的利用 在做网页时(其实是网页木马呵呵),最让人烦恼的是自己辛辛苦苦写出来的客户端IE运行的JAVASCRIPT代码常常被别人轻易的拷贝,实在让自己的心里有点不是滋味,要知道自己写点东西

python两种生成md5的方法

一. 使用md5包 import md5 src = 'this is a md5 test.' m1 = md5.new() m1.update(src) print m1.hexdigest() 二. 使用hashlib import hashlib m2 = hashlib.md5() m2.update(src) print m2.hexdigest() 推荐使用第二种方法.

ajax请求中 两种csrftoken的发送方法

通过ajax的方式发送两个数据进行加法运算 html页面 <body> <h3>index页面 </h3> <input type="text" name="cal_1">+ <input type="text" name="cal_2">= <input type="text" name="cal_3"> <

Python几种数据结构内置方法的时间复杂度

参考:https://blog.csdn.net/baoli1008/article/details/48059623 注:下文中,'n'代表容器中元素的数量,'k'代表参数的值,或者参数的数量. 1.列表(list) 以完全随机的列表考虑平均情况. 列表是以数组(Array)实现的. 最大的开销发生在超过当前分配大小的增长,这种情况下所有元素都需要移动:或者是在起始位置附近插入或者删除元素,这种情况下所有在该位置后面的元素都需要移动. 如果需要在一个队列的两端进行增删的操作,应当使用colle

python四种列表的插入方法及其效率

coding:utf-8 from timeit import timeit def count_append(): lists = [] for x in range(0, 100000): lists.append(x) cost_time = timeit(stmt=count_append, number=1) print("append花费的时间是", cost_time) def count_extend(): lists = [] for x in range(0, 10