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.

[*] usable multiple times.

[?] required at least one recipient.

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) #

Attache 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) #

Attache the file read from given IO.

In this case, file_name argument is required.


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

Add email address to Bcc header.


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

Add email address to Bcc header.


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

Add email address to Cc header.


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

Add email address to Cc header.


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

Set 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, sender_name : String | Nil = nil) #

Add email address to From header.


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

Add email address to From header.


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

Set plain text message body.


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

Set 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) #

Add 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) #

Add 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, sender_name : String | Nil = nil) #

Add email address to Reply-To header.


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

Add email address to Reply-To header.


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

Set email address to Return-Path header.


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

Set email address to Return-Path header.


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

Set email address to Sender header.


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

Set email address to Sender header.


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

Set Subject header.


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

Add email address to To header.


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

Add email address to To header.


[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]