详解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 分析Nginx访问日志并保存到MySQL数据库实例
Mar 13 Python
python使用分治法实现求解最大值的方法
May 12 Python
python获取元素在数组中索引号的方法
Jul 15 Python
Python使用Mechanize模块编写爬虫的要点解析
Mar 31 Python
Python中函数参数设置及使用的学习笔记
May 03 Python
理解生产者消费者模型及在Python编程中的运用实例
Jun 26 Python
python+matplotlib实现动态绘制图片实例代码(交互式绘图)
Jan 20 Python
django 按时间范围查询数据库实例代码
Feb 11 Python
Python分割指定页数的pdf文件方法
Oct 26 Python
Python中使用logging和traceback模块记录日志和跟踪异常
Apr 09 Python
django框架CSRF防护原理与用法分析
Jul 22 Python
python读取文件指定行内容实例讲解
Mar 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 checkbox 取值详细说明
2010/08/19 PHP
PHP获取当前所在目录位置的方法
2014/11/26 PHP
PHP CodeIgniter分页实例及多条件查询解决方案(推荐)
2017/05/20 PHP
laravel通用化的CURD的实现
2019/12/13 PHP
如何制作浮动广告 JavaScript制作浮动广告代码
2012/12/30 Javascript
Js 时间函数getYear()的使用问题探讨
2013/04/01 Javascript
使用jQuery UI的tooltip函数修饰title属性的气泡悬浮框
2013/06/24 Javascript
javascript控制台详解
2015/06/25 Javascript
AngularJS延迟加载html template
2016/07/27 Javascript
解析ajaxFileUpload 异步上传文件简单使用
2016/12/30 Javascript
webstrom Debug 调试vue项目的方法步骤
2018/07/17 Javascript
vue-cli项目修改文件热重载失效的解决方法
2018/09/19 Javascript
详细讲解如何创建, 发布自己的 Vue UI 组件库
2019/05/29 Javascript
Vue 自定义指令功能完整实例
2019/09/17 Javascript
Vue Element校验validate的实例
2020/09/21 Javascript
vue实现简易的双向数据绑定
2020/12/29 Vue.js
[43:35]TI4 循环赛第二日Liquid vs Fnatic
2014/07/11 DOTA
[01:13:59]LGD vs Mineski Supermajor 胜者组 BO3 第三场 6.5
2018/06/06 DOTA
python 创建弹出式菜单的实现代码
2017/07/11 Python
python使用sqlite3时游标使用方法
2018/03/13 Python
python selenium 执行完毕关闭chromedriver进程示例
2019/11/15 Python
Python求解正态分布置信区间教程
2019/11/20 Python
Python3 元组tuple入门基础
2020/02/09 Python
python中有帮助函数吗
2020/06/19 Python
django美化后台django-suit的安装配置操作
2020/07/12 Python
python 读取串口数据的示例
2020/11/09 Python
改变生活的男士内衣:SAXX Underwear
2019/08/28 全球购物
高三生物教学反思
2014/01/25 职场文书
职称评定自我鉴定
2014/03/18 职场文书
二年级班级文化建设方案
2014/05/10 职场文书
低碳日宣传活动总结
2014/07/09 职场文书
教师三严三实学习心得体会
2014/10/11 职场文书
技术员岗位职责范本
2015/04/11 职场文书
寻找最美乡村教师观后感
2015/06/18 职场文书
2015年音乐教研组工作总结
2015/07/22 职场文书
仅仅使用 HTML/CSS 实现各类进度条的方式汇总
2021/11/11 HTML / CSS