允许脚本将电子邮件发送到预配置的地址。
notifymail的Python项目详细描述
notifymail允许脚本将电子邮件发送到预先配置的地址。 它对于无人参与和计划的脚本报告特别有用 他们的身份是管理员。
notifymail的设计非常易于安装和使用。我写的 因为我不知道如何配置内置的 sendmail正确转发电子邮件的命令,但我无法获得 类似的ssmtp工作包。
要求
- OS X或Linux
- python 2.7或3.4
- 电子邮件帐户
安装
$ pip install notifymail # or pip3 (for Python 3.x)
配置
您必须知道电子邮件提供商的SMTP设置,它可以 通常在提供商的网站上查找。例如这里有 Gmail’s SMTP settings, 通过互联网搜索“gmail smtp设置”获得:
- gmail smtp服务器:smtp.gmail.com
- gmail smtp端口:587(用于tls)
- gmail smtp使用tls?是
通常,您的smtp用户名将与您的电子邮件地址相同,并且 您的SMTP密码将与电子邮件密码相同。
设置就绪后,运行notifymail.py --setup 命令:
$ notifymail.py --setup SMTP Server Hostname: smtp.gmail.com SMTP Server Port [465]: 587 SMTP Server Uses TLS (y/n) [n]: yes SMTP Username: robot@gmail.com SMTP Password: ******** From Address [robot@gmail.com]: robot@gmail.com From Name (optional) []: notifymail To Address: admin@example.com Verifying connection to SMTP server... OK
用法
从命令行
$ echo "Hello World" | notifymail.py -s "Subject"
notifymail将从标准输入读取消息正文并允许 使用-s选项指定主题行。你也可以 使用--from-name选项指定自定义发件人名称。
在python 2中,消息体和所有参数的编码是 假设是utf-8。在python 3中,两者的编码在 the usual fashion。
完整使用信息:
Usage: notifymail.py --setup | -s SUBJECT [-b BODY] [--from-name NAME] | --probe Options: -h, --help show this help message and exit --setup setup mail server configuration --probe check whether mail server is reachable -s SUBJECT, --subject=SUBJECT subject line. Required. -b BODY, --body=BODY body. Read from standard input if omitted. --from-name=NAME sender name. Overrides the default sender name.
从python脚本
import notifymail notifymail.send('Subject', 'Hello World')
字符串参数可以是unicode字符串或utf-8编码的 顺便说一句。
从非python脚本
只需使用语言的普通api执行notifymail命令 用于运行系统命令。
例如,在ruby中:
require 'open3' Open3.popen3('notifymail.py', '-s', 'Subject') {|stdin, stdout, stderr, wait_thr| stdin.puts('Hello World!') stdin.close exit_code = wait_thr.value.to_i if exit_code != 0 raise "notifymail exited with error code #{exit_code}." end }
例如,在Java中:
import java.io.*; try { Process notifymail = Runtime.getRuntime().exec(new String[] { "notifymail.py", "-s", "Subject" }); PrintStream stdin = new PrintStream( notifymail.getOutputStream(), /*autoFlush=*/false, "UTF-8"); stdin.println("Hello World!"); stdin.close(); int exitCode = notifymail.waitFor(); if (exitCode != 0) { throw new Exception("notifymail exited with error code " + exitCode + "."); } } catch (Exception e) { throw new RuntimeException("Unable to send email.", e); }
限制
- 配置的SMTP设置以明文形式存储,包括 SMTP密码。
许可证
此代码是在麻省理工学院许可证下提供的。
发行说明
- 1.1
- 添加对Python3.4的支持。删除对Python2.6的支持。
- 将--from-name修复为实际工作。
- 修复--setup以在完成安装后不打印使用信息。
- 1.0
- 初始版本。