详解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装饰器使用的一些建议
Jun 03 Python
Python中pygame安装方法图文详解
Nov 11 Python
Python Django使用forms来实现评论功能
Aug 17 Python
浅析python继承与多重继承
Sep 13 Python
python3 中文乱码与默认编码格式设定方法
Oct 31 Python
python实现微信机器人: 登录微信、消息接收、自动回复功能
Apr 29 Python
pytorch多进程加速及代码优化方法
Aug 19 Python
通过celery异步处理一个查询任务的完整代码
Nov 19 Python
基于TensorBoard中graph模块图结构分析
Feb 15 Python
Python3.7下安装pyqt5的方法步骤(图文)
May 12 Python
Python如何自动获取目标网站最新通知
Jun 18 Python
Python可变与不可变数据和深拷贝与浅拷贝
Apr 06 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验证码(支持中文)
2007/02/14 PHP
PHP 5.3.1 安装包 VC9 VC6不同版本的区别是什么
2010/07/04 PHP
Thinkphp中volist标签mod控制一定记录的换行BUG解决方法
2014/11/04 PHP
PHP打开和关闭文件操作函数总结
2014/11/18 PHP
php中最简单的字符串匹配算法
2014/12/16 PHP
php使用Imagick生成图片的方法
2015/07/31 PHP
thinkPHP框架通过Redis实现增删改查操作的方法详解
2019/05/13 PHP
深入理解JQuery keyUp和keyDown的区别
2013/12/12 Javascript
js计算德州扑克牌面值的方法
2015/03/04 Javascript
jQuery实现图片轮播特效代码分享
2015/09/15 Javascript
javascript for-in有序遍历json数据并探讨各个浏览器差异
2015/11/30 Javascript
认识Knockout及如何使用Knockout绑定上下文
2015/12/25 Javascript
jQuery可见性过滤选择器用法示例
2016/09/09 Javascript
3种不同的ContextMenu右键菜单实现代码
2016/11/03 Javascript
iView框架问题整理小结
2018/10/16 Javascript
Vue开发Html5微信公众号的步骤
2019/04/11 Javascript
关于vue3.0中的this.$router.replace({ path: '/'})刷新无效果问题
2020/01/16 Javascript
JS控制下拉列表左右选择实例代码
2020/05/08 Javascript
[03:11]不朽宝藏三外观展示
2020/09/18 DOTA
Python常用正则表达式符号浅析
2014/08/13 Python
Python使用MySQLdb for Python操作数据库教程
2014/10/11 Python
Python os模块中的isfile()和isdir()函数均返回false问题解决方法
2015/02/04 Python
浅谈Python peewee 使用经验
2017/10/20 Python
解决pandas .to_excel不覆盖已有sheet的问题
2018/12/10 Python
基于CentOS搭建Python Django环境过程解析
2020/08/24 Python
Python3.9新特性详解
2020/10/10 Python
CSS3 3D立方体效果示例-transform也不过如此
2016/12/05 HTML / CSS
html5之Canvas路径绘图、坐标变换应用实例
2012/12/26 HTML / CSS
意大利体育用品和运动服网上商店:Maxi Sport
2019/09/14 全球购物
请介绍一下WSDL的文档结构
2013/03/17 面试题
高考备战决心书
2014/03/11 职场文书
毕业生自荐材料范文
2014/12/30 职场文书
党员倡议书
2015/01/19 职场文书
酒店客房服务员岗位职责
2015/04/09 职场文书
全国法制宣传日活动总结
2015/05/05 职场文书
企业文化学习心得体会
2016/01/21 职场文书