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 相关文章推荐
python引用DLL文件的方法
May 11 Python
django通过ajax发起请求返回JSON格式数据的方法
Jun 04 Python
python和c语言的主要区别总结
Jul 07 Python
基于python的BP神经网络及异或实现过程解析
Sep 30 Python
python 实现兔子生兔子示例
Nov 21 Python
pytorch 实现打印模型的参数值
Dec 30 Python
Tensorflow tf.dynamic_partition矩阵拆分示例(Python3)
Feb 07 Python
Python3和PyCharm安装与环境配置【图文教程】
Feb 14 Python
python递归调用中的坑:打印有值, 返回却None
Mar 16 Python
Python绘图之二维图与三维图详解
Aug 04 Python
解决python3.x安装numpy成功但import出错的问题
Nov 17 Python
python 实时调取摄像头的示例代码
Nov 25 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-FPM和Nginx的通信机制详解
2019/02/01 PHP
用javascript实现改变TEXTAREA滚动条和按钮的颜色,以及怎样让滚动条变得扁平
2007/04/20 Javascript
JavaScript中的apply()方法和call()方法使用介绍
2012/07/25 Javascript
javascript学习(二)javascript常见问题总结
2013/01/02 Javascript
对frameset、frame、iframe的js操作示例代码
2013/08/16 Javascript
将查询条件的input、select清空
2014/01/14 Javascript
JavaScript中实现sprintf、printf函数
2015/01/27 Javascript
Bootstrap每天必学之滚动监听
2016/03/16 Javascript
微信小程序点击控件修改样式实例详解
2017/07/07 Javascript
Vue.js 单页面多路由区域操作的实例详解
2017/07/17 Javascript
vue中计算属性(computed)、methods和watched之间的区别
2017/07/27 Javascript
vue.js实现只弹一次弹框
2018/01/29 Javascript
Vue.js中 v-model 指令的修饰符详解
2018/12/03 Javascript
Vue项目中使用WebUploader实现文件上传的方法
2019/07/21 Javascript
解决使用layui对select append元素无效或者未及时更新的问题
2019/09/18 Javascript
Python基于分水岭算法解决走迷宫游戏示例
2017/09/26 Python
Python读取图片为16进制表示简单代码
2018/01/19 Python
python获取程序执行文件路径的方法(推荐)
2018/04/26 Python
Flask框架信号用法实例分析
2018/07/24 Python
django一对多模型以及如何在前端实现详解
2019/07/24 Python
用Python实现校园通知更新提醒功能
2019/11/23 Python
python实现将两个文件夹合并至另一个文件夹(制作数据集)
2020/04/03 Python
Django数据结果集序列化并展示实现过程
2020/04/22 Python
Python正则表达式高级使用方法汇总
2020/06/18 Python
浅析python 字典嵌套
2020/09/29 Python
Python使用paramiko连接远程服务器执行Shell命令的实现
2021/03/04 Python
详解HTML5通讯录获取指定多个人的信息
2016/12/20 HTML / CSS
Darphin迪梵官网: 来自巴黎,植物和精油调制的护肤品牌
2016/10/11 全球购物
Hotels.com英国:全球领先的酒店住宿提供商
2019/01/24 全球购物
新西兰最大的天然保健及护肤品网站:HealthPost(直邮中国)
2021/02/13 全球购物
几个常见的软件测试问题
2016/09/07 面试题
班主任经验交流会主持词
2014/04/01 职场文书
会计学专业求职信
2014/07/17 职场文书
武当山导游词
2015/02/03 职场文书
幼儿园小班教育随笔
2015/08/14 职场文书
Win10/Win11 任务栏替换成经典样式
2022/04/19 数码科技