【转】python3 发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)
特别留意群邮件方式,这是工作中用得多的。
附件,HTML,图片,都需要的。
文件形式的邮件
[python] view plain copy
1.#!/usr/bin/env python3
2.#coding: utf-8
3.import smtplib
4.from email.mime.text import MIMEText
5.from email.header import Header
6.
7.sender = '***'
8.receiver = '***'
9.subject = 'python email test'
10.smtpserver = 'smtp.163.com'
11.username = '***'
12.password = '***'
13.
14.msg = MIMEText('你好','plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要
15.msg['Subject'] = Header(subject, 'utf-8')
16.
17.smtp = smtplib.SMTP()
18.smtp.connect('smtp.163.com')
19.smtp.login(username, password)
20.smtp.sendmail(sender, receiver, msg.as_string())
21.smtp.quit()
HTML形式的邮件
[python] view plain copy
1.#!/usr/bin/env python3
2.#coding: utf-8
3.import smtplib
4.from email.mime.text import MIMEText
5.
6.sender = '***'
7.receiver = '***'
8.subject = 'python email test'
9.smtpserver = 'smtp.163.com'
10.username = '***'
11.password = '***'
12.
13.msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8')
14.
15.msg['Subject'] = subject
16.
17.smtp = smtplib.SMTP()
18.smtp.connect('smtp.163.com')
19.smtp.login(username, password)
20.smtp.sendmail(sender, receiver, msg.as_string())
21.smtp.quit()
带图片的HTML邮件
[python] view plain copy
1.#!/usr/bin/env python3
2.#coding: utf-8
3.import smtplib
4.from email.mime.multipart import MIMEMultipart
5.from email.mime.text import MIMEText
6.from email.mime.image import MIMEImage
7.
8.sender = '***'
9.receiver = '***'
10.subject = 'python email test'
11.smtpserver = 'smtp.163.com'
12.username = '***'
13.password = '***'
14.
15.msgRoot = MIMEMultipart('related')
16.msgRoot['Subject'] = 'test message'
17.
18.msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8')
19.msgRoot.attach(msgText)
20.
21.fp = open('h:\\python\\1.jpg', 'rb')
22.msgImage = MIMEImage(fp.read())
23.fp.close()
24.
25.msgImage.add_header('Content-ID', '<image1>')
26.msgRoot.attach(msgImage)
27.
28.smtp = smtplib.SMTP()
29.smtp.connect('smtp.163.com')
30.smtp.login(username, password)
31.smtp.sendmail(sender, receiver, msgRoot.as_string())
32.smtp.quit()
带附件的邮件
[python] view plain copy
1.#!/usr/bin/env python3
2.#coding: utf-8
3.import smtplib
4.from email.mime.multipart import MIMEMultipart
5.from email.mime.text import MIMEText
6.from email.mime.image import MIMEImage
7.
8.sender = '***'
9.receiver = '***'
10.subject = 'python email test'
11.smtpserver = 'smtp.163.com'
12.username = '***'
13.password = '***'
14.
15.msgRoot = MIMEMultipart('related')
16.msgRoot['Subject'] = 'test message'
17.
18.#构造附件
19.att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
20.att["Content-Type"] = 'application/octet-stream'
21.att["Content-Disposition"] = 'attachment; filename="1.jpg"'
22.msgRoot.attach(att)
23.
24.smtp = smtplib.SMTP()
25.smtp.connect('smtp.163.com')
26.smtp.login(username, password)
27.smtp.sendmail(sender, receiver, msgRoot.as_string())
28.smtp.quit()
群邮件
[python] view plain copy
1.#!/usr/bin/env python3
2.#coding: utf-8
3.import smtplib
4.from email.mime.text import MIMEText
5.
6.sender = '***'
7.receiver = ['***','****',……]
8.subject = 'python email test'
9.smtpserver = 'smtp.163.com'
10.username = '***'
11.password = '***'
12.
13.msg = MIMEText('你好','plain','utf-8')
14.
15.msg['Subject'] = subject
16.
17.smtp = smtplib.SMTP()
18.smtp.connect('smtp.163.com')
19.smtp.login(username, password)
20.smtp.sendmail(sender, receiver, msg.as_string())
21.smtp.quit()
各种元素都包含的邮件
[python] view plain copy
1.#!/usr/bin/env python3
2.#coding: utf-8
3.import smtplib
4.from email.mime.multipart import MIMEMultipart
5.from email.mime.text import MIMEText
6.from email.mime.image import MIMEImage
7.
8.sender = '***'
9.receiver = '***'
10.subject = 'python email test'
11.smtpserver = 'smtp.163.com'
12.username = '***'
13.password = '***'
14.
15.# Create message container - the correct MIME type is multipart/alternative.
16.msg = MIMEMultipart('alternative')
17.msg['Subject'] = "Link"
18.
19.# Create the body of the message (a plain-text and an HTML version).
20.text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
21.html = """\
22.<html>
23. <head></head>
24. <body>
25. <p>Hi!<br>
26. How are you?<br>
27. Here is the <a href="http://www.python.org">link</a> you wanted.
28. </p>
29. </body>
30.</html>
31."""
32.
33.# Record the MIME types of both parts - text/plain and text/html.
34.part1 = MIMEText(text, 'plain')
35.part2 = MIMEText(html, 'html')
36.
37.# Attach parts into message container.
38.# According to RFC 2046, the last part of a multipart message, in this case
39.# the HTML message, is best and preferred.
40.msg.attach(part1)
41.msg.attach(part2)
42.#构造附件
43.att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
44.att["Content-Type"] = 'application/octet-stream'
45.att["Content-Disposition"] = 'attachment; filename="1.jpg"'
46.msg.attach(att)
47.
48.smtp = smtplib.SMTP()
49.smtp.connect('smtp.163.com')
50.smtp.login(username, password)
51.smtp.sendmail(sender, receiver, msg.as_string())
52.smtp.quit()
基于SSL的邮件
[python] view plain copy
1.#!/usr/bin/env python3
2.#coding: utf-8
3.import smtplib
4.from email.mime.text import MIMEText
5.from email.header import Header
6.sender = '***'
7.receiver = '***'
8.subject = 'python email test'
9.smtpserver = 'smtp.163.com'
10.username = '***'
11.password = '***'
12.
13.msg = MIMEText('你好','plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要
14.msg['Subject'] = Header(subject, 'utf-8')
15.
16.smtp = smtplib.SMTP()
17.smtp.connect('smtp.163.com')
18.smtp.ehlo()
19.smtp.starttls()
20.smtp.ehlo()
21.smtp.set_debuglevel(1)
22.smtp.login(username, password)
23.smtp.sendmail(sender, receiver, msg.as_string())
24.smtp.quit()


【转】python3 发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)的更多相关文章
- python3 练手实例8 批量命名图片
#coding:utf-8 import os import tkinter as tk from tkinter import filedialog root = tk.Tk() root.with ...
- 基于Java Mail 进行发送(带附件和压缩附件)的邮件
刚进公司的training, 下面是要求: Self-study of Java Mail library: http://www.oracle.com/technetwork/java/javam ...
- C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)
我们知道,企业号主要是面向企业需求而生的,因此内部消息的交流显得非常重要,而且发送.回复消息数量应该很可观,对于大企业尤其如此,因此可以结合企业号实现内部消息的交流.企业号具有关注安全.消息无限制等特 ...
- Java 在Word中创建邮件合并模板并合并文本和图片
Word里面的邮件合并功能是一种可以快速批量操作同类型数据的方式,常见的如数据填充.打印等.其中必不可少的步骤包括用于填充的模板文档.填充的数据源以及实现邮件合并的功能.下面,通过Java程序展示如何 ...
- 【原】移动web页面给用户发送邮件的方法 (邮件含文本、图片、链接)
微信商户通有这么一个需求,用户打开H5页面后,引导用户到电脑下载设计资源包,由于各种内部原因,被告知无后台资源支持,自己折腾了一段时间找了下面2个办法,简单做下笔记. 使用mailto功能,让用户自己 ...
- C# 创建邮件合并模板并合并文本、图片
对于Word中的邮件合并功能,用户可以将邮件合并后的结果文档保存并打印,也可以通过邮件的形式发送,在很多场合需要使用到此功能.那对于编程人员,我们也可以在C#语言环境中通过代码的形式来实现.根据需要先 ...
- contents() 查找匹配元素内部所有的子节点(包括文本节点)。如果元素是一个iframe,则查找文档内容
contents() V1.2概述 查找匹配元素内部所有的子节点(包括文本节点).如果元素是一个iframe,则查找文档内容 示例 描述:大理石平台检定规程 查找所有文本节点并加粗 HTML 代码 ...
- C# 如何添加Word文本和图片超链接
超链接简单来讲就是内容链接,通过设置超链接可以实现对象与网页.站点之间的连接.链接目标可以是网页.图片.邮件地址.文件夹或者是应用程序.设置链接的对象可以是文本或者图片. 在以下内容中,我将介绍如何用 ...
- 用Python教你微信防撤回(文本、图片、语音、视频、名片等...)
大家在使用微信过程中,有时候消息还没看到,就被撤回了.毕竟好奇心大家都有,明知到消息被撤回了,就更想去看一下是什么内容心里想着万一是女神给我表白了呢.. 今天就用Python来做个微信防撤回的小功能. ...
随机推荐
- struts2标签汇总
要在jsp中使用Struts2的标志,先要指明标志的引入.通过jsp的代码的顶部加入以下的代码:<%@taglib prefix="s" uri="/struts- ...
- mvvm框架下页面与ViewModel的各种参数传递方式
传单个参数的话在xaml用 Command={Binding ViewModel的事件处理名称} CommandParameter={Binding 要传递的控件名称} ViewMode ...
- HDU2054JAVA
(包括2016级新生)除了校赛,还有什么途径可以申请加入ACM校队? A == B ? Time Limit: 1000/1000 MS (Java/Others) Memory Limit: ...
- Java基础知识强化之网络编程笔记06:TCP之TCP协议发送数据 和 接收数据
1. TCP协议发送数据 和 接收数据 TCP协议接收数据:• 创建接收端的Socket对象• 监听客户端连接.返回一个对应的Socket对象• 获取输入流,读取数据显示在控制台• 释放资源 TCP协 ...
- [图文]centos6.3搭建FTP服务器教程
我一开始是参照这个教程做的 http://www.linuxren.net/better/centos63-ftp.html 可是问题总是免不了的,我遇到几个问题. 一开始使用terminal的时候一 ...
- google code 上传源码
在使用google code 的时候 做个备份, git clone https://wushuangzilong@code.google.com/p/maplebanana-proxy/ git c ...
- JSP include HTML出现乱码
解决方法:在项目的web.xml中加入下面语句:<jsp-config> <jsp-property-group> <description> ...
- storm简介[ZZ]
场景 伴随着信息科技日新月异的发展,信息呈现出爆发式的膨胀,人们获取信息的途径也更加多样.更加便捷,同时对于信息的时效性要求也越来越高.举个搜索 场景中的例子,当一个卖家发布了一条宝贝信息时,他希望的 ...
- linux 的一个防火墙策略
#警告:在运行脚本后,勿必单独运行 iptables -F#因为脚本包含的默认规则为“禁止所有访问”#当其它规则被清除后,系统表现为无法访问状态,需要重启系统恢复#iptables -L 查看当前已应 ...
- Centos 6 设置静态 IP 地址
编辑文件进行IP设置: [root@test1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 原文件内容前面加#被注释掉 下面为新加的内容 #DEV ...