详解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中的urllib模块使用详解
Jul 07 Python
Python复数属性和方法运算操作示例
Jul 21 Python
基于django channel实现websocket的聊天室的方法示例
Apr 11 Python
python itchat给指定联系人发消息的方法
Jun 11 Python
python调用支付宝支付接口流程
Aug 15 Python
TensorFlow2.0矩阵与向量的加减乘实例
Feb 07 Python
Python3.7下安装pyqt5的方法步骤(图文)
May 12 Python
python线程池如何使用
May 28 Python
Python基于BeautifulSoup爬取京东商品信息
Jun 01 Python
Python为何不支持switch语句原理详解
Oct 21 Python
基于OpenCV的路面质量检测的实现
Nov 04 Python
解决python3输入的坑——input()
Dec 05 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
BBS(php & mysql)完整版(八)
2006/10/09 PHP
PHP判断指定时间段的2个方法
2014/03/14 PHP
JavaScript使用prototype定义对象类型(转)[
2006/12/22 Javascript
JavaScript 字符编码规则
2009/05/04 Javascript
Javascript 学习书 推荐
2009/06/13 Javascript
jQuery插件 tabBox实现代码
2010/02/09 Javascript
js各种验证文本框输入格式(正则表达式)
2010/10/22 Javascript
THREE.JS入门教程(5)你应当知道的十件事
2013/01/24 Javascript
JavaScript学习笔记之JS对象
2015/01/22 Javascript
jquery插件star-rating.js实现星级评分特效
2015/04/15 Javascript
理解JavaScript表单的基础知识
2016/01/25 Javascript
Node.js中防止错误导致的进程阻塞的方法
2016/08/11 Javascript
微信小程序 五星评价功能的实现
2017/03/09 Javascript
十大热门的JavaScript框架和库
2017/03/21 Javascript
深入理解Webpack 中路径的配置
2017/06/17 Javascript
js微信分享实现代码
2020/10/11 Javascript
解决vue数据不实时更新的问题(数据更改了,但数据不实时更新)
2020/10/27 Javascript
Python  连接字符串(join %)
2008/09/06 Python
Python使用MD5加密字符串示例
2014/08/22 Python
详解Python中的条件判断语句
2015/05/14 Python
Pandas之drop_duplicates:去除重复项方法
2018/04/18 Python
浅谈Tensorflow由于版本问题出现的几种错误及解决方法
2018/06/13 Python
python实现AES和RSA加解密的方法
2019/03/28 Python
css3 border旋转时的动画应用
2016/01/22 HTML / CSS
HTML5组件Canvas实现图像灰度化(步骤+实例效果)
2013/04/22 HTML / CSS
悦木之源美国官网:Origins美国
2016/08/01 全球购物
美国50岁以上单身人士约会平台:SilverSingles
2018/06/29 全球购物
教师旷工检讨书
2014/01/18 职场文书
员工保密承诺书
2014/05/28 职场文书
诉讼授权委托书
2014/10/15 职场文书
开展党的群众路线教育实践活动个人对照检查材料
2014/11/05 职场文书
2014年小学德育工作总结
2014/12/05 职场文书
西岭雪山导游词
2015/02/06 职场文书
教师年度个人总结
2015/02/11 职场文书
2015年教学工作总结
2015/04/02 职场文书
初三数学教学反思
2016/02/17 职场文书