python 发送邮件脚本
一、该脚本适合在 linux 中做邮件发送测试用,只需要填写好 发送账号和密码以及发送人即可,然后使用 python ./filename.py (当前目录下)即可。如果发送出错,会将错误详情抛出来。
#!/usr/bin/env python
# -*- coding: utf-8 -*- __author__ = 'Apollo' import time
import smtplib
from email.mime.text import MIMEText
_user = "" # 发送账号
_pwd = "" # 账号密码
_to = "" # 发送人 def send_email(content):
text = '[%s] Reporting:' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) try:
msg = MIMEText(content)
msg["Subject"] = text
msg["From"] = _user
msg["To"] = _to #s = smtplib.SMTP("smtp.dc.mydorma.com", timeout=30) # 使用 25 号端口(普通邮件发送)
s = smtplib.SMTP_SSL(host='smtp.qq.com', port=465) # 使用 465 号端口(SSL加密发送)
s.set_debuglevel(1)
s.login(_user, _pwd)
s.sendmail(_user, _to, msg.as_string())
s.quit()
except (smtplib.SMTPAuthenticationError,
smtplib.SMTPConnectError,
smtplib.SMTPDataError,
smtplib.SMTPException,
smtplib.SMTPHeloError,
smtplib.SMTPRecipientsRefused,
smtplib.SMTPResponseException,
smtplib.SMTPSenderRefused,
smtplib.SMTPServerDisconnected) as e:
print 'Warning: %s was caught while trying to send email.\nContent:%s\n' % (e.__class__.__name__, e.message) if __name__ == '__main__':
send_email("Prepare to work:") # 邮件内容
二、该脚本适合使用其它语言(例如PHP)外部执行改 python 脚本来实际发送电子邮件,需要填写好 发送账号和密码即可,其它的参数从 外部传进来,例如php这样调用:
exec("/data/programdir/filename.py $to $subject $content $cc",$out,$result)
若 $result == 0 则发送成功,$result == 1 则发送失败。
#!/usr/bin/env python
# -*- coding: utf-8 -*- __author__ = 'Apollo' import time
import smtplib
import sys # 使用外部传参,必须引入 sys 类库
from email.mime.text import MIMEText
_user = "xxxxx@xx.com" # 发件账号
_pwd = "" # 密码
_to = sys.argv[1] # 发送人
_cc = sys.argv[4] # 转呈人 if _cc.strip()=='':
rcpt = _to
else:
rcpt = [_to] + _cc.split(",") def send_email(content):
text = '[%s] Reporting:' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) try:
msg = MIMEText(content)
msg["Subject"] = sys.argv[2]
msg["From"] = _user
msg["To"] = _to
msg["Cc"] = _cc s = smtplib.SMTP("xxxx@xx.com", timeout=30)
#s = smtplib.SMTP_SSL(host='smtp.qq.com', port=465)
s.set_debuglevel(1)
#s.login(_user, _pwd) # 当不需要做身份认证的时候,可以屏蔽该行
s.sendmail(_user,rcpt, msg.as_string())
s.quit()
except (smtplib.SMTPAuthenticationError,
smtplib.SMTPConnectError,
smtplib.SMTPDataError,
smtplib.SMTPException,
smtplib.SMTPHeloError,
smtplib.SMTPRecipientsRefused,
smtplib.SMTPResponseException,
smtplib.SMTPSenderRefused,
smtplib.SMTPServerDisconnected) as e:
print 'Warning: %s was caught while trying to send email.\nContent:%s\n' % (e.__class__.__name__, e.message) if __name__ == '__main__':
send_email(sys.argv[3]) # 邮件内容
如有转载,请注明出处:http://www.cnblogs.com/chrdai/p/7791693.html
python 发送邮件脚本的更多相关文章
- Python发送邮件脚本
import smtplib from email.mime.text import MIMEText mailserver = "smtp.163.com" username_s ...
- python发送邮件脚本ssl 465端口
#coding:utf8 from smtplib import SMTP_SSL from email.header import Header from email.mime.text impor ...
- Zabbix调用外部脚本发送邮件:python编写脚本
Zabbix调用外部脚本发送邮件的时候,会在命令行传入两个参数,第一个参数就是要发送给哪个邮箱地址,第二个参数就是邮件信息,为了保证可以传入多个参数,所以假设有多个参数传入 #!/usr/bin/en ...
- Zabbix日志监视的汇总报警(更新发送邮件脚本)
Zabbix的用户一定会碰到这种情况: 日志报警一般设置的是multiple模式,有错误大量写入的时候,每写入一行就会触发一次action,导致出现大量的报警邮件. 特别是ora的报警,经常一出就是上 ...
- python 发送邮件例子
想到用python发送邮件 主要是服务器 有时候会产生coredump文件 ,然后因为脚本重启原因,服务器coredump产生后会重启 但是没有主动通知开发人员 想了下可以写个脚本一旦产生cored ...
- 【转】【Python】Python发送邮件(常见四种邮件内容)
在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下,Shell脚本发送邮件告警是件很简单的事,有现成的邮件服务软件或者调 ...
- Python发送邮件(最全)
简单邮件传输协议(SMTP)是一种协议,用于在邮件服务器之间发送电子邮件和路由电子邮件. Python提供smtplib模块,该模块定义了一个SMTP客户端会话对象,可用于使用SMTP或ESMTP侦听 ...
- python接口自动化(三十二)--Python发送邮件(常见四种邮件内容)番外篇——上(详解)
简介 本篇文章与前边没有多大关联,就是对前边有关发邮件的总结和梳理.在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下, ...
- Python发送邮件(常见四种邮件内容)
Python发送邮件(常见四种邮件内容) 转载 2017年03月03日 17:17:04 转自:http://lizhenliang.blog.51cto.com/7876557/1875330 ...
随机推荐
- java工程操作redis
启动redis服务 redis-server redis.windows.conf 添加驱动 <dependency> <groupId>redis.clients</g ...
- centos6.5下编译安装mariadb-10.0.20
源码编译安装mariadb-10.0.20.tar.gz 一.安装cmake编译工具 跨平台编译器 # yum install -y gcc* # yum install -y cmake 解决依赖关 ...
- Go语言规格说明书 之 类型(Types)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...
- h5新API之WebStorage解决页面数据通信问题
localStorage相信大家都不陌生,今天我们要讨论的不是怎么存储数据,获取数据.而是看看WebStorage的一些妙用,相信大家在开发中遇到过这样一个场景,一个页面中嵌套一个iframe,ifr ...
- Quartz 定时邮件发送多个备份文件
项目代码 pom.xml 文件Quartz 的包是整个项目不可缺少的 <properties> <!-- Spring的版本 --> <springframework.v ...
- hdu1937 二维尺取
/* 二维上的尺取,外层循环枚举j轴上的可能,内层在i轴上尺取即可 O(N^3) */ #include<iostream> #include<cstdio> #include ...
- 谷歌云ssh开启root密码登录
修改配置 1.先选择从浏览器打开ssh连接服务器 连接登录成功后,输入以下命令 sudo -i #切换到root passwd #修改密码 然后会要求输入新密码,然后再重复一次密码,输入密码的时候不会 ...
- .NetCore下使用Prometheus实现系统监控和警报 (五)进阶自定义收集指标 之 Counter
Prometheus下面定了四种类型的收集方式,下面我们主要来来说下Counter的使用 Nuget导入Prometheus.AspNetCore包 下面先来看下我的Prometheus配置,这里我没 ...
- #9 //[SDOI2017]新生舞会
题解: 分数规划+费用流 常数巨大开o2加inline加register还是不行 我也不知道为什么 代码: #include <bits/stdc++.h> using namespace ...
- python全栈开发day37-html
web准备总结: 结构标准:相当于人的身体.html就是用来制作网页的. 表现标准: 相当于人的衣服.css就是对网页进行美化的. 行为标准: 相当于人的动作.JS就是让网页动起来,具有生命力的 1. ...