命令行下通过smtp发送邮件的工具——SendmailViaSMTP

Unless otherwise indicated, the text of documents in this site is available under the Creative Commons Attribution 3.0 Unported License, or any later version. Copyright 2009 - 2017 leopku.

经常需要在脚本运行结束后,通过邮件发送执行结果报告。

结果在命令下通过smtp帐号发送邮件真是难啊。找来找去,找到mstmp,很强大的工具。百度和google上很多mutt + msmtp结合发邮件的办法。

不过mutt + msmtp作为桌面解决方案还是不错的,我要在多台上服务器上发送邮件,难道要这些服务器上都安装mutt?好吧,安装也就算了,这些服务器上都配置一遍mutt的……后来是直接使用了msmtp来发送邮件。

还编译了msmtp截止发稿时的最新版本1.4.21版的rpm,含i386和x86_84两种架构的。有需要的朋友请留下邮箱。(为什么不直接放博客上?因为好戏在后头)

后来想想,每台服务器都要安装一遍也麻烦,想做些改进也要去改c++的源码。有没有更简便的方法呢?

想起以前用python写过发送邮件的程序,封装一下,可以在命令下调用不就是个很好的方案嘛!

于是SendmailViaSMTP就诞生了,特性如下:

  1. 只有一个SendmailViaSMTP.py一个文件,绿色

  2. py一个文件,绿色2、跨平台,装了python 2.3以上版本的系统都可以使用。就是说现在主流的linux都能直接跑。Linux、Windwos、MacOS、BSD通吃。

  3. 支持使用gmail帐号发送邮件

  4. 跨架构,不用再区分i386还是x86_64了,不用随时备份着两个或以上的rpm

  5. python代码易读易改,可以随时定制自己想要的功能。如果能把你的改进反馈给我,那是更好了!

项目地址

http://github.com/leopku/SendmailViaSMTP

SendmailViaSMTP is a command line tool for sending mail via an existing SMTP server and support Linux, Windows, MacOS, BSD etc.SendmailViaSMTP was powered by python. The minimize version of python is 2.3.It also support sending mail through smtp.gmail.com with your gmail account.

$ python SendmailViaSMTP.py -h
Usage: python SendmailViaSMTP.py [--host=smtp.yourdomain.com] <--port=110> [--user=smtpaccount] [--password=smtppass] <--subject=subject> [--file=filename]|[--content=mailbody] [--from=sender] [--to=reciver].

    example:
    1. echo "blablabla" | python SendmailViaSMTP.py --host="mail.domain.com" --from="myname@yourdomain.com" --to="friends1@domain1.com;friends2@domain.com" --user="myname@yourdomain.com" --password="p4word" --subject="mail title"
    2. python SendmailViaSMTP.py --host="mail.domain.com" --from="myname@yourdomain.com" --to="friends1@domain1.com;friends2@domain.com" --user="myname@yourdomain.com" --password="p4word" --subject="mail title" --file=/path/of/file
    3. python SendmailViaSMTP.py --host="mail.yourdomain.com" --from="myname@yourdomain.com" --to="friends1@domain1.com;friends2@domain2.com;friends3@domain3.com" --user="myname@yourdomain.com" --password="p4word" -s "Hello from MailViaSMTP" -c "This is a mail just for testing."

    The priority of three content inputing method is: piped-data, --file, --content.

This is a command line kit for sending mail via smtp server which can use in
multiple platforms like linux, BSD, Windows etc.     This little kit was
written by leopku#qq.com using python.     The minimum version of python
required was 2.3.

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -a ATTACH, --attach=ATTACH
                        Specifies a file as attachment to be attached. Can be
                        specified more than once.
  -s SUBJECT, --subject=SUBJECT
                        The subject of the mail.
  -c CONTENT, --content=CONTENT
                        option mode. Mail body should be passed through this
                        option. Note: this option should be ignored while
                        working with piped-data or --file option.
  -f my@domain.com, --from=my@domain.com
                        Set envelope from address. If --user option is not
                        empty, commonly this option should be equaled with
                        --user options. Otherwize, the authoration of the smtp
                        server should be failed.
  -t friend@domain2.com, --to=friend@domain2.com
                        Set recipient address. Use semicolon to seperate multi
                        recipient, for example: "a@a.com;b@b.com."
  -F FILE, --file=FILE  File mode. Read mail body from file. NOTE: this option
                        should be ignored while working with piped-data.
  --host=smtp.domain.com
                        SMTP server host name or ip. Like "smtp.gmail.com"
                        through GMail(tm) or "192.168.0.99" through your own
                        smtp server.
  -P PORT, --port=PORT  SMTP server port number. Default is 25.
  -u my@domain.com, --user=my@domain.com
                        The username for SMTP server authorcation. Left this
                        option empty for non-auth smtp server.
  -p PASSWORD, --password=PASSWORD
                        The password for SMTP server authorcation. NOTE: if
                        --user option is empty, this option will be ignored.
  --tls                 Using tls to communicate with SMTP server. Default is
                        false. NOTE: if --host option equals "smtp.gmail.com",
                        this option becomes defaults true.
  --log=LOG             specify --log=DEBUG or --log=debug, more info see
                        document for logging module.

更新历史

  • 2011-12-24: 修正一个因python不同版本2.4.x、2.5.x、2.6.x下gmail发送邮件的问题(感谢doitmy)。