Python发送email的3种方法


Posted in Python onApril 28, 2015

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) 
Python 相关文章推荐
在主机商的共享服务器上部署Django站点的方法
Jul 22 Python
Python连接DB2数据库
Aug 27 Python
python图像常规操作
Nov 11 Python
Python数据结构与算法之图的基本实现及迭代器实例详解
Dec 12 Python
深入了解Python中pop和remove的使用方法
Jan 09 Python
python中将一个全部为int的list 转化为str的list方法
Apr 09 Python
win7下python3.6安装配置方法图文教程
Jul 31 Python
对python的输出和输出格式详解
Dec 08 Python
Python PyQt5 Pycharm 环境搭建及配置详解(图文教程)
Jul 16 Python
Python获取一个用户名的组ID过程解析
Sep 03 Python
Pytoch之torchvision.transforms图像变换实例
Dec 30 Python
详解numpy.ndarray.reshape()函数的参数问题
Oct 13 Python
Python中使用partial改变方法默认参数实例
Apr 28 #Python
调试Python程序代码的几种方法总结
Apr 28 #Python
解析Python中的异常处理
Apr 28 #Python
python调用java模块SmartXLS和jpype修改excel文件的方法
Apr 28 #Python
Python EOL while scanning string literal问题解决方法
Sep 18 #Python
python中尾递归用法实例详解
Apr 28 #Python
在Python中使用元类的教程
Apr 28 #Python
You might like
几个学习PHP的网址
2006/11/25 PHP
php中static静态变量的使用方法详解
2010/06/04 PHP
php实现兼容2038年后Unix时间戳转换函数
2015/03/18 PHP
php正则修正符用法实例详解
2016/12/29 PHP
PHP上传文件及图片到七牛的方法
2018/07/25 PHP
JS实现进入页面时渐变背景色的方法
2015/02/25 Javascript
JS+CSS实现滑动切换tab菜单效果
2015/08/25 Javascript
原生js仿jquery animate动画效果
2016/07/13 Javascript
jQuery实现发送验证码并60秒倒计时功能
2016/11/25 Javascript
AngularJS中run方法的巧妙运用
2017/01/04 Javascript
js编写选项卡效果
2017/05/23 Javascript
JavaScript基本语法_动力节点Java学院整理
2017/06/26 Javascript
JS实现为动态创建的元素添加事件操作示例
2018/03/17 Javascript
js中call()和apply()改变指针问题的讲解
2019/01/17 Javascript
js实现json数组分组合并操作示例
2019/02/12 Javascript
vue.js实现左边导航切换右边内容
2019/10/21 Javascript
JQuery插件tablesorter表格排序实现过程解析
2020/05/28 jQuery
JavaScript仿京东轮播图效果
2021/02/25 Javascript
[15:41]教你分分钟做大人——灰烬之灵
2015/03/11 DOTA
[01:06]DOTA2小知识课堂 Ep.02 吹风竟可解梦境缠绕
2019/12/05 DOTA
[04:50]2019DOTA2高校联赛秋季赛四强集锦
2019/12/27 DOTA
Python时间戳与时间字符串互相转换实例代码
2013/11/28 Python
在Python中Dataframe通过print输出多行时显示省略号的实例
2018/12/22 Python
python实现狄克斯特拉算法
2019/01/17 Python
python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)
2019/04/01 Python
python实现图像拼接
2020/03/05 Python
Python实现LR1文法的完整实例代码
2020/10/25 Python
地球上最先进的胡子和头发修剪器:Bevel
2018/01/23 全球购物
Happy Socks英国官网:购买五颜六色的袜子
2020/11/03 全球购物
新学期家长寄语
2014/01/19 职场文书
大学生军训自我鉴定
2014/02/12 职场文书
汉语言文学毕业生自荐信范文
2014/03/24 职场文书
python实现批量移动文件
2021/04/05 Python
利用前端HTML+CSS+JS开发简单的TODOLIST功能(记事本)
2021/04/13 Javascript
Python面向对象之内置函数相关知识总结
2021/06/24 Python
Oracle锁表解决方法的详细记录
2022/06/05 Oracle