class EMail::Message
- EMail::Message
- Reference
- Object
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_header
s:
- [*][!]
#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 ["[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"
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.crInstance Method Summary
-
#attach(file_path : String, file_name : String | Nil = nil, mime_type : String | Nil = nil)
Attaches the file from given file path.
-
#attach(io : IO, file_name : String, mime_type : String | Nil = nil)
Attaches the file read from given IO.
-
#bcc(mail_address : String, mailbox_name : String | Nil = nil)
Adds email address to Bcc header.
-
#bcc(mail_address : EMail::Address)
Adds email address to Bcc header.
-
#bcc(mail_addresses : Array(String))
Adds multiple email addresses to Bcc header at once.
-
#bcc(mail_addresses : Array(EMail::Address))
Adds multiple email addresses to Bcc header at once.
-
#cc(mail_address : String, mailbox_name : String | Nil = nil)
Adds email address to Cc header.
-
#cc(mail_address : EMail::Address)
Adds email address to Cc header.
-
#cc(mail_addresses : Array(String))
Adds multiple email addresses to Cc header at once.
-
#cc(mail_addresses : Array(EMail::Address))
Adds multiple email addresses to Cc header at once.
-
#clear_bcc
Removes all email addresses from Bcc header.
-
#clear_cc
Removes all email addresses from Cc header.
-
#clear_custom_header(name : String)
Removes all custom headers with specific name.
-
#clear_from
Removes all email addresses from From header.
-
#clear_reply_to
Removes all email addresses from Reply-To header.
-
#clear_to
Removes all email addresses from To header.
-
#custom_header(name : String, value : String)
Sets cuntome header you want to set to the message.
-
#envelope_from(mail_address : String)
Set envelope from address.
-
#from(mail_address : String, mailbox_name : String | Nil = nil)
Adds email address to From header.
-
#from(mail_address : EMail::Address)
Adds email address to From header.
-
#from(mail_addresses : Array(String))
Adds multiple email addresses to From header at once.
-
#from(mail_addresses : Array(EMail::Address))
Adds multiple email addresses to From header at once.
-
#message(message_body : String)
Sets plain text message body.
-
#message_html(message_body : String)
Sets html text message body.
-
#message_id(header_body : String)
Set Message-Id header.
-
#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.
-
#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.
-
#reply_to(mail_address : String, mailbox_name : String | Nil = nil)
Adds email address to Reply-To header.
-
#reply_to(mail_address : EMail::Address)
Adds email address to Reply-To header.
-
#reply_to(mail_addresses : Array(String))
Adds multiple email addresses to Reply-To header at once.
-
#reply_to(mail_addresses : Array(EMail::Address))
Adds multiple email addresses to Reply-To header at once.
-
#return_path(mail_address : String, mailbox_name : String | Nil = nil)
Sets email address to Return-Path header.
-
#return_path(mail_address : EMail::Address)
Sets email address to Return-Path header.
-
#sender(mail_address : String, mailbox_name : String | Nil = nil)
Sets email address to Sender header.
-
#sender(mail_address : EMail::Address)
Sets email address to Sender header.
-
#subject(header_body : String)
Set Subject header.
-
#to(mail_address : String, mailbox_name : String | Nil = nil)
Adds email address to To header.
-
#to(mail_address : EMail::Address)
Adds email address to To header.
-
#to(mail_addresses : Array(String))
Adds multiple email addresses to To header at once.
-
#to(mail_addresses : Array(EMail::Address))
Adds multiple email addresses to To header at once.
-
#to_s(io : IO)
Appends a short String representation of this object which includes its class name and its object address.
Instance Method Detail
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.
Attaches the file read from given IO.
In this case, file_name
argument is required.
Adds email address to Bcc header.
Call this multiple times to set multiple addresses.
Adds email address to Bcc header.
Call this multiple times to set multiple addresses.
Adds multiple email addresses to Bcc header at once.
In this method, you cannot set mailbox name.
Adds multiple email addresses to Bcc header at once.
Adds email address to Cc header.
Call this multiple times to set multiple addresses.
Adds email address to Cc header.
Call this multiple times to set multiple addresses.
Adds multiple email addresses to Cc header at once.
In this method, you cannot set mailbox name.
Adds multiple email addresses to Cc header at once.
Sets cuntome header you want to set to the message.
Adds email address to From header.
Call this multiple times to set multiple addresses.
Adds email address to From header.
Call this multiple times to set multiple addresses.
Adds multiple email addresses to From header at once.
In this method, you cannot set mailbox name.
Adds multiple email addresses to From header at once.
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.
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.
Adds email address to Reply-To header.
Call this multiple times to set multiple addresses.
Adds email address to Reply-To header.
Call this multiple times to set multiple addresses.
Adds multiple email addresses to Reply-To header at once.
In this method, you cannot set mailbox name.
Adds multiple email addresses to Reply-To header at once.
Sets email address to Return-Path header.
Sets email address to Sender header.
Adds email address to To header.
Call this multiple times to set multiple addresses.
Adds email address to To header.
Call this multiple times to set multiple addresses.
Adds multiple email addresses to To header at once.
In this method, you cannot set mailbox name.
Adds multiple email addresses to To header at once.
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>