iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python 发送 email 的三种方
  • 477
分享到

Python 发送 email 的三种方

三种Pythonemail 2023-01-31 07:01:34 477人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

python发送email的三种方式,分别为使用登录邮件服务器、使用smtp服务、调用sendmail命令来发送三种方法 原文请参见米扑博客:Python 发送 email 的三种方式 Python发送email比较简单,可以通过登录邮件

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

发送结果,会收到两封邮件,截图其中一份邮件如下图:

 

二、使用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

收件结果:

 

--结束END--

本文标题: Python 发送 email 的三种方

本文链接: https://www.lsjlt.com/news/192116.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python 发送 email 的三种方
    Python发送email的三种方式,分别为使用登录邮件服务器、使用smtp服务、调用sendmail命令来发送三种方法 原文请参见米扑博客:Python 发送 email 的三种方式 Python发送email比较简单,可以通过登录邮件...
    99+
    2023-01-31
    三种 Python email
  • python程序发送email的源码
    把开发过程经常用的代码段收藏起来,下边代码段是关于python程序发送email的的代码,应该是对码农们有一些好处。 server = 'smtp.gmail.com:587'; #imports from time import sl...
    99+
    2023-01-31
    源码 程序 python
  • Python使用QQ邮箱发送Email的方法实例
    前言 其实Python使用QQ邮箱发送Email代码很简单,短短几行代码就可以实现这个功能。 使用到的模块有smtplib和email这个两个模块,关于这两个模块的方法就不多说了。不了解的朋友们可以查看这...
    99+
    2022-06-04
    实例 邮箱 方法
  • node.js发送邮件email的方法详解
    本文实例讲述了node.js发送邮件email的方法。分享给大家供大家参考,具体如下: 通常我们做node项目时,可能我们会碰到做一个简单的邮件反馈,那么我们今天就来讨论一下,其中遇到的各种坑。 总的来说做...
    99+
    2022-06-04
    发送邮件 详解 方法
  • linux shell发送Email邮件的方法详解
    一封最简单的邮件 echo -e "To: handy1989@qq.comnCC: handy1989@qq.comnFrom: handy<handy@test.com>nSubjec...
    99+
    2022-06-04
    详解 邮件 方法
  • python yagmail第三方库发送
    1.安装第三方库yagmail:   pip install yagmail 2.上代码 1 import yagmail 2 import os 3 4 5 def send_email(): 6 7 #链...
    99+
    2023-01-30
    第三方 python yagmail
  • PHP使用email()函数发送邮件的方法
    随着互联网的不断发展和普及,电子邮件成为人们日常交流中不可缺少的一部分。在网站后台开发过程中,很多时候需要使用PHP发送邮件,以满足邮件通知、注册验证等功能。PHP提供了email()函数来实现邮件的发送,并且使用也非常简单。本文将详细介绍...
    99+
    2023-05-22
    PHP 发送邮件 email()
  • linux中shell发送Email邮件的实现方法
    本篇内容主要讲解“linux中shell发送Email邮件的实现方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“linux中shell发送Email邮件的实现方法”吧!一封最简单的邮件echo...
    99+
    2023-06-09
  • Android开发中怎样调用系统Email发送邮件(多种调用方式)
    我们都知道,在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外。 在Android中,调用Email有三种类型的Intent: I...
    99+
    2022-06-06
    调用 android开发 email 邮件 Android
  • 关于kafka发送消息的三种方式总结
    目录kafka发送消息的方式需要引入文件测试方法MAC下操作指令windows操作指令总结kafka发送消息的方式 package com.zl.kafkademo; impor...
    99+
    2023-05-14
    kafka发送消息 kafka发送 kafka消息
  • Asp.Net Core中发送Email的完整步骤
    前言 在项目开发中常常会需要做发送 Email 的功能,在 ASP.NET Core 中你可以用 MailKit 来实现 Email 的发送,MailKit 是一个开源的客户端...
    99+
    2022-06-07
    net ASP.NET core email ASP
  • python发送邮件的几种常用方法
    第一种是最常见的,smtp发送 import smtplibimport sysimport tracebackfrom email.mime.text import MIMETextfrom email.mime.multipart im...
    99+
    2023-09-02
    python 开发语言
  • SpringBoot实现定时发送邮件的三种方法案例详解
    目录一、发送邮件的三种方法二、定时任务介绍1.@EnableScheduling2.@Scheduled三、前期准备工作1、登录QQ邮箱获取授权码第一步:进入QQ邮箱第二步:找到PO...
    99+
    2023-03-06
    SpringBoot定时发送邮件 SpringBoot发送邮件
  • JSP与JavaMail如何发送三种类型的附件
    这篇文章给大家分享的是有关JSP与JavaMail如何发送三种类型的附件的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。发送三种类型的附件前面我们已学会了发送一般文本邮件和超文本邮件,今天我们将让大家学会编写三种类...
    99+
    2023-06-03
  • python的三种取整方式
    下面介绍几种常用的取整方法,包括向下取整、四舍五入、向上取整。 (1)向下取整 向下取整很简单,直接使用int()函数即可,如下代码(Python 2.7.5 IDLE) a = 3.75 int(a) ...
    99+
    2023-01-31
    三种 方式 python
  • c# 两种发送邮件的方法
    目录一、两种发送邮件的方法二、遇到的问题 三、示例System.Web.MailSystem.Net.Mail一、两种发送邮件的方法 有用到两种方式发邮件,一种是用Syst...
    99+
    2022-11-12
  • Python实现各种邮件发送
    目录一、发送纯文本内容二、发送附件图片三、发送纯文本附件四、发送excel表格附件前言: Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。 Pyt...
    99+
    2022-11-11
  • python之三行代码发送邮件
    (1)首先进入cmd,输入pip install yagmail (2)思路:1 、连接服务器:yagmail.SMTP(邮箱账号,邮箱密码,邮箱服务器地址,邮箱服务器端口)                     2 、准备正文内容:c...
    99+
    2023-01-30
    发送邮件 代码 python
  • python安装pillow的三种方法
    目录第一种方法第二种方法第三种方法安装pillow(python的图形界面库) 第一种方法 在Dos界面输入pip install pillow(但是不知为何总是失败);搞了好几次都...
    99+
    2022-11-12
  • python中的三种注释方法
    目录python注释方法方式1方式2方式3python小技巧 开头注释设置路径python注释方法 方式1 单行注释:shift + #(在代码的最前面输入,非选中代码进行注释)多行...
    99+
    2022-11-11
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作