详解Python发送email的三种方式
作者:mimvp 发布时间:2023-07-01 07:19:28
标签:Python,发送,email
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
发送结果,会收到两封邮件,截图其中一份邮件如下图:
二、使用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
收件结果:
来源:https://segmentfault.com/a/1190000016721254


猜你喜欢
- read()方法读取文件size个字节大小。如果读取命中获得EOF大小字节之前,那么它只能读取可用的字节。语法以下是read()
- GO 语言的 for…range 能做什么呢?for…range 如何使用 ?for…range 的返回
- substr(string,start,length)参数:1,string 即你要截取的字符串2,start 即要截取的开始位置(0表示从
- matplotlib中的字体文件被封装在font_manager这个子模块中,fontManager.ttflist这个列表涵盖了所有Mat
- 1、PandasPython Data Analysis Library 或 pandas 是基于NumPy 的一种工具,相当于这是Pyth
- 01 并行复制的概念 在MySQL的主从复制架构中,主库上经常会并发的执行很多SQL,只要这些SQL没有产生锁等待,
- Vue给数组第一位添加对象数据核心代码如下: this.menuBar.unshift({
- 前言tips:第一次发技术文章,篇幅比较简短,主要采取文字和关键代码表现的形式,希望帮助到大家。(若有不正确还请多多指正)nextTick作
- 本文实例讲述了Go语言实现简单留言板的方法。分享给大家供大家参考。具体实现方法如下:package mainimport ( &n
- 前言终于能够挤出一点时间来总结最近学到的一些技术知识点了,博主这两周被居家隔离-集中隔离-居家隔离来回折腾,现在终于是得到解放能够空出的时间
- 1.业务场景有如下树形结构: +—0 +—1 +—2 +—4 +—5 +—3如果删除某个父节点,则其子节点,以及其子节点的子节点,以此类推,
- 文件对象提供了 read() 方法来按字节或字符读取文件内容,到底是读取宇节还是字符,则取决于是否使用了 b 模式,如果使用了 b 模式,则
- 1.问题背景Python之所以强大,不仅是因为该语言本身的特点,也是因为它拥有众多无所不能的第三方库。强大的软件库允许开发者专注于业务,避免
- 独立 fmt Log输出重定向golang的fmt包的输出函数 Println、Printf、PrintStack等,默认将打印输出到os.
- C#调用python脚本在平常工程项目开发过程中常常会涉及到机器学习、深度学习算法方面的开发任务,但是受限于程序设计语言本身的应用特点,该类
- 在工作和学习中如果同时传输多个文件,大的安装包,python提供了一种无线传输的方法,开启一个本地http服务器,同一局域网下可方便访问 经
- 本文实例讲述了Python实现扩展内置类型的方法。分享给大家供大家参考,具体如下:简介除了实现新的类型的对象方式外,有时我们也可以通过扩展P
- demo用了点extjs的东西,主要是为了打印json数组出来。 js code(XmlUtils.js): /**/ function X
- 我们都知道打开文件有两种方法:f = open()with open() as f:这两种方法的区别就是第一种方法需要我们自己关闭文件;f.
- 很多组织机构慢慢的在不同的服务器和地点部署SQL Server数据库——为各种应用和目的&m