Class: Mail::Message
- Inherits:
-
Object
- Object
- Mail::Message
- Defined in:
- lib/mail/message.rb
Overview
The Message class provides a single point of access to all things to do with an email message.
You create a new email message by calling the Mail::Message.new method, or just Mail.new
A Message object by default has the following objects inside it:
-
A Header object which contians all information and settings of the header of the email
-
Body object which contains all parts of the email that are not part of the header, this includes any attachments, body text, mime parts etc.
Per RFC2822
2.1. General Description
At the most basic level, a message is a series of characters. A
message that is conformant with this standard is comprised of
characters with values in the range 1 through 127 and interpreted as
US-ASCII characters [ASCII]. For brevity, this document sometimes
refers to this range of characters as simply "US-ASCII characters".
Note: This standard specifies that messages are made up of characters
in the US-ASCII range of 1 through 127. There are other documents,
specifically the MIME document series [RFC2045, RFC2046, RFC2047,
RFC2048, RFC2049], that extend this standard to allow for values
outside of that range. Discussion of those mechanisms is not within
the scope of this standard.
Messages are divided into lines of characters. A line is a series of
characters that is delimited with the two characters carriage-return
and line-feed; that is, the carriage return (CR) character (ASCII
value 13) followed immediately by the line feed (LF) character (ASCII
value 10). (The carriage-return/line-feed pair is usually written in
this document as "CRLF".)
A message consists of header fields (collectively called "the header
of the message") followed, optionally, by a body. The header is a
sequence of lines of characters with special syntax as defined in
this standard. The body is simply a sequence of characters that
follows the header and is separated from the header by an empty line
(i.e., a line with nothing preceding the CRLF).
Direct Known Subclasses
Constant Summary
Constants included from Patterns
Patterns::ATOM_UNSAFE, Patterns::CONTROL_CHAR, Patterns::CRLF, Patterns::FIELD_BODY, Patterns::FIELD_LINE, Patterns::FIELD_NAME, Patterns::FWS, Patterns::HEADER_LINE, Patterns::PHRASE_UNSAFE, Patterns::TEXT, Patterns::TOKEN_UNSAFE, Patterns::WSP
Instance Attribute Summary collapse
-
#delivery_handler ⇒ Object
If you assign a delivery handler, mail will call :deliver_mail on the object you assign to delivery_handler, it will pass itself as the single argument.
-
#perform_deliveries ⇒ Object
If set to false, mail will go through the motions of doing a delivery, but not actually call the delivery method or append the mail object to the Mail.deliveries collection.
-
#raise_delivery_errors ⇒ Object
If set to false, mail will silently catch and ignore any exceptions raised through attempting to deliver an email.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Provides the operator needed for sort et al.
-
#==(other) ⇒ Object
Two emails are the same if they have the same fields and body contents.
-
#[](name) ⇒ Object
Allows you to read an arbitrary header.
-
#[]=(name, value) ⇒ Object
Allows you to add an arbitrary header.
- #action ⇒ Object
-
#add_charset ⇒ Object
Adds a content type and charset if the body is US-ASCII.
-
#add_content_transfer_encoding ⇒ Object
Adds a content transfer encoding.
-
#add_content_type ⇒ Object
Adds a content type and charset if the body is US-ASCII.
-
#add_date(date_val = '') ⇒ Object
Creates a new empty Date field and inserts it in the correct order into the Header.
-
#add_file(values) ⇒ Object
Adds a file to the message.
-
#add_message_id(msg_id_val = '') ⇒ Object
Creates a new empty Message-ID field and inserts it in the correct order into the Header.
-
#add_mime_version(ver_val = '') ⇒ Object
Creates a new empty Mime Version field and inserts it in the correct order into the Header.
-
#add_part(part) ⇒ Object
Adds a part to the parts list or creates the part list.
-
#add_transfer_encoding ⇒ Object
:nodoc:.
- #all_parts ⇒ Object
-
#attachment ⇒ Object
Returns the attachment data if there is any.
-
#attachment? ⇒ Boolean
Returns true if this part is an attachment.
-
#attachments ⇒ Object
Returns an AttachmentsList object, which holds all of the attachments in the receiver object (either the entier email or a part within) and all of it’s descendants.
-
#bcc(val = nil) ⇒ Object
Returns the Bcc value of the mail object as an array of strings of address specs.
-
#bcc=(val) ⇒ Object
Sets the Bcc value of the mail object, pass in a string of the field.
-
#bcc_addrs ⇒ Object
Returns an array of addresses (the encoded value) in the Bcc field, if no Bcc field, returns an empty array.
-
#body(value = nil) ⇒ Object
Returns the body of the message object.
-
#body=(value) ⇒ Object
Sets the body object of the message object.
- #bounced? ⇒ Boolean
-
#boundary ⇒ Object
Returns the current boundary for this message part.
-
#cc(val = nil) ⇒ Object
Returns the Cc value of the mail object as an array of strings of address specs.
-
#cc=(val) ⇒ Object
Sets the Cc value of the mail object, pass in a string of the field.
-
#cc_addrs ⇒ Object
Returns an array of addresses (the encoded value) in the Cc field, if no Cc field, returns an empty array.
-
#charset ⇒ Object
Returns the character set defined in the content type field.
-
#charset=(value) ⇒ Object
Sets the charset to the supplied value.
- #comments(val = nil) ⇒ Object
- #comments=(val) ⇒ Object
- #content_description(val = nil) ⇒ Object
- #content_description=(val) ⇒ Object
- #content_disposition(val = nil) ⇒ Object
- #content_disposition=(val) ⇒ Object
- #content_id(val = nil) ⇒ Object
- #content_id=(val) ⇒ Object
- #content_location(val = nil) ⇒ Object
- #content_location=(val) ⇒ Object
- #content_transfer_encoding(val = nil) ⇒ Object
- #content_transfer_encoding=(val) ⇒ Object
- #content_type(val = nil) ⇒ Object
- #content_type=(val) ⇒ Object
-
#content_type_parameters ⇒ Object
Returns the content type parameters.
- #convert_to_multipart ⇒ Object
- #date(val = nil) ⇒ Object
- #date=(val) ⇒ Object
- #decode_body ⇒ Object
- #decoded ⇒ Object
-
#default(sym, val = nil) ⇒ Object
Returns the default value of the field requested as a symbol.
-
#deliver ⇒ Object
Delivers an mail object.
-
#deliver! ⇒ Object
This method bypasses checking perform_deliveries and raise_delivery_errors, so use with caution.
- #delivery_method(method = nil, settings = {}) ⇒ Object
-
#delivery_status_part ⇒ Object
returns the part in a multipart/report email that has the content-type delivery-status.
-
#delivery_status_report? ⇒ Boolean
Returns true if the message is a multipart/report; report-type=delivery-status;.
-
#destinations ⇒ Object
Returns the list of addresses this message should be sent to by collecting the addresses off the to, cc and bcc fields.
- #diagnostic_code ⇒ Object
- #encode! ⇒ Object
-
#encoded ⇒ Object
Outputs an encoded string representation of the mail message including all headers, attachments, etc.
- #envelope_date ⇒ Object
- #envelope_from ⇒ Object
- #error_status ⇒ Object
-
#filename ⇒ Object
Returns the filename of the attachment.
- #final_recipient ⇒ Object
- #find_first_mime_type(mt) ⇒ Object
-
#from(val = nil) ⇒ Object
Returns the From value of the mail object as an array of strings of address specs.
-
#from=(val) ⇒ Object
Sets the From value of the mail object, pass in a string of the field.
-
#from_addrs ⇒ Object
Returns an array of addresses (the encoded value) in the From field, if no From field, returns an empty array.
- #has_attachments? ⇒ Boolean
- #has_charset? ⇒ Boolean
- #has_content_transfer_encoding? ⇒ Boolean
- #has_content_type? ⇒ Boolean
-
#has_date? ⇒ Boolean
Returns true if the message has a Date field, the field may or may not have a value, but the field exists or not.
-
#has_message_id? ⇒ Boolean
Returns true if the message has a message ID field, the field may or may not have a value, but the field exists or not.
-
#has_mime_version? ⇒ Boolean
Returns true if the message has a Date field, the field may or may not have a value, but the field exists or not.
-
#has_transfer_encoding? ⇒ Boolean
:nodoc:.
-
#header(value = nil) ⇒ Object
Returns the header object of the message object.
-
#header=(value) ⇒ Object
Sets the header of the message object.
-
#header_fields ⇒ Object
Returns an FieldList of all the fields in the header in the order that they appear in the header.
-
#headers(hash = {}) ⇒ Object
Provides a way to set custom headers, by passing in a hash.
-
#html_part(&block) ⇒ Object
Accessor for html_part.
-
#html_part=(msg = nil) ⇒ Object
Helper to add a html part to a multipart/alternative email.
- #in_reply_to(val = nil) ⇒ Object
- #in_reply_to=(val) ⇒ Object
- #inform_observers ⇒ Object
-
#initialize(*args, &block) ⇒ Message
constructor
Making an email.
- #keywords(val = nil) ⇒ Object
- #keywords=(val) ⇒ Object
-
#main_type ⇒ Object
Returns the main content type.
- #message_content_type ⇒ Object
-
#message_id(val = nil) ⇒ Object
Returns the Message-ID of the mail object.
-
#message_id=(val) ⇒ Object
Sets the Message-ID.
-
#method_missing(name, *args, &block) ⇒ Object
Method Missing in this implementation allows you to set any of the standard fields directly as you would the “to”, “subject” etc.
-
#mime_parameters ⇒ Object
Returns the content type parameters.
-
#mime_type ⇒ Object
Returns the mime type of part we are on, this is taken from the content-type header.
-
#mime_version(val = nil) ⇒ Object
Returns the mime version of the email as a string.
-
#mime_version=(val) ⇒ Object
Sets the mime version of the email by accepting a string.
-
#multipart? ⇒ Boolean
Returns true if the message is multipart.
-
#multipart_report? ⇒ Boolean
Returns true if the message is a multipart/report.
-
#part(params = {}) {|new_part| ... } ⇒ Object
Allows you to add a part in block form to an existing mail message object.
-
#parts ⇒ Object
Returns a parts list object of all the parts in the message.
-
#raw_envelope ⇒ Object
The raw_envelope is the From [email protected] Mon May 2 16:07:05 2009 type field that you can see at the top of any email that has come from a mailbox.
-
#raw_source ⇒ Object
Provides access to the raw source of the message as it was when it was instantiated.
- #read ⇒ Object
-
#ready_to_send! ⇒ Object
Encodes the message, calls encode on all it’s parts, gets an email message ready to send.
- #received(val = nil) ⇒ Object
- #received=(val) ⇒ Object
- #references(val = nil) ⇒ Object
- #references=(val) ⇒ Object
- #register_for_delivery_notification(observer) ⇒ Object
- #remote_mta ⇒ Object
-
#reply_to(val = nil) ⇒ Object
Returns the Reply-To value of the mail object as an array of strings of address specs.
-
#reply_to=(val) ⇒ Object
Sets the Reply-To value of the mail object, pass in a string of the field.
-
#resent_bcc(val = nil) ⇒ Object
Returns the Resent-Bcc value of the mail object as an array of strings of address specs.
-
#resent_bcc=(val) ⇒ Object
Sets the Resent-Bcc value of the mail object, pass in a string of the field.
-
#resent_cc(val = nil) ⇒ Object
Returns the Resent-Cc value of the mail object as an array of strings of address specs.
-
#resent_cc=(val) ⇒ Object
Sets the Resent-Cc value of the mail object, pass in a string of the field.
- #resent_date(val = nil) ⇒ Object
- #resent_date=(val) ⇒ Object
-
#resent_from(val = nil) ⇒ Object
Returns the Resent-From value of the mail object as an array of strings of address specs.
-
#resent_from=(val) ⇒ Object
Sets the Resent-From value of the mail object, pass in a string of the field.
- #resent_message_id(val = nil) ⇒ Object
- #resent_message_id=(val) ⇒ Object
-
#resent_sender(val = nil) ⇒ Object
Returns the Resent-Sender value of the mail object, as a single string of an address spec.
-
#resent_sender=(val) ⇒ Object
Sets the Resent-Sender value of the mail object, pass in a string of the field.
-
#resent_to(val = nil) ⇒ Object
Returns the Resent-To value of the mail object as an array of strings of address specs.
-
#resent_to=(val) ⇒ Object
Sets the Resent-To value of the mail object, pass in a string of the field.
- #retryable? ⇒ Boolean
-
#return_path(val = nil) ⇒ Object
Returns the return path of the mail object, or sets it if you pass a string.
-
#return_path=(val) ⇒ Object
Sets the return path of the object.
-
#sender(val = nil) ⇒ Object
Returns the Sender value of the mail object, as a single string of an address spec.
-
#sender=(val) ⇒ Object
Sets the Sender value of the mail object, pass in a string of the field.
-
#set_envelope(val) ⇒ Object
Sets the envelope from for the email.
-
#sub_type ⇒ Object
Returns the sub content type.
-
#subject(val = nil) ⇒ Object
Returns the decoded value of the subject field, as a single string.
-
#subject=(val) ⇒ Object
Sets the Subject value of the mail object, pass in a string of the field.
-
#text_part(&block) ⇒ Object
Accessor for text_part.
-
#text_part=(msg = nil) ⇒ Object
Helper to add a text part to a multipart/alternative email.
-
#to(val = nil) ⇒ Object
Returns the To value of the mail object as an array of strings of address specs.
-
#to=(val) ⇒ Object
Sets the To value of the mail object, pass in a string of the field.
-
#to_addrs ⇒ Object
Returns an array of addresses (the encoded value) in the To field, if no To field, returns an empty array.
- #to_s ⇒ Object
-
#transfer_encoding ⇒ Object
:nodoc:.
Methods included from Utilities
Methods included from Patterns
Constructor Details
#initialize(*args, &block) ⇒ Message
Making an email
You can make an new mail object via a block, passing a string, file or direct assignment.
Making an email via a block
mail = Mail.new do
from '[email protected]'
to '[email protected]'
subject 'This is a test email'
body File.read('body.txt')
end
mail.to_s #=> "From: [email protected]\r\nTo: you@...
Making an email via passing a string
mail = Mail.new("To: [email protected]\r\nSubject: Hello\r\n\r\nHi there!")
mail.body.to_s #=> 'Hi there!'
mail.subject #=> 'Hello'
mail.to #=> '[email protected]'
Making an email from a file
mail = Mail.read('path/to/file.eml')
mail.body.to_s #=> 'Hi there!'
mail.subject #=> 'Hello'
mail.to #=> '[email protected]'
Making an email via assignment
You can assign values to a mail object via four approaches:
Examples:
mail = Mail.new
mail['from'] = '[email protected]'
mail[:to] = '[email protected]'
mail.subject 'This is a test email'
mail.body = 'This is a body'
mail.to_s #=> "From: [email protected]\r\nTo: you@...
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/mail/message.rb', line 98 def initialize(*args, &block) @body = nil @text_part = nil @html_part = nil @perform_deliveries = true @raise_delivery_errors = true @delivery_handler = nil @delivery_method = Mail.delivery_method.dup @delivery_notification_observers = [] if args.flatten.first.respond_to?(:each_pair) init_with_hash(args.flatten.first) else init_with_string(args.flatten[0].to_s.strip) end if block_given? instance_eval(&block) end self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Method Missing in this implementation allows you to set any of the standard fields directly as you would the “to”, “subject” etc.
Those fields used most often (to, subject et al) are given their own method for ease of documentation and also to avoid the hook call to method missing.
This will only catch the known fields listed in:
Mail::Field::KNOWN_FIELDS
as per RFC 2822, any ruby string or method name could pretty much be a field name, so we don’t want to just catch ANYTHING sent to a message object and interpret it as a header.
This method provides all three types of header call to set, read and explicitly set with the = operator
Examples:
mail.comments = 'These are some comments'
mail.comments #=> 'These are some comments'
mail.comments 'These are other comments'
mail.comments #=> 'These are other comments'
mail.date = 'Tue, 1 Jul 2003 10:52:37 +0200'
mail.date.to_s #=> 'Tue, 1 Jul 2003 10:52:37 +0200'
mail.date 'Tue, 1 Jul 2003 10:52:37 +0200'
mail.date.to_s #=> 'Tue, 1 Jul 2003 10:52:37 +0200'
mail.resent_msg_id = '<1234@resent_msg_id.lindsaar.net>'
mail.resent_msg_id #=> '<1234@resent_msg_id.lindsaar.net>'
mail.resent_msg_id '<4567@resent_msg_id.lindsaar.net>'
mail.resent_msg_id #=> '<4567@resent_msg_id.lindsaar.net>'
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 |
# File 'lib/mail/message.rb', line 1184 def method_missing(name, *args, &block) #:nodoc: # Only take the structured fields, as we could take _anything_ really # as it could become an optional field... "but therin lies the dark side" field_name = underscoreize(name).chomp("=") if Mail::Field::KNOWN_FIELDS.include?(field_name) if args.empty? header[field_name] else header[field_name] = args.first end else super # otherwise pass it on end #:startdoc: end |
Instance Attribute Details
#delivery_handler ⇒ Object
If you assign a delivery handler, mail will call :deliver_mail on the object you assign to delivery_handler, it will pass itself as the single argument.
If you define a delivery_handler, then you are responsible for the following actions in the delivery cycle:
-
Appending the mail object to Mail.deliveries as you see fit.
-
Checking the mail.perform_deliveries flag to decide if you should actually call :deliver! the mail object or not.
-
Checking the mail.raise_delivery_errors flag to decide if you should raise delivery errors if they occur.
-
Actually calling :deliver! (with the bang) on the mail object to get it to deliver itself.
A simplest implementation of a delivery_handler would be
class MyObject
def initialize
@mail = Mail.new('To: [email protected]')
@mail.delivery_handler = self
end
attr_accessor :mail
def deliver_mail(mail)
yield
end
end
Then doing:
obj = MyObject.new
obj.mail.deliver
Would cause Mail to call obj.deliver_mail passing itself as a parameter, which then can just yield and let Mail do it’s own private do_delivery method.
163 164 165 |
# File 'lib/mail/message.rb', line 163 def delivery_handler @delivery_handler end |
#perform_deliveries ⇒ Object
If set to false, mail will go through the motions of doing a delivery, but not actually call the delivery method or append the mail object to the Mail.deliveries collection. Useful for testing.
Mail.deliveries.size #=> 0
mail.delivery_method :smtp
mail.perform_deliveries = false
mail.deliver # Mail::SMTP not called here
Mail.deliveries.size #=> 0
If you want to test and query the Mail.deliveries collection to see what mail you sent, you should set perform_deliveries to true and use the :test mail delivery_method:
Mail.deliveries.size #=> 0
mail.delivery_method :test
mail.perform_deliveries = true
mail.deliver
Mail.deliveries.size #=> 1
This setting is ignored by mail (though still available as a flag) if you define a delivery_handler
187 188 189 |
# File 'lib/mail/message.rb', line 187 def perform_deliveries @perform_deliveries end |
#raise_delivery_errors ⇒ Object
If set to false, mail will silently catch and ignore any exceptions raised through attempting to deliver an email.
This setting is ignored by mail (though still available as a flag) if you define a delivery_handler
194 195 196 |
# File 'lib/mail/message.rb', line 194 def raise_delivery_errors @raise_delivery_errors end |
Instance Method Details
#<=>(other) ⇒ Object
Provides the operator needed for sort et al.
Compares this mail object with another mail object, this is done by date, so an email that is older than another will appear first.
Example:
mail1 = Mail.new do
date(Time.now)
end
mail2 = Mail.new do
date(Time.now - 86400) # 1 day older
end
[mail2, mail1].sort #=> [mail2, mail1]
258 259 260 261 262 263 264 |
# File 'lib/mail/message.rb', line 258 def <=>(other) if other.nil? 1 else self.date <=> other.date end end |
#==(other) ⇒ Object
Two emails are the same if they have the same fields and body contents. One gotcha here is that Mail will insert Message-IDs when calling encoded, so doing mail1.encoded == mail2.encoded is most probably not going to return what you think as the assigned Message-IDs by Mail (if not already defined as the same) will ensure that the two objects are unique, and this comparison will ALWAYS return false.
So the == operator has been defined like so: Two messages are the same if they have the same content, ignoring the Message-ID field, unless BOTH emails have a defined and different Message-ID value, then they are false.
So, in practice the == operator works like this:
m1 = Mail.new("Subject: Hello\r\n\r\nHello")
m2 = Mail.new("Subject: Hello\r\n\r\nHello")
m1 == m2 #=> true
m1 = Mail.new("Subject: Hello\r\n\r\nHello")
m2 = Mail.new("Message-ID: <1234@test>\r\nSubject: Hello\r\n\r\nHello")
m1 == m2 #=> true
m1 = Mail.new("Message-ID: <1234@test>\r\nSubject: Hello\r\n\r\nHello")
m2 = Mail.new("Subject: Hello\r\n\r\nHello")
m1 == m2 #=> true
m1 = Mail.new("Message-ID: <1234@test>\r\nSubject: Hello\r\n\r\nHello")
m2 = Mail.new("Message-ID: <1234@test>\r\nSubject: Hello\r\n\r\nHello")
m1 == m2 #=> true
m1 = Mail.new("Message-ID: <1234@test>\r\nSubject: Hello\r\n\r\nHello")
m2 = Mail.new("Message-ID: <DIFFERENT@test>\r\nSubject: Hello\r\n\r\nHello")
m1 == m2 #=> false
297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/mail/message.rb', line 297 def ==(other) return false unless other.respond_to?(:encoded) if self. && other. result = (self.encoded == other.encoded) else , = self., other. self., other. = '<temp@test>', '<temp@test>' result = self.encoded == other.encoded self., other. = "<#{}>", "<#{}>" result end end |
#[](name) ⇒ Object
Allows you to read an arbitrary header
Example:
mail['foo'] = '1234'
mail['foo'].to_s #=> '1234'
1141 1142 1143 |
# File 'lib/mail/message.rb', line 1141 def [](name) header[underscoreize(name)] end |
#[]=(name, value) ⇒ Object
Allows you to add an arbitrary header
Example:
mail['foo'] = '1234'
mail['foo'].to_s #=> '1234'
1125 1126 1127 1128 1129 1130 1131 1132 1133 |
# File 'lib/mail/message.rb', line 1125 def []=(name, value) if name.to_s == 'body' self.body = value elsif name.to_s =~ /content[-_]type/i header[underscoreize(name)] = value else header[underscoreize(name)] = value end end |
#action ⇒ Object
1385 1386 1387 |
# File 'lib/mail/message.rb', line 1385 def action delivery_status_part and delivery_status_part.action end |
#add_charset ⇒ Object
Adds a content type and charset if the body is US-ASCII
Otherwise raises a warning
1282 1283 1284 1285 1286 1287 1288 1289 1290 |
# File 'lib/mail/message.rb', line 1282 def add_charset if body.only_us_ascii? header[:content_type].parameters['charset'] = 'US-ASCII' else warning = "Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect.\n" STDERR.puts(warning) header[:content_type].parameters['charset'] = 'UTF-8' end end |
#add_content_transfer_encoding ⇒ Object
Adds a content transfer encoding
Otherwise raises a warning
1295 1296 1297 1298 1299 1300 1301 1302 1303 |
# File 'lib/mail/message.rb', line 1295 def add_content_transfer_encoding if body.only_us_ascii? header[:content_transfer_encoding] = '7bit' else warning = "Non US-ASCII detected and no content-transfer-encoding defined.\nDefaulting to 8bit, set your own if this is incorrect.\n" STDERR.puts(warning) header[:content_transfer_encoding] = '8bit' end end |
#add_content_type ⇒ Object
Adds a content type and charset if the body is US-ASCII
Otherwise raises a warning
1275 1276 1277 |
# File 'lib/mail/message.rb', line 1275 def add_content_type header[:content_type] = 'text/plain' end |
#add_date(date_val = '') ⇒ Object
Creates a new empty Date field and inserts it in the correct order into the Header. The DateField object will automatically generate DateTime.now’s date if you try and encode it or output it to_s without specifying a date yourself.
It will preserve any date you specify if you do.
1258 1259 1260 |
# File 'lib/mail/message.rb', line 1258 def add_date(date_val = '') header['date'] = date_val end |
#add_file(values) ⇒ Object
Adds a file to the message. You have two options with this method, you can just pass in the absolute path to the file you want and Mail will read the file, get the filename from the path you pass in and guess the mime type, or you can pass in the filename as a string, and pass in the file content as a blob.
Example:
m = Mail.new
m.add_file('/path/to/filename.png')
m = Mail.new
m.add_file(:filename => 'filename.png', :content => File.read('/path/to/file.jpg'))
Note also that if you add a file to an existing message, Mail will convert that message to a MIME multipart email, moving whatever plain text body you had into it’s own text plain part.
Example:
m = Mail.new do
body 'this is some text'
end
m.multipart? #=> false
m.add_file('/path/to/filename.png')
m.multipart? #=> true
m.parts.first.content_type.content_type #=> 'text/plain'
m.parts.last.content_type.content_type #=> 'image/png'
See also #attachments
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 |
# File 'lib/mail/message.rb', line 1566 def add_file(values) convert_to_multipart unless self.multipart? || self.body.decoded.blank? add_multipart_mixed_header if values.is_a?(String) basename = File.basename(values) filedata = File.read(values) else basename = values[:filename] filedata = values[:content] || File.read(values[:filename]) end self.[basename] = filedata end |
#add_message_id(msg_id_val = '') ⇒ Object
Creates a new empty Message-ID field and inserts it in the correct order into the Header. The MessageIdField object will automatically generate a unique message ID if you try and encode it or output it to_s without specifying a message id.
It will preserve the message ID you specify if you do.
1248 1249 1250 |
# File 'lib/mail/message.rb', line 1248 def (msg_id_val = '') header['message-id'] = msg_id_val end |
#add_mime_version(ver_val = '') ⇒ Object
Creates a new empty Mime Version field and inserts it in the correct order into the Header. The MimeVersion object will automatically generate DateTime.now’s date if you try and encode it or output it to_s without specifying a date yourself.
It will preserve any date you specify if you do.
1268 1269 1270 |
# File 'lib/mail/message.rb', line 1268 def add_mime_version(ver_val = '') header['mime-version'] = ver_val end |
#add_part(part) ⇒ Object
Adds a part to the parts list or creates the part list
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 |
# File 'lib/mail/message.rb', line 1510 def add_part(part) if body.parts.empty? && !self.body.decoded.blank? @text_part = Mail::Part.new('Content-Type: text/plain;') @text_part.body = body.decoded self.body << @text_part add_multipart_alternate_header end add_boundary self.body << part end |
#add_transfer_encoding ⇒ Object
:nodoc:
1305 1306 1307 1308 |
# File 'lib/mail/message.rb', line 1305 def add_transfer_encoding # :nodoc: STDERR.puts(":add_transfer_encoding is deprecated in Mail 1.4.3. Please use add_content_transfer_encoding\n#{caller}") add_content_transfer_encoding end |
#all_parts ⇒ Object
1653 1654 1655 |
# File 'lib/mail/message.rb', line 1653 def all_parts parts.map { |p| [p, p.all_parts] }.flatten end |
#attachment ⇒ Object
Returns the attachment data if there is any
1644 1645 1646 |
# File 'lib/mail/message.rb', line 1644 def @attachment end |
#attachment? ⇒ Boolean
Returns true if this part is an attachment
1639 1640 1641 |
# File 'lib/mail/message.rb', line 1639 def end |
#attachments ⇒ Object
Returns an AttachmentsList object, which holds all of the attachments in the receiver object (either the entier email or a part within) and all of it’s descendants.
It also allows you to add attachments to the mail object directly, like so:
mail.['filename.jpg'] = File.read('/path/to/filename.jpg')
If you do this, then Mail will take the file name and work out the mime type set the Content-Type, Content-Disposition, Content-Transfer-Encoding and base64 encode the contents of the attachment all for you.
You can also specify overrides if you want by passing a hash instead of a string:
mail.['filename.jpg'] = {:mime_type => 'application/x-gzip',
:content => File.read('/path/to/filename.jpg')}
If you want to use a different encoding than Base64, you can pass an encoding in, but then it is up to you to pass in the content pre-encoded, and don’t expect Mail to know how to decode this data:
file_content = SpecialEncode(File.read('/path/to/filename.jpg'))
mail.['filename.jpg'] = {:mime_type => 'application/x-gzip',
:encoding => 'SpecialEncoding',
:content => file_content }
You can also search for specific attachments:
# By Filename
mail.['filename.jpg'] #=> Mail::Part object or nil
# or by index
mail.[0] #=> Mail::Part (first attachment)
1453 1454 1455 |
# File 'lib/mail/message.rb', line 1453 def parts. end |
#bcc(val = nil) ⇒ Object
Returns the Bcc value of the mail object as an array of strings of address specs.
Example:
mail.bcc = 'Mikel <[email protected]>'
mail.bcc #=> ['[email protected]']
mail.bcc = 'Mikel <[email protected]>, [email protected]'
mail.bcc #=> ['[email protected]', '[email protected]']
Also allows you to set the value by passing a value as a parameter
Example:
mail.bcc 'Mikel <[email protected]>'
mail.bcc #=> ['[email protected]']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.bcc 'Mikel <[email protected]>'
mail.bcc << '[email protected]'
mail.bcc #=> ['[email protected]', '[email protected]']
401 402 403 |
# File 'lib/mail/message.rb', line 401 def bcc( val = nil ) default :bcc, val end |
#bcc=(val) ⇒ Object
Sets the Bcc value of the mail object, pass in a string of the field
Example:
mail.bcc = 'Mikel <[email protected]>'
mail.bcc #=> ['[email protected]']
mail.bcc = 'Mikel <[email protected]>, [email protected]'
mail.bcc #=> ['[email protected]', '[email protected]']
413 414 415 |
# File 'lib/mail/message.rb', line 413 def bcc=( val ) header[:bcc] = val end |
#bcc_addrs ⇒ Object
Returns an array of addresses (the encoded value) in the Bcc field, if no Bcc field, returns an empty array
1115 1116 1117 |
# File 'lib/mail/message.rb', line 1115 def bcc_addrs bcc ? [bcc].flatten : [] end |
#body(value = nil) ⇒ Object
Returns the body of the message object. Or, if passed a parameter sets the value.
Example:
mail = Mail::Message.new('To: mikel\r\n\r\nThis is the body')
mail.body #=> #<Mail::Body:0x13919c @raw_source="This is the bo...
mail.body 'This is another body'
mail.body #=> #<Mail::Body:0x13919c @raw_source="This is anothe...
1072 1073 1074 1075 1076 1077 1078 1079 |
# File 'lib/mail/message.rb', line 1072 def body(value = nil) if value self.body = value add_encoding_to_body else @body end end |
#body=(value) ⇒ Object
Sets the body object of the message object.
Example:
mail.body = 'This is the body'
mail.body #=> #<Mail::Body:0x13919c @raw_source="This is the bo...
You can also reset the body of an Message object by setting body to nil
Example:
mail.body = 'this is the body'
mail.body.encoded #=> 'this is the body'
mail.body = nil
mail.body.encoded #=> ''
If you try and set the body of an email that is a multipart email, then instead of deleting all the parts of your email, mail will add a text/plain part to your email:
mail.add_file 'somefilename.png'
mail.parts.length #=> 1
mail.body = "This is a body"
mail.parts.length #=> 2
mail.parts.last.content_type.content_type #=> 'This is a body'
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 |
# File 'lib/mail/message.rb', line 1050 def body=(value) case when value == nil @body = Mail::Body.new('') when @body && !@body.parts.empty? @body << Mail::Part.new(value) else @body = Mail::Body.new(value) end add_encoding_to_body end |
#bounced? ⇒ Boolean
1381 1382 1383 |
# File 'lib/mail/message.rb', line 1381 def bounced? delivery_status_part and delivery_status_part.bounced? end |
#boundary ⇒ Object
Returns the current boundary for this message part
1410 1411 1412 |
# File 'lib/mail/message.rb', line 1410 def boundary content_type_parameters ? content_type_parameters['boundary'] : nil end |
#cc(val = nil) ⇒ Object
Returns the Cc value of the mail object as an array of strings of address specs.
Example:
mail.cc = 'Mikel <[email protected]>'
mail.cc #=> ['[email protected]']
mail.cc = 'Mikel <[email protected]>, [email protected]'
mail.cc #=> ['[email protected]', '[email protected]']
Also allows you to set the value by passing a value as a parameter
Example:
mail.cc 'Mikel <[email protected]>'
mail.cc #=> ['[email protected]']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.cc 'Mikel <[email protected]>'
mail.cc << '[email protected]'
mail.cc #=> ['[email protected]', '[email protected]']
442 443 444 |
# File 'lib/mail/message.rb', line 442 def cc( val = nil ) default :cc, val end |
#cc=(val) ⇒ Object
Sets the Cc value of the mail object, pass in a string of the field
Example:
mail.cc = 'Mikel <[email protected]>'
mail.cc #=> ['[email protected]']
mail.cc = 'Mikel <[email protected]>, [email protected]'
mail.cc #=> ['[email protected]', '[email protected]']
454 455 456 |
# File 'lib/mail/message.rb', line 454 def cc=( val ) header[:cc] = val end |
#cc_addrs ⇒ Object
Returns an array of addresses (the encoded value) in the Cc field, if no Cc field, returns an empty array
1109 1110 1111 |
# File 'lib/mail/message.rb', line 1109 def cc_addrs cc ? [cc].flatten : [] end |
#charset ⇒ Object
Returns the character set defined in the content type field
1326 1327 1328 |
# File 'lib/mail/message.rb', line 1326 def charset content_type ? content_type_parameters['charset'] : nil end |
#charset=(value) ⇒ Object
Sets the charset to the supplied value. Will set the content type to text/plain if it does not already exist
1332 1333 1334 1335 1336 1337 1338 |
# File 'lib/mail/message.rb', line 1332 def charset=(value) if content_type content_type_parameters['charset'] = value else self.content_type ['text', 'plain', {'charset' => value}] end end |
#comments(val = nil) ⇒ Object
458 459 460 |
# File 'lib/mail/message.rb', line 458 def comments( val = nil ) default :comments, val end |
#comments=(val) ⇒ Object
462 463 464 |
# File 'lib/mail/message.rb', line 462 def comments=( val ) header[:comments] = val end |
#content_description(val = nil) ⇒ Object
466 467 468 |
# File 'lib/mail/message.rb', line 466 def content_description( val = nil ) default :content_description, val end |
#content_description=(val) ⇒ Object
470 471 472 |
# File 'lib/mail/message.rb', line 470 def content_description=( val ) header[:content_description] = val end |
#content_disposition(val = nil) ⇒ Object
474 475 476 |
# File 'lib/mail/message.rb', line 474 def content_disposition( val = nil ) default :content_disposition, val end |
#content_disposition=(val) ⇒ Object
478 479 480 |
# File 'lib/mail/message.rb', line 478 def content_disposition=( val ) header[:content_disposition] = val end |
#content_id(val = nil) ⇒ Object
482 483 484 |
# File 'lib/mail/message.rb', line 482 def content_id( val = nil ) default :content_id, val end |
#content_id=(val) ⇒ Object
486 487 488 |
# File 'lib/mail/message.rb', line 486 def content_id=( val ) header[:content_id] = val end |
#content_location(val = nil) ⇒ Object
490 491 492 |
# File 'lib/mail/message.rb', line 490 def content_location( val = nil ) default :content_location, val end |
#content_location=(val) ⇒ Object
494 495 496 |
# File 'lib/mail/message.rb', line 494 def content_location=( val ) header[:content_location] = val end |
#content_transfer_encoding(val = nil) ⇒ Object
498 499 500 |
# File 'lib/mail/message.rb', line 498 def content_transfer_encoding( val = nil ) default :content_transfer_encoding, val end |
#content_transfer_encoding=(val) ⇒ Object
502 503 504 |
# File 'lib/mail/message.rb', line 502 def content_transfer_encoding=( val ) header[:content_transfer_encoding] = val end |
#content_type(val = nil) ⇒ Object
506 507 508 |
# File 'lib/mail/message.rb', line 506 def content_type( val = nil ) default :content_type, val end |
#content_type=(val) ⇒ Object
510 511 512 |
# File 'lib/mail/message.rb', line 510 def content_type=( val ) header[:content_type] = val end |
#content_type_parameters ⇒ Object
Returns the content type parameters
1357 1358 1359 |
# File 'lib/mail/message.rb', line 1357 def content_type_parameters has_content_type? ? header[:content_type].parameters : nil end |
#convert_to_multipart ⇒ Object
1579 1580 1581 1582 1583 1584 1585 |
# File 'lib/mail/message.rb', line 1579 def convert_to_multipart text = @body.decoded self.body = '' text_part = Mail::Part.new({:content_type => 'text/plain;', :body => text}) self.body << text_part end |
#date(val = nil) ⇒ Object
514 515 516 |
# File 'lib/mail/message.rb', line 514 def date( val = nil ) default :date, val end |
#date=(val) ⇒ Object
518 519 520 |
# File 'lib/mail/message.rb', line 518 def date=( val ) header[:date] = val end |
#decode_body ⇒ Object
1630 1631 1632 1633 1634 1635 1636 |
# File 'lib/mail/message.rb', line 1630 def decode_body if Mail::Encodings.defined?(content_transfer_encoding) Mail::Encodings.get_encoding(content_transfer_encoding).decode(body.encoded) else raise UnknownEncodingType, "Don't know how to decode #{content_transfer_encoding}, please call #encoded and decode it yourself." end end |
#decoded ⇒ Object
1614 1615 1616 1617 1618 1619 1620 |
# File 'lib/mail/message.rb', line 1614 def decoded if self. decode_body else raise NoMethodError, 'Can not decode an entire message, try calling #decoded on the various fields and body or parts if it is a multipart message.' end end |
#default(sym, val = nil) ⇒ Object
Returns the default value of the field requested as a symbol.
Each header field has a :default method which returns the most common use case for that field, for example, the date field types will return a DateTime object when sent :default, the subject, or unstructured fields will return a decoded string of their value, the address field types will return a single addr_spec or an array of addr_specs if there is more than one.
1017 1018 1019 1020 1021 1022 1023 |
# File 'lib/mail/message.rb', line 1017 def default( sym, val = nil ) if val header[sym] = val else header[sym].default if header[sym] end end |
#deliver ⇒ Object
214 215 216 217 218 219 220 221 222 |
# File 'lib/mail/message.rb', line 214 def deliver if delivery_handler delivery_handler.deliver_mail(self) { do_delivery } else do_delivery inform_observers end self end |
#deliver! ⇒ Object
This method bypasses checking perform_deliveries and raise_delivery_errors, so use with caution.
It still however fires callbacks to the observers if they are defined.
Returns self
230 231 232 233 234 |
# File 'lib/mail/message.rb', line 230 def deliver! delivery_method.deliver!(self) inform_observers self end |
#delivery_method(method = nil, settings = {}) ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/mail/message.rb', line 236 def delivery_method(method = nil, settings = {}) unless method @delivery_method else @delivery_method = Mail::Configuration.instance.lookup_delivery_method(method).new(settings) end end |
#delivery_status_part ⇒ Object
returns the part in a multipart/report email that has the content-type delivery-status
1377 1378 1379 |
# File 'lib/mail/message.rb', line 1377 def delivery_status_part @delivery_stats_part ||= parts.select { |p| p.delivery_status_report_part? }.first end |
#delivery_status_report? ⇒ Boolean
Returns true if the message is a multipart/report; report-type=delivery-status;
1372 1373 1374 |
# File 'lib/mail/message.rb', line 1372 def delivery_status_report? multipart_report? && content_type_parameters['report-type'] =~ /^delivery-status$/i end |
#destinations ⇒ Object
Returns the list of addresses this message should be sent to by collecting the addresses off the to, cc and bcc fields.
Example:
mail.to = '[email protected]'
mail.cc = '[email protected]'
mail.bcc = '[email protected]'
mail.destinations.length #=> 3
mail.destinations.first #=> '[email protected]'
1091 1092 1093 |
# File 'lib/mail/message.rb', line 1091 def destinations [to_addrs, cc_addrs, bcc_addrs].compact.flatten end |
#diagnostic_code ⇒ Object
1397 1398 1399 |
# File 'lib/mail/message.rb', line 1397 def diagnostic_code delivery_status_part and delivery_status_part.diagnostic_code end |
#encode! ⇒ Object
1594 1595 1596 1597 |
# File 'lib/mail/message.rb', line 1594 def encode! STDERR.puts("Deprecated in 1.1.0 in favour of :ready_to_send! as it is less confusing with encoding and decoding.") ready_to_send! end |
#encoded ⇒ Object
Outputs an encoded string representation of the mail message including all headers, attachments, etc. This is an encoded email in US-ASCII, so it is able to be directly sent to an email server.
1602 1603 1604 1605 1606 1607 1608 |
# File 'lib/mail/message.rb', line 1602 def encoded ready_to_send! buffer = header.encoded buffer << "\r\n" buffer << body.encoded buffer end |
#envelope_date ⇒ Object
340 341 342 |
# File 'lib/mail/message.rb', line 340 def envelope_date @envelope ? @envelope.date : nil end |
#envelope_from ⇒ Object
336 337 338 |
# File 'lib/mail/message.rb', line 336 def envelope_from @envelope ? @envelope.from : nil end |
#error_status ⇒ Object
1393 1394 1395 |
# File 'lib/mail/message.rb', line 1393 def error_status delivery_status_part and delivery_status_part.error_status end |
#filename ⇒ Object
Returns the filename of the attachment
1649 1650 1651 |
# File 'lib/mail/message.rb', line 1649 def filename end |
#final_recipient ⇒ Object
1389 1390 1391 |
# File 'lib/mail/message.rb', line 1389 def final_recipient delivery_status_part and delivery_status_part.final_recipient end |
#find_first_mime_type(mt) ⇒ Object
1657 1658 1659 |
# File 'lib/mail/message.rb', line 1657 def find_first_mime_type(mt) all_parts.detect { |p| p.mime_type == mt } end |
#from(val = nil) ⇒ Object
Returns the From value of the mail object as an array of strings of address specs.
Example:
mail.from = 'Mikel <[email protected]>'
mail.from #=> ['[email protected]']
mail.from = 'Mikel <[email protected]>, [email protected]'
mail.from #=> ['[email protected]', '[email protected]']
Also allows you to set the value by passing a value as a parameter
Example:
mail.from 'Mikel <[email protected]>'
mail.from #=> ['[email protected]']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.from 'Mikel <[email protected]>'
mail.from << '[email protected]'
mail.from #=> ['[email protected]', '[email protected]']
547 548 549 |
# File 'lib/mail/message.rb', line 547 def from( val = nil ) default :from, val end |
#from=(val) ⇒ Object
Sets the From value of the mail object, pass in a string of the field
Example:
mail.from = 'Mikel <[email protected]>'
mail.from #=> ['[email protected]']
mail.from = 'Mikel <[email protected]>, [email protected]'
mail.from #=> ['[email protected]', '[email protected]']
559 560 561 |
# File 'lib/mail/message.rb', line 559 def from=( val ) header[:from] = val end |
#from_addrs ⇒ Object
Returns an array of addresses (the encoded value) in the From field, if no From field, returns an empty array
1097 1098 1099 |
# File 'lib/mail/message.rb', line 1097 def from_addrs from ? [from].flatten : [] end |
#has_attachments? ⇒ Boolean
1457 1458 1459 |
# File 'lib/mail/message.rb', line 1457 def !.empty? end |
#has_charset? ⇒ Boolean
1229 1230 1231 |
# File 'lib/mail/message.rb', line 1229 def has_charset? !!charset end |
#has_content_transfer_encoding? ⇒ Boolean
1233 1234 1235 |
# File 'lib/mail/message.rb', line 1233 def has_content_transfer_encoding? !!content_transfer_encoding end |
#has_content_type? ⇒ Boolean
1225 1226 1227 |
# File 'lib/mail/message.rb', line 1225 def has_content_type? !!header[:content_type] end |
#has_date? ⇒ Boolean
Returns true if the message has a Date field, the field may or may not have a value, but the field exists or not.
1215 1216 1217 |
# File 'lib/mail/message.rb', line 1215 def has_date? header.has_date? end |
#has_message_id? ⇒ Boolean
Returns true if the message has a message ID field, the field may or may not have a value, but the field exists or not.
1209 1210 1211 |
# File 'lib/mail/message.rb', line 1209 def header. end |
#has_mime_version? ⇒ Boolean
Returns true if the message has a Date field, the field may or may not have a value, but the field exists or not.
1221 1222 1223 |
# File 'lib/mail/message.rb', line 1221 def has_mime_version? header.has_mime_version? end |
#has_transfer_encoding? ⇒ Boolean
:nodoc:
1237 1238 1239 1240 |
# File 'lib/mail/message.rb', line 1237 def has_transfer_encoding? # :nodoc: STDERR.puts(":has_transfer_encoding? is deprecated in Mail 1.4.3. Please use has_content_transfer_encoding?\n#{caller}") has_content_transfer_encoding? end |
#header(value = nil) ⇒ Object
Returns the header object of the message object. Or, if passed a parameter sets the value.
Example:
mail = Mail::Message.new('To: mikel\r\nFrom: you')
mail.header #=> #<Mail::Header:0x13ce14 @raw_source="To: mikel\r\nFr...
mail.header #=> nil
mail.header 'To: mikel\r\nFrom: you'
mail.header #=> #<Mail::Header:0x13ce14 @raw_source="To: mikel\r\nFr...
365 366 367 |
# File 'lib/mail/message.rb', line 365 def header(value = nil) value ? self.header = value : @header end |
#header=(value) ⇒ Object
Sets the header of the message object.
Example:
mail.header = 'To: [email protected]\r\nFrom: [email protected]'
mail.header #=> <#Mail::Header
350 351 352 |
# File 'lib/mail/message.rb', line 350 def header=(value) @header = Mail::Header.new(value) end |
#header_fields ⇒ Object
Returns an FieldList of all the fields in the header in the order that they appear in the header
1203 1204 1205 |
# File 'lib/mail/message.rb', line 1203 def header_fields header.fields end |
#headers(hash = {}) ⇒ Object
Provides a way to set custom headers, by passing in a hash
370 371 372 373 374 |
# File 'lib/mail/message.rb', line 370 def headers(hash = {}) hash.each_pair do |k,v| header[k] = v end end |
#html_part(&block) ⇒ Object
Accessor for html_part
1462 1463 1464 1465 1466 1467 1468 1469 1470 |
# File 'lib/mail/message.rb', line 1462 def html_part(&block) if block_given? @html_part = Mail::Part.new(&block) add_multipart_alternate_header unless html_part.blank? add_part(@html_part) else @html_part || find_first_mime_type('text/html') end end |
#html_part=(msg = nil) ⇒ Object
Helper to add a html part to a multipart/alternative email. If this and text_part are both defined in a message, then it will be a multipart/alternative message and set itself that way.
1486 1487 1488 1489 1490 1491 1492 1493 1494 |
# File 'lib/mail/message.rb', line 1486 def html_part=(msg = nil) if msg @html_part = msg else @html_part = Mail::Part.new('Content-Type: text/html;') end add_multipart_alternate_header unless text_part.blank? add_part(@html_part) end |
#in_reply_to(val = nil) ⇒ Object
563 564 565 |
# File 'lib/mail/message.rb', line 563 def in_reply_to( val = nil ) default :in_reply_to, val end |
#in_reply_to=(val) ⇒ Object
567 568 569 |
# File 'lib/mail/message.rb', line 567 def in_reply_to=( val ) header[:in_reply_to] = val end |
#inform_observers ⇒ Object
202 203 204 205 206 |
# File 'lib/mail/message.rb', line 202 def inform_observers @delivery_notification_observers.each do |observer| observer.delivered_email(self) end end |
#keywords(val = nil) ⇒ Object
571 572 573 |
# File 'lib/mail/message.rb', line 571 def keywords( val = nil ) default :keywords, val end |
#keywords=(val) ⇒ Object
575 576 577 |
# File 'lib/mail/message.rb', line 575 def keywords=( val ) header[:keywords] = val end |
#main_type ⇒ Object
Returns the main content type
1341 1342 1343 |
# File 'lib/mail/message.rb', line 1341 def main_type has_content_type? ? header[:content_type].main_type : nil end |
#message_content_type ⇒ Object
1320 1321 1322 1323 |
# File 'lib/mail/message.rb', line 1320 def STDERR.puts(":message_content_type is deprecated in Mail 1.4.3. Please use mime_type\n#{caller}") mime_type end |
#message_id(val = nil) ⇒ Object
Returns the Message-ID of the mail object. Note, per RFC 2822 the Message ID consists of what is INSIDE the < > usually seen in the mail header, so this method will return only what is inside.
Example:
mail. = '<[email protected]>'
mail. #=> '[email protected]'
Also allows you to set the Message-ID by passing a string as a parameter
mail. '<[email protected]>'
mail. #=> '[email protected]'
592 593 594 |
# File 'lib/mail/message.rb', line 592 def ( val = nil ) default :message_id, val end |
#message_id=(val) ⇒ Object
Sets the Message-ID. Note, per RFC 2822 the Message ID consists of what is INSIDE the < > usually seen in the mail header, so this method will return only what is inside.
mail. = '<[email protected]>'
mail. #=> '[email protected]'
601 602 603 |
# File 'lib/mail/message.rb', line 601 def ( val ) header[:message_id] = val end |
#mime_parameters ⇒ Object
Returns the content type parameters
1351 1352 1353 1354 |
# File 'lib/mail/message.rb', line 1351 def mime_parameters STDERR.puts(':mime_parameters is deprecated in Mail 1.4.3, please use :content_type_parameters instead') content_type_parameters end |
#mime_type ⇒ Object
Returns the mime type of part we are on, this is taken from the content-type header
1316 1317 1318 |
# File 'lib/mail/message.rb', line 1316 def mime_type content_type ? header[:content_type].string : nil end |
#mime_version(val = nil) ⇒ Object
Returns the mime version of the email as a string
Example:
mail.mime_version = '1.0'
mail.mime_version #=> '1.0'
Also allows you to set the mime version by passing a string as a parameter.
Example:
mail.mime_version '1.0'
mail.mime_version #=> '1.0'
618 619 620 |
# File 'lib/mail/message.rb', line 618 def mime_version( val = nil ) default :mime_version, val end |
#mime_version=(val) ⇒ Object
Sets the mime version of the email by accepting a string
Example:
mail.mime_version = '1.0'
mail.mime_version #=> '1.0'
628 629 630 |
# File 'lib/mail/message.rb', line 628 def mime_version=( val ) header[:mime_version] = val end |
#multipart? ⇒ Boolean
Returns true if the message is multipart
1362 1363 1364 |
# File 'lib/mail/message.rb', line 1362 def multipart? has_content_type? ? !!(main_type =~ /^multipart$/i) : false end |
#multipart_report? ⇒ Boolean
Returns true if the message is a multipart/report
1367 1368 1369 |
# File 'lib/mail/message.rb', line 1367 def multipart_report? multipart? && sub_type =~ /^report$/i end |
#part(params = {}) {|new_part| ... } ⇒ Object
Allows you to add a part in block form to an existing mail message object
Example:
mail = Mail.new do
part :content_type => "multipart/alternative", :content_disposition => "inline" do |p|
p.part :content_type => "text/plain", :body => "test text\nline #2"
p.part :content_type => "text/html", :body => "<b>test</b> HTML<br/>\nline #2"
end
end
1531 1532 1533 1534 1535 |
# File 'lib/mail/message.rb', line 1531 def part(params = {}) new_part = Part.new(params) yield new_part if block_given? add_part(new_part) end |
#parts ⇒ Object
Returns a parts list object of all the parts in the message
1415 1416 1417 |
# File 'lib/mail/message.rb', line 1415 def parts body.parts end |
#raw_envelope ⇒ Object
The raw_envelope is the From [email protected] Mon May 2 16:07:05 2009 type field that you can see at the top of any email that has come from a mailbox
332 333 334 |
# File 'lib/mail/message.rb', line 332 def raw_envelope @raw_envelope end |
#raw_source ⇒ Object
319 320 321 |
# File 'lib/mail/message.rb', line 319 def raw_source @raw_source end |
#read ⇒ Object
1622 1623 1624 1625 1626 1627 1628 |
# File 'lib/mail/message.rb', line 1622 def read if self. decode_body else raise NoMethodError, 'Can not call read on a part unless it is an attachment.' end end |
#ready_to_send! ⇒ Object
Encodes the message, calls encode on all it’s parts, gets an email message ready to send
1589 1590 1591 1592 |
# File 'lib/mail/message.rb', line 1589 def ready_to_send! parts.each { |part| part.ready_to_send! } add_required_fields end |
#received(val = nil) ⇒ Object
632 633 634 635 636 637 638 |
# File 'lib/mail/message.rb', line 632 def received( val = nil ) if val header[:received] = val else header[:received] end end |
#received=(val) ⇒ Object
640 641 642 |
# File 'lib/mail/message.rb', line 640 def received=( val ) header[:received] = val end |
#references(val = nil) ⇒ Object
644 645 646 |
# File 'lib/mail/message.rb', line 644 def references( val = nil ) default :references, val end |
#references=(val) ⇒ Object
648 649 650 |
# File 'lib/mail/message.rb', line 648 def references=( val ) header[:references] = val end |
#register_for_delivery_notification(observer) ⇒ Object
196 197 198 199 200 |
# File 'lib/mail/message.rb', line 196 def register_for_delivery_notification(observer) unless @delivery_notification_observers.include?(observer) @delivery_notification_observers << observer end end |
#remote_mta ⇒ Object
1401 1402 1403 |
# File 'lib/mail/message.rb', line 1401 def remote_mta delivery_status_part and delivery_status_part.remote_mta end |
#reply_to(val = nil) ⇒ Object
Returns the Reply-To value of the mail object as an array of strings of address specs.
Example:
mail.reply_to = 'Mikel <[email protected]>'
mail.reply_to #=> ['[email protected]']
mail.reply_to = 'Mikel <[email protected]>, [email protected]'
mail.reply_to #=> ['[email protected]', '[email protected]']
Also allows you to set the value by passing a value as a parameter
Example:
mail.reply_to 'Mikel <[email protected]>'
mail.reply_to #=> ['[email protected]']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.reply_to 'Mikel <[email protected]>'
mail.reply_to << '[email protected]'
mail.reply_to #=> ['[email protected]', '[email protected]']
677 678 679 |
# File 'lib/mail/message.rb', line 677 def reply_to( val = nil ) default :reply_to, val end |
#reply_to=(val) ⇒ Object
Sets the Reply-To value of the mail object, pass in a string of the field
Example:
mail.reply_to = 'Mikel <[email protected]>'
mail.reply_to #=> ['[email protected]']
mail.reply_to = 'Mikel <[email protected]>, [email protected]'
mail.reply_to #=> ['[email protected]', '[email protected]']
689 690 691 |
# File 'lib/mail/message.rb', line 689 def reply_to=( val ) header[:reply_to] = val end |
#resent_bcc(val = nil) ⇒ Object
Returns the Resent-Bcc value of the mail object as an array of strings of address specs.
Example:
mail.resent_bcc = 'Mikel <[email protected]>'
mail.resent_bcc #=> ['[email protected]']
mail.resent_bcc = 'Mikel <[email protected]>, [email protected]'
mail.resent_bcc #=> ['[email protected]', '[email protected]']
Also allows you to set the value by passing a value as a parameter
Example:
mail.resent_bcc 'Mikel <[email protected]>'
mail.resent_bcc #=> ['[email protected]']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.resent_bcc 'Mikel <[email protected]>'
mail.resent_bcc << '[email protected]'
mail.resent_bcc #=> ['[email protected]', '[email protected]']
718 719 720 |
# File 'lib/mail/message.rb', line 718 def resent_bcc( val = nil ) default :resent_bcc, val end |
#resent_bcc=(val) ⇒ Object
Sets the Resent-Bcc value of the mail object, pass in a string of the field
Example:
mail.resent_bcc = 'Mikel <[email protected]>'
mail.resent_bcc #=> ['[email protected]']
mail.resent_bcc = 'Mikel <[email protected]>, [email protected]'
mail.resent_bcc #=> ['[email protected]', '[email protected]']
730 731 732 |
# File 'lib/mail/message.rb', line 730 def resent_bcc=( val ) header[:resent_bcc] = val end |
#resent_cc(val = nil) ⇒ Object
Returns the Resent-Cc value of the mail object as an array of strings of address specs.
Example:
mail.resent_cc = 'Mikel <[email protected]>'
mail.resent_cc #=> ['[email protected]']
mail.resent_cc = 'Mikel <[email protected]>, [email protected]'
mail.resent_cc #=> ['[email protected]', '[email protected]']
Also allows you to set the value by passing a value as a parameter
Example:
mail.resent_cc 'Mikel <[email protected]>'
mail.resent_cc #=> ['[email protected]']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.resent_cc 'Mikel <[email protected]>'
mail.resent_cc << '[email protected]'
mail.resent_cc #=> ['[email protected]', '[email protected]']
759 760 761 |
# File 'lib/mail/message.rb', line 759 def resent_cc( val = nil ) default :resent_cc, val end |
#resent_cc=(val) ⇒ Object
Sets the Resent-Cc value of the mail object, pass in a string of the field
Example:
mail.resent_cc = 'Mikel <[email protected]>'
mail.resent_cc #=> ['[email protected]']
mail.resent_cc = 'Mikel <[email protected]>, [email protected]'
mail.resent_cc #=> ['[email protected]', '[email protected]']
771 772 773 |
# File 'lib/mail/message.rb', line 771 def resent_cc=( val ) header[:resent_cc] = val end |
#resent_date(val = nil) ⇒ Object
775 776 777 |
# File 'lib/mail/message.rb', line 775 def resent_date( val = nil ) default :resent_date, val end |
#resent_date=(val) ⇒ Object
779 780 781 |
# File 'lib/mail/message.rb', line 779 def resent_date=( val ) header[:resent_date] = val end |
#resent_from(val = nil) ⇒ Object
Returns the Resent-From value of the mail object as an array of strings of address specs.
Example:
mail.resent_from = 'Mikel <[email protected]>'
mail.resent_from #=> ['[email protected]']
mail.resent_from = 'Mikel <[email protected]>, [email protected]'
mail.resent_from #=> ['[email protected]', '[email protected]']
Also allows you to set the value by passing a value as a parameter
Example:
mail.resent_from ['Mikel <[email protected]>']
mail.resent_from #=> '[email protected]'
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.resent_from 'Mikel <[email protected]>'
mail.resent_from << '[email protected]'
mail.resent_from #=> ['[email protected]', '[email protected]']
808 809 810 |
# File 'lib/mail/message.rb', line 808 def resent_from( val = nil ) default :resent_from, val end |
#resent_from=(val) ⇒ Object
Sets the Resent-From value of the mail object, pass in a string of the field
Example:
mail.resent_from = 'Mikel <[email protected]>'
mail.resent_from #=> ['[email protected]']
mail.resent_from = 'Mikel <[email protected]>, [email protected]'
mail.resent_from #=> ['[email protected]', '[email protected]']
820 821 822 |
# File 'lib/mail/message.rb', line 820 def resent_from=( val ) header[:resent_from] = val end |
#resent_message_id(val = nil) ⇒ Object
824 825 826 |
# File 'lib/mail/message.rb', line 824 def ( val = nil ) default :resent_message_id, val end |
#resent_message_id=(val) ⇒ Object
828 829 830 |
# File 'lib/mail/message.rb', line 828 def ( val ) header[:resent_message_id] = val end |
#resent_sender(val = nil) ⇒ Object
Returns the Resent-Sender value of the mail object, as a single string of an address spec. A sender per RFC 2822 must be a single address, so you can not append to this address.
Example:
mail.resent_sender = 'Mikel <[email protected]>'
mail.resent_sender #=> '[email protected]'
Also allows you to set the value by passing a value as a parameter
Example:
mail.resent_sender 'Mikel <[email protected]>'
mail.resent_sender #=> '[email protected]'
847 848 849 |
# File 'lib/mail/message.rb', line 847 def resent_sender( val = nil ) default :resent_sender, val end |
#resent_sender=(val) ⇒ Object
Sets the Resent-Sender value of the mail object, pass in a string of the field
Example:
mail.sender = 'Mikel <[email protected]>'
mail.sender #=> '[email protected]'
857 858 859 |
# File 'lib/mail/message.rb', line 857 def resent_sender=( val ) header[:resent_sender] = val end |
#resent_to(val = nil) ⇒ Object
Returns the Resent-To value of the mail object as an array of strings of address specs.
Example:
mail.resent_to = 'Mikel <[email protected]>'
mail.resent_to #=> ['[email protected]']
mail.resent_to = 'Mikel <[email protected]>, [email protected]'
mail.resent_to #=> ['[email protected]', '[email protected]']
Also allows you to set the value by passing a value as a parameter
Example:
mail.resent_to 'Mikel <[email protected]>'
mail.resent_to #=> ['[email protected]']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.resent_to 'Mikel <[email protected]>'
mail.resent_to << '[email protected]'
mail.resent_to #=> ['[email protected]', '[email protected]']
886 887 888 |
# File 'lib/mail/message.rb', line 886 def resent_to( val = nil ) default :resent_to, val end |
#resent_to=(val) ⇒ Object
Sets the Resent-To value of the mail object, pass in a string of the field
Example:
mail.resent_to = 'Mikel <[email protected]>'
mail.resent_to #=> ['[email protected]']
mail.resent_to = 'Mikel <[email protected]>, [email protected]'
mail.resent_to #=> ['[email protected]', '[email protected]']
898 899 900 |
# File 'lib/mail/message.rb', line 898 def resent_to=( val ) header[:resent_to] = val end |
#retryable? ⇒ Boolean
1405 1406 1407 |
# File 'lib/mail/message.rb', line 1405 def retryable? delivery_status_part and delivery_status_part.retryable? end |
#return_path(val = nil) ⇒ Object
Returns the return path of the mail object, or sets it if you pass a string
903 904 905 |
# File 'lib/mail/message.rb', line 903 def return_path( val = nil ) default :return_path, val end |
#return_path=(val) ⇒ Object
Sets the return path of the object
908 909 910 |
# File 'lib/mail/message.rb', line 908 def return_path=( val ) header[:return_path] = val end |
#sender(val = nil) ⇒ Object
Returns the Sender value of the mail object, as a single string of an address spec. A sender per RFC 2822 must be a single address.
Example:
mail.sender = 'Mikel <[email protected]>'
mail.sender #=> '[email protected]'
Also allows you to set the value by passing a value as a parameter
Example:
mail.sender 'Mikel <[email protected]>'
mail.sender #=> '[email protected]'
926 927 928 |
# File 'lib/mail/message.rb', line 926 def sender( val = nil ) default :sender, val end |
#sender=(val) ⇒ Object
Sets the Sender value of the mail object, pass in a string of the field
Example:
mail.sender = 'Mikel <[email protected]>'
mail.sender #=> '[email protected]'
936 937 938 |
# File 'lib/mail/message.rb', line 936 def sender=( val ) header[:sender] = val end |
#set_envelope(val) ⇒ Object
Sets the envelope from for the email
324 325 326 327 |
# File 'lib/mail/message.rb', line 324 def set_envelope( val ) @raw_envelope = val @envelope = Mail::Envelope.new( val ) end |
#sub_type ⇒ Object
Returns the sub content type
1346 1347 1348 |
# File 'lib/mail/message.rb', line 1346 def sub_type has_content_type? ? header[:content_type].sub_type : nil end |
#subject(val = nil) ⇒ Object
Returns the decoded value of the subject field, as a single string.
Example:
mail.subject = "G'Day mate"
mail.subject #=> "G'Day mate"
mail.subject = '=?UTF-8?Q?This_is_=E3=81=82_string?='
mail.subject #=> "This is あ string"
Also allows you to set the value by passing a value as a parameter
Example:
mail.subject "G'Day mate"
mail.subject #=> "G'Day mate"
955 956 957 |
# File 'lib/mail/message.rb', line 955 def subject( val = nil ) default :subject, val end |
#subject=(val) ⇒ Object
Sets the Subject value of the mail object, pass in a string of the field
Example:
mail.subject = '=?UTF-8?Q?This_is_=E3=81=82_string?='
mail.subject #=> "This is あ string"
965 966 967 |
# File 'lib/mail/message.rb', line 965 def subject=( val ) header[:subject] = val end |
#text_part(&block) ⇒ Object
Accessor for text_part
1473 1474 1475 1476 1477 1478 1479 1480 1481 |
# File 'lib/mail/message.rb', line 1473 def text_part(&block) if block_given? @text_part = Mail::Part.new(&block) add_multipart_alternate_header unless html_part.blank? add_part(@text_part) else @text_part || find_first_mime_type('text/plain') end end |
#text_part=(msg = nil) ⇒ Object
Helper to add a text part to a multipart/alternative email. If this and html_part are both defined in a message, then it will be a multipart/alternative message and set itself that way.
1499 1500 1501 1502 1503 1504 1505 1506 1507 |
# File 'lib/mail/message.rb', line 1499 def text_part=(msg = nil) if msg @text_part = msg else @text_part = Mail::Part.new('Content-Type: text/plain;') end add_multipart_alternate_header unless html_part.blank? add_part(@text_part) end |
#to(val = nil) ⇒ Object
Returns the To value of the mail object as an array of strings of address specs.
Example:
mail.to = 'Mikel <[email protected]>'
mail.to #=> ['[email protected]']
mail.to = 'Mikel <[email protected]>, [email protected]'
mail.to #=> ['[email protected]', '[email protected]']
Also allows you to set the value by passing a value as a parameter
Example:
mail.to 'Mikel <[email protected]>'
mail.to #=> ['[email protected]']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.to 'Mikel <[email protected]>'
mail.to << '[email protected]'
mail.to #=> ['[email protected]', '[email protected]']
994 995 996 |
# File 'lib/mail/message.rb', line 994 def to( val = nil ) default :to, val end |
#to=(val) ⇒ Object
Sets the To value of the mail object, pass in a string of the field
Example:
mail.to = 'Mikel <[email protected]>'
mail.to #=> ['[email protected]']
mail.to = 'Mikel <[email protected]>, [email protected]'
mail.to #=> ['[email protected]', '[email protected]']
1006 1007 1008 |
# File 'lib/mail/message.rb', line 1006 def to=( val ) header[:to] = val end |
#to_addrs ⇒ Object
Returns an array of addresses (the encoded value) in the To field, if no To field, returns an empty array
1103 1104 1105 |
# File 'lib/mail/message.rb', line 1103 def to_addrs to ? [to].flatten : [] end |
#to_s ⇒ Object
1610 1611 1612 |
# File 'lib/mail/message.rb', line 1610 def to_s encoded end |
#transfer_encoding ⇒ Object
:nodoc:
1310 1311 1312 1313 |
# File 'lib/mail/message.rb', line 1310 def transfer_encoding # :nodoc: STDERR.puts(":transfer_encoding is deprecated in Mail 1.4.3. Please use content_transfer_encoding\n#{caller}") content_transfer_encoding end |