详解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 相关文章推荐
Ubuntu 14.04+Django 1.7.1+Nginx+uwsgi部署教程
Nov 18 Python
python实现简单的计时器功能函数
Mar 14 Python
Python入门之modf()方法的使用
May 15 Python
Python实现优先级队列结构的方法详解
Jun 02 Python
python爬取各类文档方法归类汇总
Mar 22 Python
Python实现的redis分布式锁功能示例
May 29 Python
Python如何获得百度统计API的数据并发送邮件示例代码
Jan 27 Python
Python Pandas数据中对时间的操作
Jul 30 Python
浅谈Python 递归算法指归
Aug 22 Python
使用pytorch实现可视化中间层的结果
Dec 30 Python
pytorch中的卷积和池化计算方式详解
Jan 03 Python
Python数据分析之绘图和可视化详解
Jun 02 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 SPL使用方法和他的威力
2013/11/12 PHP
PHP正则表达式过滤html标签属性(DEMO)
2016/05/04 PHP
PHP+jquery+CSS制作头像登录窗(仿QQ登陆)
2016/10/20 PHP
php PDO判断连接是否可用的实现方法
2017/04/03 PHP
PHP实现搜索时记住状态的方法示例
2018/05/11 PHP
[原创]静态页面也可以实现预览 列表不同的显示方式
2006/10/14 Javascript
Javascript优化技巧(文件瘦身篇)
2008/01/28 Javascript
iframe父页面获取子页面参数的方法
2014/02/21 Javascript
javascript基于HTML5 canvas制作画箭头组件
2014/06/25 Javascript
JavaScript获取伪元素(Pseudo-Element)属性的方法技巧
2015/03/13 Javascript
用JavaScript显示浏览器客户端信息的超相近教程
2015/06/18 Javascript
jQuery在线选座位插件seat-charts特效代码分享
2015/08/27 Javascript
js实现超简单的展开、折叠目录代码
2015/08/28 Javascript
jfinal与bootstrap的登录跳转实战演习
2015/09/22 Javascript
JS实现将Asp.Net的DateTime Json类型转换为标准时间的方法
2016/08/02 Javascript
canvas实现爱心和彩虹雨效果
2017/03/09 Javascript
从零开始学习Node.js系列教程之设置HTTP头的方法示例
2017/04/13 Javascript
vue的Virtual Dom实现snabbdom解密
2017/05/03 Javascript
vue-scroller记录滚动位置的示例代码
2018/01/17 Javascript
Vue项目中使用WebUploader实现文件上传的方法
2019/07/21 Javascript
JS实现骰子3D旋转效果
2019/10/24 Javascript
JavaScript经典案例之简易计算器
2020/08/24 Javascript
在项目vue中使用echarts的操作步骤
2020/09/07 Javascript
在elementui中Notification组件添加点击事件实例
2020/11/11 Javascript
Vue 401配合Vuex防止多次弹框的案例
2020/11/11 Javascript
JS hasOwnProperty()方法检测一个属性是否是对象的自有属性的方法
2021/01/29 Javascript
celery4+django2定时任务的实现代码
2018/12/23 Python
基于python cut和qcut的用法及区别详解
2019/11/22 Python
python 检查数据中是否有缺失值,删除缺失值的方式
2019/12/02 Python
PyQt5实现登录页面
2020/05/30 Python
生产现场工艺工程师岗位职责
2013/11/28 职场文书
项目投资建议书
2014/05/16 职场文书
机电系毕业生求职信
2014/07/11 职场文书
我的大学四年规划书范文2014
2014/09/26 职场文书
校本研修个人总结
2015/02/28 职场文书
了解Redis常见应用场景
2021/06/23 Redis