nginx配置
lua_package_path "/usr/local/openresty/lualib/resty/smtp/?.lua;;"; lua_need_request_body on; location ^~ /alarm/mail { resolver 202.106.0.20 valid=3600s; content_by_lua_file /path/to/send_mail.lua; }
其中resolver后为dns服务器ip;
lua代码
local smtp = require "resty.smtp" local mime = require "resty.smtp.mime" local mesgt = { headers= { subject= mime.ew("$subject", nil, { charset= "utf-8" }), ["content-transfer-encoding"]= "BASE64", ["content-type"]= "text/html; charset='utf-8'", }, body= mime.b64("$body") } local ret, err = smtp.send { from= "$from_email", rcpt= { "$to_email" }, user= "$from_email", password= "$password", server= "$smtp_server", port= 25, timeout= 30000, ssl= {enable= false, verify_cert= false}, domain= "$domain", source= smtp.message(mesgt), }
参考:
https://github.com/duhoobo/lua-resty-smtp