• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

duhoobo/lua-resty-smtp: I must be crazy trying to send mail with Nginx.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

duhoobo/lua-resty-smtp

开源软件地址(OpenSource Url):

https://github.com/duhoobo/lua-resty-smtp

开源编程语言(OpenSource Language):

Lua 98.9%

开源软件介绍(OpenSource Introduction):

lua-resty-smtp

I must be crazy trying to send mail with Nginx.

Purpose

To make Nginx a bridge between HTTP and SMTP.

Using lua-resty-smtp in your lua code under Nginx, you just need to issue a HTTP request with your handy HTTP client (curl, wget, urllib2 from Python etc.), in order to send a mail to your SMTP server.

Features

  • Based on module socket.smtp from LuaScoket 2.0.2, and API-compatible with it also

  • SSL connection supported (lua-nginx-lua >= v0.9.11 needed)

APIs

lua-resty-smtp is API-compatible with socket.smtp from LuaSocket 2.0.2, and you can check SMTP for detailed reference of it.

And to support SSL connection to SMTP server, optional parameter ssl is added:

  • ssl: should be a table with following fields:

    • enable - boolean - whether or not use SSL connection to SMTP server, default false;

    • verify_cert - boolean - whether or not to perform SSL verification, default false. When set to true, the server certificate will be verified according to the CA certificate specified by the lua_ssl_trusted_cerfificate directive.

Extra filters

In addtion to the low-level filters provided by LuaSocket, two more filters is provided:

  • mime.ew: used to encode non-ASCII string into the Encoded-Word format (not support Q-encoding yet);

  • mime.unew: used to decode string in Encoded-Word format (not implemented);

Installation

make install

or

luarocks install --local rockspec/resty.smtp-0.0.3-1.rockspec

Example

local config = require("config")
local smtp = require("resty.smtp")
local mime = require("resty.smtp.mime")
local ltn12 = require("resty.smtp.ltn12")

-- ...
-- Suppose your mail data in table `args` and default settings
-- in table `config.mail`
-- ...

local mesgt = {
    headers= {
        subject= mime.ew(args.subject or config.mail.SUBJECT, nil,
                         { charset= "utf-8" }),
        ["content-transfer-encoding"]= "BASE64",
        ["content-type"]= "text/plain; charset='utf-8'",
    },

    body= mime.b64(args.body)
}

local ret, err = smtp.send {
    from= args.from or config.mail.FROM,
    rcpt= rcpts,
    user= args.user or config.mail.USER,
    password= args.password or config.mail.PASSWORD,
    server= args.server or config.mail.SERVER,
    domain= args.domain or config.mail.DOMAIN,
    source= smtp.message(mesgt),
}

NOTICES:

  • ngx.tcp.socket uses Nginx's builtin and nonblocking DNS resolver to resolve domain names IF you configure server field with hostname instead of IP address. SEE doc of Nginx directive resolver for more information.

TODO

  • Don't abort the whole SMTP request while one of the many recipients invalid;
  • To reimplement MIME-relative pure-lua version low-level filters with FFI?
  • To implement filter mime.unew;
  • To Reduce namespace pollution by module();

Performance

Your SMTP server is the bottleneck. :)

Known Issues

  • Only work with LuaJIT 2.x now, because the codebase relies on pcall massively and lua-nginx-module does not work well with standard Lua 5.1 VM under this situation. See Known Issues



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
awesomeWM/awesome: awesome window manager发布时间:2022-08-16
下一篇:
wizzard0/js2lua: Javascript to Lua translator发布时间:2022-08-16
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap