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 服务的更多相关文章

  1. jenkins 使用smtp2http 邮件服务,扩展灵活的构建通知功能

    smtp2http 是一个很方便的可以将smtp 转换为http 服务的工具,同时也支持扩展的开发,我们可以使用此工具 扩展灵活的ci.cd 生命周期管理,而不是简单的邮件处理 备注: 使用docke ...

  2. 给lnmp一键包中的nginx安装openresty的lua扩展

    lnmp一键包(https://lnmp.org)本人在使用之后发现确实好用,能帮助我们快速搭建起lnmp.lamp和lnmpa的web生产环境,因此推荐大家可以多试试.但有的朋友可能需要使用open ...

  3. LUA+resty 搭建验证码服务器

    使用Lua和OpenResty搭建验证码服务器 雨客 2016-04-08 16:38:11 浏览2525 评论0 云数据库Redis版 摘要: Lua下有个Lua-GD图形库,通过简单的Lua语句就 ...

  4. gearman openresty 集成试用

    很简单使用了一个openresty 的lua 模块 环境准备 docker-compose 文件 详细配置可以参考 https://github.com/rongfengliang/gearmango ...

  5. (转)OpenResty(nginx+lua) 开发入门

    原文:https://blog.csdn.net/enweitech/article/details/78519398 OpenResty 官网:http://openresty.org/  Open ...

  6. CentOS安装OpenResty(Nginx+Lua)开发环境

    一.简介 OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高 ...

  7. OpenResty(nginx+lua) 入门

    OpenResty 官网:http://openresty.org/ OpenResty 是一个nginx和它的各种三方模块的一个打包而成的软件平台.最重要的一点是它将lua/luajit打包了进来, ...

  8. openresty 集成 sentry 异常系统

    sentry 是一个方便的错误异常追踪系统,同时社区也提供了openresty 的lua 包,使用docker-compose 进行测试 备注: sentry 部分的配置来自官方文档 环境准备 doc ...

  9. 【原创】大叔问题定位分享(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 ...

随机推荐

  1. Linux系统管理常用命令用法总结(2)

    1.free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内存区段,以及系统核心使用的缓冲区等. 语法:free [-bkmotV][-s <间隔秒数>] 参数说明: - ...

  2. Docker常用命令学习

    sudo service docker startsudo docker run hello-worlddocker stats --helpdocker run -d -P training/web ...

  3. ios UITableView背景图片设置

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa ...

  4. SS报错的解决

    SS报错 AttributeError: /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1: undefined symbol: 解决方案: EVP_CIPHER_ ...

  5. Vue实例data对象里允许有哪些类型数据

     做项目中遇到了data赋值的问题,总结了下常用的data赋值的数据类型.之前一直不确定是否能在data里写函数,实践证明data里也是可以对函数赋值的. export default { name: ...

  6. eact native生成APP报错:You have not accepted the license agreements of the following SDK components:

    一.报错信息 * What went wrong: A problem occurred configuring project ':app'. > You have not accepted ...

  7. hadoop day 2

    1.hdfs shell相关命令 #hadoop fs -ls / 查看HDFS根目录 #hadoop fs -mkdir /test 在根目录创建一个目录test #hadoop fs -mkdir ...

  8. python 中的堆 (heapq 模块)应用:Merge K Sorted Lists

    堆是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短 ...

  9. PHP 设计模式系列 —— 工厂方法模式(Factory Method)(转)

    1.模式定义 定义一个创建对象的接口,但是让子类去实例化具体类.工厂方法模式让类的实例化延迟到子类中. 2.问题引出 框架需要为多个应用提供标准化的架构模型,同时也要允许独立应用定义自己的域对象并对其 ...

  10. insserv: warning: script 'busybox-httpd' missing LSB tags and overrides

    /********************************************************************************* * insserv: warnin ...