详解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 3调用百度OCR API实现剪贴板文字识别
Sep 04 Python
Python转换时间的图文方法
Jul 01 Python
django-初始配置(纯手写)详解
Jul 30 Python
基于Python的图像数据增强Data Augmentation解析
Aug 13 Python
TensorFlow实现自定义Op方式
Feb 04 Python
python numpy--数组的组合和分割实例
Feb 24 Python
Django中的session用法详解
Mar 09 Python
解决django 向mysql中写入中文字符出错的问题
May 18 Python
Windows下PyCharm配置Anaconda环境(超详细教程)
Jul 31 Python
基于 Python 实践感知器分类算法
Jan 07 Python
Python加密技术之RSA加密解密的实现
Apr 08 Python
python绘制简单直方图(质量分布图)的方法
Apr 21 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类
2006/07/15 PHP
PHP入门教程之表单与验证实例详解
2016/09/11 PHP
CI框架教程之优化验证码机制详解【验证码辅助函数】
2019/04/16 PHP
jquery通过visible来判断标签是否显示或隐藏
2014/05/08 Javascript
详解javascript遍历方式
2015/11/11 Javascript
基于javascript实现全国省市二级联动下拉选择菜单
2016/01/28 Javascript
ArtEditor富文本编辑器增加表单提交功能
2016/04/18 Javascript
详解XMLHttpRequest(一)同步请求和异步请求
2016/09/14 Javascript
js+css3制作时钟特效
2016/10/16 Javascript
JS实现重新加载当前页面或者父页面的几种方法
2016/11/30 Javascript
详解React 16 中的异常处理
2017/07/28 Javascript
详解Nuxt.js Vue服务端渲染摸索
2018/02/08 Javascript
vue checkbox 全选 数据的绑定及获取和计算方法
2018/02/09 Javascript
vue2.0中set添加属性后视图不能更新的解决办法
2019/02/22 Javascript
js动态添加带圆圈序号列表的实例代码
2021/02/18 Javascript
[03:24]2014DOTA2国际邀请赛 神秘商店生意火爆
2014/07/18 DOTA
Python获取运行目录与当前脚本目录的方法
2015/06/01 Python
python音频处理用到的操作的示例代码
2017/10/27 Python
Python如何计算语句执行时间
2019/11/22 Python
pytorch:torch.mm()和torch.matmul()的使用
2019/12/27 Python
三个python爬虫项目实例代码
2019/12/28 Python
Python 连接 MySQL 的几种方法
2020/09/09 Python
CSS3 创建网页动画实现弹跳球动效果
2018/10/30 HTML / CSS
数以千计的折扣工业产品:ESE Direct
2018/05/20 全球购物
巴西最大的在线约会网站:ParPerfeito
2018/07/11 全球购物
新西兰领先的内衣店:Bendon Lingerie新西兰
2018/07/11 全球购物
是什么让J2EE适合用来开发多层的分布式的应用
2015/01/16 面试题
高级文秘工作总结的自我评价
2013/09/28 职场文书
学期自我鉴定
2013/11/04 职场文书
总账会计岗位职责
2014/03/13 职场文书
股份合作协议书范本
2014/04/14 职场文书
董事长秘书工作职责
2014/06/10 职场文书
学校联谊协议书
2014/09/16 职场文书
颐和园导游词
2015/01/30 职场文书
2015员工年度考核评语
2015/03/25 职场文书
廉政党课工作报告案例
2019/06/21 职场文书