class

EMail::Message

Inherits Reference / Object

Email message object.

Minimal email with plain text message.

email = EMail::Message.new

# Email headers
email.from "your_addr@example.com"
email.to "to@example.com"
email.subject "Subject of the mail"

# Email plain text email body
email.message <<-EOM
  Message body of the mail.

  --
  Your Signature
  EOM

You can set following preset headers and your own #custom_headers:

  • [*][!] #from
  • [*][?] #to
  • [*][?] #cc
  • [*][?] #bcc
  • [*] #reply_to
  • #return_path
  • #sender
  • #envelope_from
  • [!] #subject

[!] required.

[*] callable multiple times.

[?] required at least one of these recipients.

Add multiple email addresses at once

For From, To, Cc, Bcc, and Reply-To headers, you can add multiple email addresses at once with array of String or EMail::Address.

# Add two email addresses with array of email address strings
email.to ["to1@example.com", "to2@example.com"]

# Notice: Following code will not add two email addresses
#         but only one email address "to1@example.com"
#         that has mailbox name "to2@example.com"
email.to "to1@example.com", "to2@example.com"

or

# Add two email addresses with array of EMail::Address ojects
addr_list = [] of EMail::Address
addr_list << EMail::Address.new("to1@example.com", "to1 name")
addr_list << EMail::Address.new("to2@example.com", "to2 name")
email.to addr_list

Set custom header

email.custom_header "X-Mailer", "Your APP Name"

Set mailbox name with email address

email.from "your_addr@example.com", "your name"

Also, #to, #cc, #bcc, etc...

HTML email with altanative plain text message.

email = EMail::Message.new

# Email headers
email.from "your_addr@example.com"
email.to "to@example.com"
email.subject "Subject of the mail"

# Email plain text email body
email.message <<-EOM
  Message body of the mail.

  --
  Your Signature
  EOM

# Email HTML email body
email.message_html <<-EOM
  <html>
  <body>
  <h1>Subject of the mail<h1>
  <p>Message body of the mail.</p>
  <footer>
  Your Signature
  </footer>
  </body>
  </html>
  EOM

Attache files

email = EMail::Message.new

# Email headers
email.from "your_addr@example.com"
email.to "to@example.com"
email.subject "Subject of the mail"

# Email plain text email body
email.message <<-EOM
  Message body of the mail.

  --
  Your Signature
  EOM

# Attach file to email
email.attach "./photo.jpeg"

Set alternative file name for recipient

email.attach "./photo.jpeg", file_name: "last_year.jpeg"

Set specific MIME type

email.attach "./data", mime_type: "text/csv"

Read attachment file data from IO

email.attach io, file_name: "photo.jpeg"

In this case, file_name argument is required.

Add message resouces

email = EMail::Message.new

# Email headers
email.from "your_addr@example.com"
email.to "to@example.com"
email.subject "Subject of the mail"

# Email plain text email body
email.message <<-EOM
  Message body of the mail.

  --
  Your Signature
  EOM

# Email HTML email body
email.message_html <<-EOM
  <html>
  <body>
  <img src="cid:logo@some.domain">
  <h1>Subject of the mail<h1>
  <p>Message body of the mail.</p>
  <footer>
  Your Signature
  </footer>
  </body>
  </html>
  EOM

# Add message resource
email.message_resource "./logo.png", cid: "logo@some.domain"

#message_resource is lmost same as #attach expect it requires cid argument.

Instance methods

attach(file_path : String, file_name : String | Nil = nil, mime_type : String | Nil = nil)

Attaches the file from given file path.

You can set another file_name for recipients and sprcific mime_type. By default, MIME type will be inferred from extname of the file name.

Source
attach(io : IO, file_name : String, mime_type : String | Nil = nil)

Attaches the file read from given IO.

In this case, file_name argument is required.

Source
bcc(mail_address : String, mailbox_name : String | Nil = nil)

Adds email address to Bcc header.

Call this multiple times to set multiple addresses.

Source
bcc(mail_address : EMail::Address)

Adds email address to Bcc header.

Call this multiple times to set multiple addresses.

Source
bcc(mail_addresses : Array(String))

Adds multiple email addresses to Bcc header at once.

In this method, you cannot set mailbox name.

Source
bcc(mail_addresses : Array(EMail::Address))

Adds multiple email addresses to Bcc header at once.

Source
cc(mail_address : String, mailbox_name : String | Nil = nil)

Adds email address to Cc header.

Call this multiple times to set multiple addresses.

Source
cc(mail_address : EMail::Address)

Adds email address to Cc header.

Call this multiple times to set multiple addresses.

Source
cc(mail_addresses : Array(String))

Adds multiple email addresses to Cc header at once.

In this method, you cannot set mailbox name.

Source
cc(mail_addresses : Array(EMail::Address))

Adds multiple email addresses to Cc header at once.

Source
clear_bcc

Removes all email addresses from Bcc header.

Source
clear_cc

Removes all email addresses from Cc header.

Source
clear_custom_header(name : String)

Removes all custom headers with specific name.

Source
clear_from

Removes all email addresses from From header.

Source
clear_reply_to

Removes all email addresses from Reply-To header.

Source
clear_to

Removes all email addresses from To header.

Source
custom_header(name : String, value : String)

Sets cuntome header you want to set to the message.

Source
envelope_from(mail_address : String)

Set envelope from address.

Source
from(mail_address : String, mailbox_name : String | Nil = nil)

Adds email address to From header.

Call this multiple times to set multiple addresses.

Source
from(mail_address : EMail::Address)

Adds email address to From header.

Call this multiple times to set multiple addresses.

Source
from(mail_addresses : Array(String))

Adds multiple email addresses to From header at once.

In this method, you cannot set mailbox name.

Source
from(mail_addresses : Array(EMail::Address))

Adds multiple email addresses to From header at once.

Source
message(message_body : String)

Sets plain text message body.

Source
message_html(message_body : String)

Sets html text message body.

Source
message_id(header_body : String)

Set Message-Id header.

Source
message_resource(file_path : String, cid : String, file_name : String | Nil = nil, mime_type : String | Nil = nil)

Adds message resource file, such as images or stylesheets for the html message, from given file path.

Almost same as #attach expect this require cid argument.

Source
message_resource(io : IO, cid : String, file_name : String, mime_type : String | Nil = nil)

Adds message resource file, such as images or stylesheets for the html message, read from given IO.

Almost same as #attach expect this require cid argument.

Source
reply_to(mail_address : String, mailbox_name : String | Nil = nil)

Adds email address to Reply-To header.

Call this multiple times to set multiple addresses.

Source
reply_to(mail_address : EMail::Address)

Adds email address to Reply-To header.

Call this multiple times to set multiple addresses.

Source
reply_to(mail_addresses : Array(String))

Adds multiple email addresses to Reply-To header at once.

In this method, you cannot set mailbox name.

Source
reply_to(mail_addresses : Array(EMail::Address))

Adds multiple email addresses to Reply-To header at once.

Source
return_path(mail_address : String, mailbox_name : String | Nil = nil)

Sets email address to Return-Path header.

Source
return_path(mail_address : EMail::Address)

Sets email address to Return-Path header.

Source
sender(mail_address : String, mailbox_name : String | Nil = nil)

Sets email address to Sender header.

Source
sender(mail_address : EMail::Address)

Sets email address to Sender header.

Source
subject(header_body : String)

Set Subject header.

Source
to(mail_address : String, mailbox_name : String | Nil = nil)

Adds email address to To header.

Call this multiple times to set multiple addresses.

Source
to(mail_address : EMail::Address)

Adds email address to To header.

Call this multiple times to set multiple addresses.

Source
to(mail_addresses : Array(String))

Adds multiple email addresses to To header at once.

In this method, you cannot set mailbox name.

Source
to(mail_addresses : Array(EMail::Address))

Adds multiple email addresses to To header at once.

Source
to_s(io : IO)

Appends a short String representation of this object which includes its class name and its object address.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).to_s # => #<Person:0x10a199f20>
Source