详解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 19 Python
跟老齐学Python之list和str比较
Sep 20 Python
举例讲解Python程序与系统shell交互的方式
Apr 09 Python
python安装mysql-python简明笔记(ubuntu环境)
Jun 25 Python
Python实现Pig Latin小游戏实例代码
Feb 02 Python
python读取word文档,插入mysql数据库的示例代码
Nov 07 Python
对pandas通过索引提取dataframe的行方法详解
Feb 01 Python
git查看、创建、删除、本地、远程分支方法详解
Feb 18 Python
PIL.Image.open和cv2.imread的比较与相互转换的方法
Jun 03 Python
python输入中文的实例方法
Sep 14 Python
PyTorch中的拷贝与就地操作详解
Dec 09 Python
Python 排序最长英文单词链(列表中前一个单词末字母是下一个单词的首字母)
Dec 14 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
escape unescape的php下的实现方法
2007/04/27 PHP
js下函数般调用正则的方法附代码
2008/06/22 PHP
php is_executable判断给定文件名是否可执行实例
2016/09/26 PHP
PHP 实现 WebSocket 协议原理与应用详解
2020/04/22 PHP
火狐下table中创建form导致两个table之间出现空白
2013/09/02 Javascript
按钮接受回车事件的三种实现方法
2014/06/06 Javascript
防止登录页面出现在frame中js代码
2014/07/22 Javascript
js判断手机端(Android手机还是iPhone手机)
2015/07/22 Javascript
JS onkeypress兼容性写法详解
2016/04/27 Javascript
省市联动效果的简单实现代码(推荐)
2016/06/06 Javascript
进阶之初探nodeJS
2017/01/24 NodeJs
vue router下的html5 history在iis服务器上的设置方法
2017/10/18 Javascript
从parcel.js打包出错到选择nvm的全部过程
2018/01/23 Javascript
Vue动画事件详解及过渡动画实例
2019/02/09 Javascript
layui table动态表头 改变表格头部 重新加载表格的方法
2019/09/21 Javascript
详解Vue的ref特性的使用
2020/01/24 Javascript
[17:13]DOTA2 HEROS教学视频教你分分钟做大人-斯拉克
2014/06/13 DOTA
[05:59]带你看看DPC的台前幕后
2021/03/11 DOTA
Python中的浮点数原理与运算分析
2017/10/12 Python
Python 实现中值滤波、均值滤波的方法
2019/01/09 Python
python实现七段数码管和倒计时效果
2019/11/23 Python
xadmin使用formfield_for_dbfield函数过滤下拉表单实例
2020/04/07 Python
Python 日期时间datetime 加一天,减一天,加减一小时一分钟,加减一年
2020/04/16 Python
python读取excel数据绘制简单曲线图的完整步骤记录
2020/10/30 Python
python如何编写类似nmap的扫描工具
2020/11/06 Python
Django怎么在admin后台注册数据库表
2020/11/14 Python
巧用 CSS3的webkit-box-reflect 倒影实现各类动效
2021/03/05 HTML / CSS
Spartoo芬兰:欧洲最大的网上鞋店
2016/08/28 全球购物
Java语言程序设计测试题选择题部分
2014/04/03 面试题
仓库管理制度
2014/01/21 职场文书
运动会开幕式解说词
2014/02/05 职场文书
工伤事故赔偿协议书
2014/04/15 职场文书
关于运动会的广播稿
2014/09/22 职场文书
七年级作文之冬景
2019/11/07 职场文书
《我在为谁工作》:工作的质量往往决定生活的质量
2019/12/27 职场文书
浅谈Python数学建模之数据导入
2021/06/23 Python