• 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
    "}
    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

  • 相关阅读:
    20170519
    20170515
    20170511
    20170509
    20170505
    使用 FirewallD 构建动态防火墙
    Elasticsearch 5.2.1Cluster 搭建
    elk,centos7,filebeat,elasticsearch-head详细安装步骤
    elk,centos7,filebeat,elasticsearch-head集成搭建
    memcached
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/10217943.html
Copyright © 2020-2023  润新知