class EMail::Message

Overview

Email message object.

Minimal email with plain text message.

email = EMail::Message.new

# Email headers
email.from "[email protected]"
email.to "[email protected]"
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:

[!] 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 ["[email protected]", "[email protected]"]

# Notice: Following code will not add two email addresses
#         but only one email address "[email protected]"
#         that has mailbox name "[email protected]"
email.to "[email protected]", "[email protected]"

or

# Add two email addresses with array of EMail::Address ojects
addr_list = [] of EMail::Address
addr_list << EMail::Address.new("[email protected]", "to1 name")
addr_list << EMail::Address.new("[email protected]", "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 "[email protected]", "your name"

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

HTML email with altanative plain text message.

email = EMail::Message.new

# Email headers
email.from "[email protected]"
email.to "[email protected]"
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 "[email protected]"
email.to "[email protected]"
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 "[email protected]"
email.to "[email protected]"
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:[email protected]">
  <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: "[email protected]"

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

Defined in:

email/message.cr

Instance Method Summary

Instance Method Detail

def 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.


[View source]
def 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.


[View source]
def bcc(mail_address : String, mailbox_name : String | Nil = nil) #

Adds email address to Bcc header.

Call this multiple times to set multiple addresses.


[View source]
def bcc(mail_address : EMail::Address) #

Adds email address to Bcc header.

Call this multiple times to set multiple addresses.


[View source]
def bcc(mail_addresses : Array(String)) #

Adds multiple email addresses to Bcc header at once.

In this method, you cannot set mailbox name.


[View source]
def bcc(mail_addresses : Array(EMail::Address)) #

Adds multiple email addresses to Bcc header at once.


[View source]
def cc(mail_address : String, mailbox_name : String | Nil = nil) #

Adds email address to Cc header.

Call this multiple times to set multiple addresses.


[View source]
def cc(mail_address : EMail::Address) #

Adds email address to Cc header.

Call this multiple times to set multiple addresses.


[View source]
def cc(mail_addresses : Array(String)) #

Adds multiple email addresses to Cc header at once.

In this method, you cannot set mailbox name.


[View source]
def cc(mail_addresses : Array(EMail::Address)) #

Adds multiple email addresses to Cc header at once.


[View source]
def clear_bcc #

Removes all email addresses from Bcc header.


[View source]
def clear_cc #

Removes all email addresses from Cc header.


[View source]
def clear_custom_header(name : String) #

Removes all custom headers with specific name.


[View source]
def clear_from #

Removes all email addresses from From header.


[View source]
def clear_reply_to #

Removes all email addresses from Reply-To header.


[View source]
def clear_to #

Removes all email addresses from To header.


[View source]
def custom_header(name : String, value : String) #

Sets cuntome header you want to set to the message.


[View source]
def envelope_from(mail_address : String) #

Set envelope from address.


[View source]
def from(mail_address : String, mailbox_name : String | Nil = nil) #

Adds email address to From header.

Call this multiple times to set multiple addresses.


[View source]
def from(mail_address : EMail::Address) #

Adds email address to From header.

Call this multiple times to set multiple addresses.


[View source]
def from(mail_addresses : Array(String)) #

Adds multiple email addresses to From header at once.

In this method, you cannot set mailbox name.


[View source]
def from(mail_addresses : Array(EMail::Address)) #

Adds multiple email addresses to From header at once.


[View source]
def message(message_body : String) #

Sets plain text message body.


[View source]
def message_html(message_body : String) #

Sets html text message body.


[View source]
def message_id(header_body : String) #

Set Message-Id header.


[View source]
def 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.


[View source]
def 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.


[View source]
def 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.


[View source]
def reply_to(mail_address : EMail::Address) #

Adds email address to Reply-To header.

Call this multiple times to set multiple addresses.


[View source]
def reply_to(mail_addresses : Array(String)) #

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

In this method, you cannot set mailbox name.


[View source]
def reply_to(mail_addresses : Array(EMail::Address)) #

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


[View source]
def return_path(mail_address : String, mailbox_name : String | Nil = nil) #

Sets email address to Return-Path header.


[View source]
def return_path(mail_address : EMail::Address) #

Sets email address to Return-Path header.


[View source]
def sender(mail_address : String, mailbox_name : String | Nil = nil) #

Sets email address to Sender header.


[View source]
def sender(mail_address : EMail::Address) #

Sets email address to Sender header.


[View source]
def subject(header_body : String) #

Set Subject header.


[View source]
def to(mail_address : String, mailbox_name : String | Nil = nil) #

Adds email address to To header.

Call this multiple times to set multiple addresses.


[View source]
def to(mail_address : EMail::Address) #

Adds email address to To header.

Call this multiple times to set multiple addresses.


[View source]
def to(mail_addresses : Array(String)) #

Adds multiple email addresses to To header at once.

In this method, you cannot set mailbox name.


[View source]
def to(mail_addresses : Array(EMail::Address)) #

Adds multiple email addresses to To header at once.


[View source]
def to_s(io : IO) #
Description copied from class Reference

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>

[View source]