详解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中使用第三方库xlutils来追加写入Excel文件示例
Apr 05 Python
Python聚类算法之基本K均值实例详解
Nov 20 Python
安装Python的教程-Windows
Jul 22 Python
关于反爬虫的一些简单总结
Dec 13 Python
python获取txt文件词向量过程详解
Jul 05 Python
Python 实现遥感影像波段组合的示例代码
Aug 04 Python
django认证系统实现自定义权限管理的方法
Aug 28 Python
Python Opencv中用compareHist函数进行直方图比较对比图片
Apr 07 Python
Restful_framework视图组件代码实例解析
Nov 17 Python
python 爬取豆瓣网页的示例
Apr 13 Python
AI:如何训练机器学习的模型
Apr 16 Python
Python排序算法之插入排序及其优化方案详解
Jun 11 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函数array_merge用法一例(合并同类数组)
2013/02/03 PHP
PHP函数func_num_args用法实例分析
2015/12/07 PHP
CI框架简单邮件发送类实例
2016/05/18 PHP
PHP设计模式之装饰器模式定义与用法简单示例
2018/08/13 PHP
PHP安装memcache扩展的步骤讲解
2019/02/14 PHP
PHP ob缓存以及ob函数原理实例解析
2020/11/13 PHP
jQuery $.each的用法说明
2010/03/22 Javascript
详解JavaScript函数绑定
2013/08/18 Javascript
javascript引用赋值(地址传值)用法实例
2015/01/13 Javascript
JS响应鼠标点击实现两个滑块区间拖动效果
2015/10/26 Javascript
javascript类型系统 Window对象学习笔记
2016/01/07 Javascript
深入浅析AngularJS和DataModel
2016/02/16 Javascript
深入理解ECMAScript的几个关键语句
2016/06/01 Javascript
js实现文字超出部分用省略号代替实例代码
2016/09/01 Javascript
详解Vue.js之视图和数据的双向绑定(v-model)
2017/06/23 Javascript
详解Angular Reactive Form 表单验证
2017/07/06 Javascript
vue 中filter的多种用法
2018/04/26 Javascript
JQuery特殊效果和链式调用操作示例
2019/05/13 jQuery
Node.js API详解之 console模块用法详解
2020/05/12 Javascript
解决vue watch数据的方法被调用了两次的问题
2020/11/07 Javascript
Bootstrap FileInput实现图片上传功能
2021/01/28 Javascript
python中lambda与def用法对比实例分析
2015/04/30 Python
matplotlib绘制符合论文要求的图片实例(必看篇)
2017/06/02 Python
python Tkinter版学生管理系统
2019/02/20 Python
Pytorch中index_select() 函数的实现理解
2019/11/19 Python
Pandas —— resample()重采样和asfreq()频度转换方式
2020/02/26 Python
Mac中PyCharm配置Anaconda环境的方法
2020/03/04 Python
Python 跨.py文件调用自定义函数说明
2020/06/01 Python
大学毕业生自荐书怎么写?
2014/01/06 职场文书
春游踏青活动方案
2014/08/14 职场文书
最新离婚协议书范本
2014/08/19 职场文书
大学生团员个人总结
2015/02/14 职场文书
项目经理助理岗位职责
2015/04/13 职场文书
综治目标管理责任书
2015/05/11 职场文书
python数据可视化JupyterLab实用扩展程序Mito
2021/11/20 Python
AngularJS实现多级下拉框
2022/03/25 Javascript