openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务
lua-resty-mail 是一个不错的openresty mail 扩展,我们可以用来进行邮件发送,支持附件功能
smtp2http 是一个smtp 服务,可以将smtp 请求数据转换为http rest 请求,这个在我们的实际应用
中还是很方便的,比如需要mail 服务,但是我们需要进行一些灵活的控制,比如一些devops平台
我们需要监控的报警处理,同时想对于内容进行一些处理
备注: 测试使用openresty + docker-compose 的方式运行,同时使用了一个webhook 的工具
环境准备
- docker-compose 文件
version: "3"
services:
app:
build: ./
ports:
- "8080:80"
volumes:
- "./app/:/opt/app/"
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
benthos:
image: jeffail/benthos
volumes:
- "./conf/webhook.yaml:/benthos.yaml"
ports:
- "4195:4195"
smtp2http:
image: uflare/smtp2http
command: --listen=:25 --webhook=http://benthos:4195/ --strict=false
- nginx 配置
worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_code_cache off;
gzip on;
resolver 127.0.0.11; # 注意dns 的地址
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path '/opt/app/?.lua;;';
server {
listen 80;
server_name localhost;
charset utf-8;
root html;
default_type text/html;
location / {
default_type text/html;
index index.html;
}
location /mail {
content_by_lua_block {
require("mail/init")();
}
}
location /info {
more_set_headers 'Content-Type application/json';
content_by_lua_block {
require("app/init")("dalong","admin");
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- webhook 配置
conf/webhook.yaml
input:
type: broker
broker:
inputs:
- type: http_server
http_server:
path: /
processors:
- type: text
text:
operator: prepend
value: "get email message: "
output:
type: stdout
- dockerfile
安装mail 包
FROM openresty/openresty:alpine-fat
LABEL author="1141591465@qq.com"
RUN /usr/local/openresty/luajit/bin/luarocks install lua-rocks-app-project
RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-mail
mail 调用代码
参考官方的demo,简单修改几个参数
local mail = require "resty.mail"
function send()
local mailer, err = mail.new({
host = "smtp2http",
port = 25,
ssl=false,
})
if err then
ngx.log(ngx.ERR, "mail.new error: ", err)
return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
local ok, err = mailer:send({
from = "dalongrong <rongfl@demo.com>",
to = { "1141591465@qq.com" },
subject = "荣锋亮测试!",
text = "There's pizza in the sewer.",
html = "<h1>There's pizza in the sewer.</h1>",
})
if err then
ngx.log(ngx.ERR, "mailer:send error: ", err)
return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
end
return send
启动&&测试
- 启动
docker-compose up -d
- 效果
使用docker-compose logs -f 查看日志
curl -i http://localhost:8080/mail
HTTP/1.1 200 OK
Server: openresty/1.13.6.2
Date: Fri, 04 Jan 2019 01:01:56 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
日志信息:
Attaching to openresty-my-luarocks-demo-mail_benthos_1, openresty-my-luarocks-demo-mail_app_1, openresty-my-luarocks-demo-mail_smtp2http_1
benthos_1 | {"@timestamp":"2019-01-04T01:01:29Z","@service":"benthos","level":"INFO","component":"benthos","message":"Launching a benthos instance, use CTRL+C to close."}
benthos_1 | {"@timestamp":"2019-01-04T01:01:29Z","@service":"benthos","level":"INFO","component":"benthos","message":"Listening for HTTP requests at: http://0.0.0.0:4195\n"}
smtp2http_1 | start the smtp server on address: :25
smtp2http_1 | specified maximum body size: 2097152 bytes
smtp2http_1 | specified server name: smtp2http
smtp2http_1 | specified webhook: http://benthos:4195/
smtp2http_1 | validating the incoming FROM header: false
app_1 | 2019/01/04 01:01:29 [alert] 1#1: lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:11
app_1 | nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:11
benthos_1 | get email message: addresses%5Bbcc%5D=&addresses%5Bcc%5D=&addresses%5Bfrom%5D=rongfl%40demo.com&addresses%5Bto%5D=1141591465%40qq.com&body%5Bhtml%5D=PGgxPlRoZXJlJ3MgcGl6emEgaW4gdGhlIHNld2VyLjwvaDE%2B&body%5Btext%5D=VGhlcmUncyBwaXp6YSBpbiB0aGUgc2V3ZXIu&id=%3C1546563716.18541b6b1bdaf9deea2f3c73908a2b94e63a432d%40localhost.localdomain%3E&subject=%E8%8D%A3%E9%94%8B%E4%BA%AE%E6%B5%8B%E8%AF%95%21
app_1 | 172.26.0.1 - - [04/Jan/2019:01:01:56 +0000] "GET /mail HTTP/1.1" 200 5 "-" "curl/7.54.0"
说明
上边的邮件内容是url+base64编码了的,实际内容可以通过base64 编码的工具处理下就可以了,使用openresty 的mail 模块我们
可以方便的搞好多事情
参考资料
https://github.com/GUI/lua-resty-mail
https://github.com/rongfengliang/openresty_luarocksmodule-demo
https://github.com/Jeffail/benthos
https://github.com/uflare/smtp2http
openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务的更多相关文章
- jenkins 使用smtp2http 邮件服务,扩展灵活的构建通知功能
smtp2http 是一个很方便的可以将smtp 转换为http 服务的工具,同时也支持扩展的开发,我们可以使用此工具 扩展灵活的ci.cd 生命周期管理,而不是简单的邮件处理 备注: 使用docke ...
- 给lnmp一键包中的nginx安装openresty的lua扩展
lnmp一键包(https://lnmp.org)本人在使用之后发现确实好用,能帮助我们快速搭建起lnmp.lamp和lnmpa的web生产环境,因此推荐大家可以多试试.但有的朋友可能需要使用open ...
- LUA+resty 搭建验证码服务器
使用Lua和OpenResty搭建验证码服务器 雨客 2016-04-08 16:38:11 浏览2525 评论0 云数据库Redis版 摘要: Lua下有个Lua-GD图形库,通过简单的Lua语句就 ...
- gearman openresty 集成试用
很简单使用了一个openresty 的lua 模块 环境准备 docker-compose 文件 详细配置可以参考 https://github.com/rongfengliang/gearmango ...
- (转)OpenResty(nginx+lua) 开发入门
原文:https://blog.csdn.net/enweitech/article/details/78519398 OpenResty 官网:http://openresty.org/ Open ...
- CentOS安装OpenResty(Nginx+Lua)开发环境
一.简介 OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高 ...
- OpenResty(nginx+lua) 入门
OpenResty 官网:http://openresty.org/ OpenResty 是一个nginx和它的各种三方模块的一个打包而成的软件平台.最重要的一点是它将lua/luajit打包了进来, ...
- openresty 集成 sentry 异常系统
sentry 是一个方便的错误异常追踪系统,同时社区也提供了openresty 的lua 包,使用docker-compose 进行测试 备注: sentry 部分的配置来自官方文档 环境准备 doc ...
- 【原创】大叔问题定位分享(36)openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil
openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil This function returns nil if the request ...
随机推荐
- linux系统安装tomcat详细配置
1.通过ssh工具将apache-tomcat-7.0.85.tar.gz拖拽到 /home文件下 2.切换到/home 目录下 3.解压 指令 tar -zvxf apache-tomcat-7.0 ...
- DevExpress v18.1新版亮点——Reporting篇(四)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress Reporting v18.1 的新功能,快来下载试用新版本 ...
- android 应用程序中执行Linux 命令
ADB 无线调试命令son = "setprop service.adb.tcp.port 5555\n" + "stop adbd\n" + "st ...
- Oracle备份
今天被吊,特来学习备份. https://blog.csdn.net/zhaiqi618/article/details/5616215 https://www.cnblogs.com/yingpp/ ...
- 移动端设置, mobile , 一张图片作为背景 ,平铺 ,自动拉伸 , 图片 铺满视界 ,窗口. background-image , background-size, background-repeat
1. 效果: 浏览器: 手机模拟: 2.代码: <!DOCTYPE html> <html lang="en"> <head> <met ...
- python day03作业
- Python 封装
# 封装: # 1. 对属性的封装 # 2. 对功能的封装 # 3. 模块 # 4. 包 class Student: def __init__(self, num, name, clazz): se ...
- swift3.0 自定义键盘
...绕了一大圈,又绕回原生来了,今天,学习一下swift3.0语法下的自定义键盘.效果图如下: 其实,很简单,只需要把UITextView(或者UITextField)的inputView属性设置为 ...
- 微软Power BI 每月功能更新系列——10月Power BI 新功能学习
Power BI Desktop10月产品功能摘要 本月Power Plus Desktop的更新充满了整个产品的小型和大型改进.一个巨大的更新是Power BI服务支持我们的复合模型和聚合预览.这实 ...
- java pojo类
POJO POJO是Plain OrdinaryJava Object的缩写 可以当作简单的Java对象 实际就是普通JavaBeans 外文名 POJO 实际意义 普通JavaBeans 全 ...