GUI / lua-resty-mail

A high-level, easy to use, and non-blocking email and SMTP library for OpenResty.
MIT License
66 stars 16 forks source link
email lua openresty smtp

lua-resty-mail

CircleCI

A high-level, easy to use, and non-blocking email and SMTP library for OpenResty.

Features

Installation

Via OPM:

opm get GUI/lua-resty-mail

Or via LuaRocks:

luarocks install lua-resty-mail

Usage

local mail = require "resty.mail"

local mailer, err = mail.new({
  host = "smtp.gmail.com",
  port = 587,
  starttls = true,
  username = "example@gmail.com",
  password = "password",
})
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 = "Master Splinter <splinter@example.com>",
  to = { "michelangelo@example.com" },
  cc = { "leo@example.com", "Raphael <raph@example.com>", "donatello@example.com" },
  subject = "Pizza is here!",
  text = "There's pizza in the sewer.",
  html = "<h1>There's pizza in the sewer.</h1>",
  attachments = {
    {
      filename = "toppings.txt",
      content_type = "text/plain",
      content = "1. Cheese\n2. Pepperoni",
    },
  },
})
if err then
  ngx.log(ngx.ERR, "mailer:send error: ", err)
  return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end

API

new

syntax: mailer, err = mail.new(options)

Create and return a new mail object. In case of errors, returns nil and a string describing the error.

The options table accepts the following fields:

mailer:send

syntax: ok, err = mailer:send(data)

Send an email via the SMTP server. This function returns true on success. In case of errors, returns nil and a string describing the error.

The data table accepts the following fields:

Development

After checking out the repo, Docker can be used to run the test suite:

docker-compose run --rm app make test

Release Process

To release a new version to LuaRocks and OPM: