Python 发送 email 的三种方式

Python 发送 email 的三种方式Python发送email的三种方式,分别为使用登录邮件服务器、使用smtp服务、调用sendmail命令来发送三种方法原文请参见米扑博客:Python发送email的三种方式Python发送email比较简单,可以通过登录邮件服务来发送,linux下也可以使用调用sendmail命令来发送,还可以使用本地或者是远程的smtp服务来发送邮件,不管是单个,群发,还是抄送都比较容易实现。…

大家好,又见面了,我是你们的朋友全栈君。

Python发送email的三种方式,分别为使用登录邮件服务器、使用smtp服务、调用sendmail命令来发送三种方法

原文请参见米扑博客:Python 发送 email 的三种方式

Python发送email比较简单,可以通过登录邮件服务来发送,linux下也可以使用调用sendmail命令来发送,还可以使用本地或者是远程的smtp服务来发送邮件,不管是单个,群发,还是抄送都比较容易实现。本米扑博客先介绍几个最简单的发送邮件方式记录下,像html邮件,附件等也是支持的,需要时查文档即可。

 

一、登录邮件服务器

通过smtp登录第三方smtp邮箱发送邮件,支持 25 和 465端口

vim python_email_1.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

#!/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服务

测试失败,略过或留言指正

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

#!/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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

#!/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 的三种方式

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/157561.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • postfix邮箱_用post方式发送文件

    postfix邮箱_用post方式发送文件一、首先关闭防火墙[root@localhost~]#systemctlstopfirewalld[root@localhost~]#setenforce0[root@localhost~]#getenforcePermissive二、搭建postfix[root@localhost~]#yuminstallpostfix三、配置postfix[root@local…

  • Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件

    Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件

    2021年10月15日
  • Activiti工作流_activiti使用教程

    Activiti工作流_activiti使用教程Activiti项目是一项新的基于Apache许可的开源BPM平台,从基础开始构建,旨在提供支持新的BPMN2.0标准,包括支持对象管理组(OMG),面对新技术的机遇,诸如互操作性和云架构,提供技术实现。 <!–添加Activiti工作流的支持一般需要exclusions–> <dependency> <groupId…

  • db2 terminate作用_db2 truncate table immediate

    db2 terminate作用_db2 truncate table immediate SQLSTATE是按类代码进行分组的;对于子代码,请参阅相应的表。表2.SQLSTATE类代码类代码  含义要获得子代码,参阅…00完全成功完成表301警告表402无数据表507动态SQL错误表608连接异常表709触发操作异常表80A功能部件不受支持表90D目标类型规范无效表100F无效标记表11

    2022年10月29日
  • ANT如何安装?

    ANT如何安装?1、安装Ant之前首先的安装好JAVA环境,ant的官网下载:https://ant.apache.org/manualdownload.cgi(注意:要选择与自己JDK相匹配的ant.这里我选择的是

  • windows安装opencv(opencv安装不了)

    终于实现了在windows下配置Qt和opencv,从刚学opencv尝试,到现在终于配置成功,断断续续经历了一年左右,真实操碎了心。。。走了太多弯路了系统:windows10Qt:Qt5.11.1,mingw5.3版本opencv:3.2.0版本一直有用最新软件的强迫症,这次屈服了,没有使用opencv3.4.2版本,本人尝试了2.7.13版本到3.4.2版本的所有opencv,…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号