Skip to main content
Version: LOC v0.6 (legacy)

Mail Agent

Sending emails with a SMTP server.

Availability

  • ✓ Generic logic
  • ✗ Aggregator logic

Connect to SMTP Server

async smtp?.connect(host: string, username?: string, password?: string): Promise<SmtpAgentHub>

Connect to a SMTP server. Returns a SmtpAgentHub object.

ParameterTypeDescription
hoststringSMTP server address
usernamestring(Optional) SMTP user name
passwordstring(Optional) SMTP user password
portnumber(Optional) SMTP server port (465, 25 or 587). Default: 465.
secureConnectionSmtp.SecureConnection(Optional) Secured connection mode

Smtp.SecureConnection

MemberDescription
NoneNo encryption
Wrapper(default) TLS wrapper connection; usually on port 465
RequiredStart with insecure connection and use STARTTLS; usually on port 587
OpportunisticStart with insecure connection and use STARTTLS when available
info

Import Smtp

JavaScript users can access Smtp via Saffron.Smtp:

const Smtp = Saffron.Smtp;

TypeScript users can import it from @fstnetwork/loc-logic-sdk:

import { ..., Smtp } from "@fstnetwork/loc-logic-sdk";

Example (default)

// import Smtp as above

// connect to smtp (TLS wrapped connection on port 465)
const mailClient = await ctx.agents.smtp?.connect(
"smtp.example.com", // host
"user@example.com", // username (mail)
"******", // password
);

Example (with custom port and secured connection)

// import Smtp as above

const mailClient = await ctx.agents.smtp?.connect(
"smtp.example.com",
"user@example.com",
"******",
587, // port
Smtp.SecureConnection.Required, // TLS mode
);

Mail Client

async mailClient?.send(mail: Smtp.Mail)

Send an email.

Example

// connect to SMTP...

// mail body
const mail_body = `Nothing travels faster
than the speed of light,
with the possible exception of bad news,
which obeys its own special laws.`;

// get a new mail object
let mail = new Smtp.Mail();
// required fields
.setSender("sender@example.com", "sender name")
.setSubject("Mail subject")
.setReceivers("receiver1@example.com", "receiver 1 name")
.setReceivers("receiver2@example.com", "receiver 2 name")
.setBody(mail_body)

// optional fields
.setReplyTo("reply@example.com", "reply name")
.setCC("cc@example.com", "cc name")
.setBCC("bcc@example.com", "bcc name")

// end of mail object
;

// send the mail
await mailClient?.send(mail);
note

You can omit the second name parameter in setSender, setReceivers, setReplyTo, setCC and setBCC