logo

Email

SMTP Hello World

// create simple plain text email
email := Email
{
  to = ["foo@somewhere.com"]
  from = "bob@foo.com"
  subject = "hi"
  body = TextPart { text = "hello world" }
}

// configure smtp mailer and send email
mailer := SmtpClient { host = "mail.foo.com"; username = "bob"; password = "pw" }
mailer.send(email)

SMTP Batch Send

// open a session, send multiple emails, and guarantee close
mailer.open
try
{
  emails.each |Email email| { mailer.send(email) }
}
finally
{
  mailer.close
}

To, CC, BCC

email := Email
{
  to = ["alice@foo.com", "charlie@foo.com"]
  cc = ["theboss@foo.com", "bob@foo.com"]
  bcc = ["zack@foo.com"]
  from = "bob@foo.com"
  subject = "hi"
  body = TextPart { text = "hello world" }
}

HTML Multipart

email := Email
{
  to = ["alice@foo.com"]
  from = "bob@foo.com"
  subject = "html/plain alternative"
  body = MultiPart
  {
    headers["Content-Type"] = "multipart/alternative"
    parts =
    [
      // put plain text first as backup
      TextPart
      {
        text = "this is bold and italics!"
      },

      // put html part last as best alternative
      TextPart
      {
        headers["Content-Type"] = "text/html"
        text = "this is <b>bold</b> and <i>italics</i>!"
      }
    ]
  }
}

File Attachments

email := Email
{
  to = ["alice@foo.com"]
  from = "bob@foo.com"
  subject = "file attachments"
  body = MultiPart
  {
    parts =
    [
      // put body first
      TextPart
      {
        text = "\u00A1Hola Se\u00F1or! This is the body!"
      },

      // attachment 1
      FilePart
      {
        headers["Content-Disposition"] = "inline"
        file = `image1.jpg`.toFile
      },

      // attachment 2
      FilePart
      {
        headers["Content-Disposition"] = "attachment; filename=image2.jpg"
        file = `image2.jpg`.toFile
      }
    ]
  }
}

Debug

// turn on tracing
mailer.log.level = LogLevel.trace

// dump email to stdout
email.encode(Sys.out)