Skip to content
/ mua Public

Minimal SMTP client in Elixir

Notifications You must be signed in to change notification settings

ruslandoga/mua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

93 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mua

Documentation badge Hex.pm badge

Minimal SMTP client (aka Mail user agent).

Can be used with Bamboo and Swoosh.

Features

  • Direct messaging (no relays)
  • Indirect messaging (relays)
  • Minimal API
  • Processless

Installation

defp deps do
  [
    {:mua, "~> 0.2.0"}
  ]
end

Usage

This demo will use Mailpit:

$ docker run -d --rm -p 1025:1025 -p 8025:8025 -e "MP_SMTP_AUTH_ACCEPT_ANY=1" -e "MP_SMTP_AUTH_ALLOW_INSECURE=1" --name mailpit axllent/mailpit
$ open http://localhost:8025

High-level API:

message = """
Date: Mon, 25 Dec 2023 06:52:15 +0000\r
From: Mua <[email protected]>\r
Subject: README\r
To: Mr Receiver <[email protected]>\r
CC: Ms Receiver <[email protected]>\r
\r
like and subscribe
"""

{:ok, _receipt} =
  Mua.easy_send(
    _host = "localhost",
    _mail_from = "[email protected]",
    _rcpt_to = ["[email protected]", "[email protected]"],
    message,
    port: 1025,
    auth: [username: "username", password: "password"]
  )

Low-level API:

{:ok, socket, _banner} = Mua.connect(:tcp, "localhost", _port = 1025)
{:ok, extensions} = Mua.ehlo(socket, _sending_domain = "github.com")

{:ok, socket} =
  if "STARTTLS" in extensions do
    Mua.starttls(socket, "localhost")
  else
    {:ok, socket}
  end

:plain = Mua.pick_auth_method(extensions)
:ok = Mua.auth(socket, :plain, username: "username", password: "password")

:ok = Mua.mail_from(socket, "[email protected]")
:ok = Mua.rcpt_to(socket, "[email protected]")

message =
  """
  Date: Mon, 25 Dec 2023 06:52:15 +0000\r
  From: Mua <[email protected]>\r
  Subject: How was your day?\r
  To: Mr Receiver <[email protected]>\r
  \r
  Mine was fine.
  """

{:ok, _receipt} = Mua.data(socket, message)
:ok = Mua.close(socket)

About

Minimal SMTP client in Elixir

Resources

Stars

Watchers

Forks

Languages