在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
lua-resty-mail 是一个不错的openresty mail 扩展,我们可以用来进行邮件发送,支持附件功能 环境准备
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
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;
}
}
}
input:
type: broker
broker:
inputs:
- type: http_server
http_server:
path: /
processors:
- type: text
text:
operator: prepend
value: "get email message: "
output:
type: stdout
FROM openresty/openresty:alpine-fat
LABEL author="[email protected]"
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 <[email protected]>",
to = { "[email protected]" },
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
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 |
请发表评论