详解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求素数示例分享
Feb 16 Python
Python检测网站链接是否已存在
Apr 07 Python
python 对dataframe下面的值进行大规模赋值方法
Jun 09 Python
Python实现登陆文件验证方法
Oct 06 Python
树莓派实现移动拍照
Jun 22 Python
python批量解压zip文件的方法
Aug 20 Python
Python二元赋值实用技巧解析
Oct 25 Python
利用Python校准本地时间的方法教程
Oct 31 Python
使用Pandas将inf, nan转化成特定的值
Dec 19 Python
SpringBoot实现登录注册常见问题解决方案
Mar 04 Python
Python getsizeof()和getsize()区分详解
Nov 20 Python
pytorch MSELoss计算平均的实现方法
May 12 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 动态随机生成验证码类代码
2010/04/09 PHP
基于php在各种web服务器的运行模式详解
2013/06/03 PHP
ThinkPHP学习笔记(一)ThinkPHP部署
2014/06/22 PHP
PHP中new static() 和 new self() 的区别介绍
2015/01/09 PHP
Yii2分页的使用及其扩展方法详解
2016/05/23 PHP
php版微信开发之接收消息,自动判断及回复相应消息的方法
2016/09/23 PHP
购物车实现的几种方式优缺点对比
2018/05/02 PHP
php实现微信支付之现金红包
2018/05/30 PHP
Thinkphp5.0 框架视图view的比较标签用法分析
2019/10/12 PHP
json简单介绍
2008/06/10 Javascript
ext 同步和异步示例代码
2009/09/18 Javascript
原始的js代码和jquery对比体会
2013/09/10 Javascript
ie中js创建checkbox默认选中问题探讨
2013/10/21 Javascript
Jquery实现textarea根据文本内容自适应高度
2015/04/03 Javascript
javascript操作cookie
2017/01/17 Javascript
jquery实现提示语淡入效果
2017/05/05 jQuery
深入理解angular2启动项目步骤
2017/07/15 Javascript
解决js相同的正则多次调用test()返回的值却不同的问题
2018/10/10 Javascript
JS使用iView的Dropdown实现一个右键菜单
2019/05/06 Javascript
Vue项目中使用WebUploader实现文件上传的方法
2019/07/21 Javascript
layui扩展上传组件模拟进度条的方法
2019/09/23 Javascript
jQuery高级编程之js对象、json与ajax用法实例分析
2019/11/01 jQuery
浅析Vue 防抖与节流的使用
2019/11/14 Javascript
鸿蒙系统中的 JS 开发框架
2020/09/18 Javascript
python构建深度神经网络(DNN)
2018/03/10 Python
python获取微信企业号打卡数据并生成windows计划任务
2019/04/30 Python
python标准库OS模块函数列表与实例全解
2020/03/10 Python
python matplotlib 绘图 和 dpi对应关系详解
2020/03/14 Python
css3弹性盒子flex实现三栏布局的实现
2020/11/12 HTML / CSS
如何写出高性能的JSP和Servlet
2013/01/22 面试题
介绍一下Linux中的链接
2016/06/05 面试题
出纳岗位职责
2013/11/09 职场文书
经贸日语专业个人求职信
2013/12/13 职场文书
班子成员四风问题自我剖析材料
2014/09/29 职场文书
漫画「狩龙人拉格纳」公开TV动画预告图
2022/03/22 日漫
Win11无法安装更新补丁KB3045316怎么办 附KB3045316补丁修复教程
2022/08/14 数码科技