搜索
首页后端开发Python教程Python发送email的3种方法

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

代码如下:


#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
#python2.7x 
#send_simple_email_by_account.py  @2014-07-30 
#author: orangleliu 
 
'''''
使用python写邮件 simple
使用126 的邮箱服务
''' 
 
import smtplib 
from email.mime.text import MIMEText 
 
SMTPserver = 'smtp.126.com' 
sender = 'liuzhizhi123@126.com' 
password = "xxxx" 
 
message = 'I send a message by Python. 你好' 
msg = MIMEText(message) 
 
msg['Subject'] = 'Test Email by Python' 
msg['From'] = sender 
msg['To'] = destination 
 
mailserver = smtplib.SMTP(SMTPserver, 25) 
mailserver.login(sender, password) 
mailserver.sendmail(sender, [sender], msg.as_string()) 
mailserver.quit() 
print 'send email success' 

2、调用sendmail命令 (linux)

代码如下:


# -*- coding: utf-8 -*- 
#python2.7x 
#send_email_by_.py 
#author: orangleliu 
#date: 2014-08-15 
'''''
用的是sendmail命令的方式
 
这个时候邮件还不定可以发出来,hostname配置可能需要更改
''' 
 
from email.mime.text import MIMEText 
from subprocess import Popen, PIPE 
 
def get_sh_res(): 
    p = Popen(['/Application/2.0/nirvana/logs/log.sh'], stdout=PIPE) 
    return str(p.communicate()[0]) 
 
def mail_send(sender, recevier): 
    print "get email info..." 
    msg = MIMEText(get_sh_res()) 
    msg["From"] = sender 
    msg["To"] = recevier 
    msg["Subject"] = "Yestoday interface log results" 
    p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE) 
    res = p.communicate(msg.as_string()) 
    print 'mail sended ...' 
 
if __name__ == "__main__": 
    s = "957748332@qq.com" 
    r = "zhizhi.liu@chinacache.com" 
    mail_send(s, r) 


3、使用smtp服务来发送(本地或者是远程服务器)

代码如下:


#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
#python2.7x 
#send_email_by_smtp.py 
#author: orangleliu 
#date: 2014-08-15 
'''''
linux 下使用本地的smtp服务来发送邮件
前提要开启smtp服务,检查的方法
#ps -ef|grep sendmail
#telnet localhost 25
 
这个时候邮件还不定可以发出来,hostname配置可能需要更改
''' 
import smtplib 
from email.mime.text import MIMEText 
from subprocess import Popen, PIPE 
 
 
def get_sh_res(): 
    p = Popen(['/Application/2.0/nirvana/logs/log.sh'], stdout=PIPE) 
    return str(p.communicate()[0]) 
 
def mail_send(sender, recevier): 
    msg = MIMEText(get_sh_res()) 
    msg["From"] = sender 
    msg["To"] = recevier 
    msg["Subject"] = "Yestoday interface log results" 
    s = smtplib.SMTP('localhost') 
    s.sendmail(sender, [recevier], msg.as_string()) 
    s.quit() 
    print 'send mail finished...' 
 
if __name__ == "__main__": 
    s = "zhizhi.liu@chinacache.com" 
    r =  s 
    mail_send(s, r) 

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python中的合并列表:选择正确的方法Python中的合并列表:选择正确的方法May 14, 2025 am 12:11 AM

Tomergelistsinpython,YouCanusethe操作员,estextMethod,ListComprehension,Oritertools

如何在Python 3中加入两个列表?如何在Python 3中加入两个列表?May 14, 2025 am 12:09 AM

在Python3中,可以通过多种方法连接两个列表:1)使用 运算符,适用于小列表,但对大列表效率低;2)使用extend方法,适用于大列表,内存效率高,但会修改原列表;3)使用*运算符,适用于合并多个列表,不修改原列表;4)使用itertools.chain,适用于大数据集,内存效率高。

Python串联列表字符串Python串联列表字符串May 14, 2025 am 12:08 AM

使用join()方法是Python中从列表连接字符串最有效的方法。1)使用join()方法高效且易读。2)循环使用 运算符对大列表效率低。3)列表推导式与join()结合适用于需要转换的场景。4)reduce()方法适用于其他类型归约,但对字符串连接效率低。完整句子结束。

Python执行,那是什么?Python执行,那是什么?May 14, 2025 am 12:06 AM

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python:关键功能是什么Python:关键功能是什么May 14, 2025 am 12:02 AM

Python的关键特性包括:1.语法简洁易懂,适合初学者;2.动态类型系统,提高开发速度;3.丰富的标准库,支持多种任务;4.强大的社区和生态系统,提供广泛支持;5.解释性,适合脚本和快速原型开发;6.多范式支持,适用于各种编程风格。

Python:编译器还是解释器?Python:编译器还是解释器?May 13, 2025 am 12:10 AM

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

python用于循环与循环时:何时使用哪个?python用于循环与循环时:何时使用哪个?May 13, 2025 am 12:07 AM

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

Python循环:最常见的错误Python循环:最常见的错误May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)