详解Python发送email的三种方式


Posted in Python onOctober 18, 2018

Python发送email的三种方式,分别为使用登录邮件服务器、使用smtp服务、调用sendmail命令来发送三种方法

Python发送email比较简单,可以通过登录邮件服务来发送,linux下也可以使用调用sendmail命令来发送,还可以使用本地或者是远程的smtp服务来发送邮件,不管是单个,群发,还是抄送都比较容易实现。本米扑博客先介绍几个最简单的发送邮件方式记录下,像html邮件,附件等也是支持的,需要时查文档即可。

一、登录邮件服务器

通过smtp登录第三方smtp邮箱发送邮件,支持 25 和 465端口

vim python_email_1.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# author: mimvp.com
# 2015.10.05
 
 
import smtplib 
from email.mime.text import MIMEText 
  
smtpHost = 'smtp.exmail.qq.com' 
sender = 'robot@mimvp.com' 
password = "mimvp-password" 
receiver = 'yanggang@mimvp.com'
  
content = 'hello mimvp.com' 
msg = MIMEText(content) 
  
msg['Subject'] = 'email-subject' 
msg['From'] = sender 
msg['To'] = receiver 
  
## smtp port 25
smtpServer = smtplib.SMTP(smtpHost, 25)     # SMTP
smtpServer.login(sender, password) 
smtpServer.sendmail(sender, receiver, msg.as_string()) 
smtpServer.quit() 
print 'send success by port 25' 
 
## smtp ssl port 465
smtpServer = smtplib.SMTP_SSL(smtpHost, 465)  # SMTP_SSL
smtpServer.login(sender, password) 
smtpServer.sendmail(sender, receiver, msg.as_string()) 
smtpServer.quit() 
print 'send success by port 465'

执行命令:

$ python python_email_1.py 
send success by port 25
send success by port 465

发送结果,会收到两封邮件,截图其中一份邮件如下图:

详解Python发送email的三种方式

二、使用smtp服务

测试失败,略过或留言指正

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# author: mimvp.com
# 2015.10.05
 
 
import smtplib 
from email.mime.text import MIMEText 
import subprocess
  
smtpHost = 'smtp.exmail.qq.com' 
sender = 'robot@mimvp.com' 
password = "mimvp-password" 
receiver = 'yanggang@mimvp.com'
  
content = 'hello mimvp.com' 
msg = MIMEText(content)  
  
  
 
if __name__ == "__main__":  
  p = subprocess.Popen(['/usr/sbin/sendmail', '-t'], stdout=subprocess.PIPE) 
  print(str(p.communicate()))
  p_res = str(p.communicate()[0])
  msg = MIMEText(p_res)
 
  msg["From"] = sender 
  msg["To"] = receiver 
  msg["Subject"] = "hello mimvp.com" 
  s = smtplib.SMTP(smtpHost) 
  s.login(sender, password)
  s.sendmail(sender, receiver, msg.as_string()) 
  s.quit() 
  print 'send success'

三、调用sendmail命令

调用本机linux自身sendmail服务发送邮件,不需要启动sendmail后台进程,不需要发送者登录,邮件发送者可以是任意名字,没有限制。

特别注意:sendmail 命令发送邮件,默认用25端口号,由于阿里云、腾讯云等封禁了25端口号,因此本示例需在开通25端口机器上测试

vim python_email_3.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# author: mimvp.com
# 2015.10.05
 
 
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
import commands
 
import sys 
reload(sys)
sys.setdefaultencoding('utf-8')
 
def send_mail(sender, recevier, subject, html_content):
    msg = MIMEText(html_content, 'html', 'utf-8')
    msg["From"] = sender
    msg["To"] = recevier
    msg["Subject"] = subject
    p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
    p.communicate(msg.as_string())
 
 
sender = 'robot@mimvp.com'
recevier = 'yanggang@mimvp.com'
subject = 'sendmail-subject'
html_content = 'hello mimvp.com'
send_mail(sender, recevier, subject, html_content)

执行命令:

python python_email_3.py

收件结果:

详解Python发送email的三种方式

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python Mysql数据库操作 Perl操作Mysql数据库
Jan 12 Python
python2 与python3的print区别小结
Jan 16 Python
python使用response.read()接收json数据的实例
Dec 19 Python
pandas 把数据写入txt文件每行固定写入一定数量的值方法
Dec 28 Python
python基于Selenium的web自动化框架
Jul 14 Python
python Manager 之dict KeyError问题的解决
Dec 21 Python
基于Python3.7.1无法导入Numpy的解决方式
Mar 09 Python
使用pygame编写Flappy bird小游戏
Mar 14 Python
python MultipartEncoder传输zip文件实例
Apr 07 Python
pycharm设置默认的UTF-8编码模式的方法详解
Jun 01 Python
python excel和yaml文件的读取封装
Jan 12 Python
教你怎么用PyCharm为同一服务器配置多个python解释器
May 31 Python
python try except 捕获所有异常的实例
Oct 18 #Python
对Python中Iterator和Iterable的区别详解
Oct 18 #Python
对python中的iter()函数与next()函数详解
Oct 18 #Python
对Python 3.2 迭代器的next函数实例讲解
Oct 18 #Python
对python中的高效迭代器函数详解
Oct 18 #Python
对Python中内置异常层次结构详解
Oct 18 #Python
Python运维开发之psutil库的使用详解
Oct 18 #Python
You might like
作为PHP程序员应该了解MongoDB的五件事
2013/06/03 PHP
ThinkPHP模版引擎之变量输出详解
2014/12/05 PHP
简单谈谈PHP中的include、include_once、require以及require_once语句
2016/04/23 PHP
PHP实现UTF8二进制及明文字符串的转化功能示例
2017/11/20 PHP
window.open被浏览器拦截后的自定义提示效果代码
2007/11/19 Javascript
教您去掉ie网页加载进度条的方法
2010/12/09 Javascript
利用JS延迟加载百度分享代码,提高网页速度
2013/07/01 Javascript
js中传递特殊字符(+,&)的方法
2014/01/16 Javascript
js计算德州扑克牌面值的方法
2015/03/04 Javascript
jQuery实现图片轮播效果代码(基于jquery.pack.js插件)
2016/06/02 Javascript
把普通对象转换成json格式的对象的简单实例
2016/07/04 Javascript
微信小程序 switch组件详解及简单实例
2017/01/10 Javascript
JavaScript中捕获与冒泡详解及实例
2017/02/03 Javascript
Angular 4依赖注入学习教程之InjectToken的使用(八)
2017/06/04 Javascript
微信小程序动态添加分享数据
2017/06/14 Javascript
从parcel.js打包出错到选择nvm的全部过程
2018/01/23 Javascript
在Vue组件中使用 TypeScript的方法
2018/02/28 Javascript
vue.js实现简单购物车功能
2020/05/30 Javascript
python数组复制拷贝的实现方法
2015/06/09 Python
Python实现删除文件但保留指定文件
2015/06/21 Python
Python正则抓取网易新闻的方法示例
2017/04/21 Python
itchat和matplotlib的结合使用爬取微信信息的实例
2017/08/25 Python
对web.py设置favicon.ico的方法详解
2018/12/04 Python
python网络编程 使用UDP、TCP协议收发信息详解
2019/08/29 Python
python 三元运算符使用解析
2019/09/16 Python
Python3 Tensorlfow:增加或者减小矩阵维度的实现
2020/05/22 Python
python中_del_还原数据的方法
2020/12/09 Python
GOOD AMERICAN官网:为曲线性感而设计
2017/12/28 全球购物
巴西最好的男鞋:Rafarillo
2018/05/25 全球购物
英国领先的独立酒精饮料零售商:DrinkSupermarket
2021/01/13 全球购物
介绍一下linux的文件权限
2012/02/15 面试题
公司业务主管岗位职责
2013/12/07 职场文书
计算机专业学生的自我评价
2013/12/15 职场文书
商场中秋节广播稿
2014/01/17 职场文书
个人担保书范文
2014/05/20 职场文书
Prometheus 监控MySQL使用grafana展示
2021/08/30 MySQL