Returns the main content type
# File lib/mail/message.rb, line 1439 1439: def main_type 1440: has_content_type? ? header[:content_type].main_type : nil rescue nil 1441: end
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 contains 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.
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).
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: mikel@test.lindsaar.net') @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.
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
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
# File lib/mail/message.rb, line 1771 1771: def self.from_hash(hash) 1772: Mail::Message.new(hash) 1773: end
# File lib/mail/message.rb, line 1749 1749: def self.from_yaml(str) 1750: hash = YAML.load(str) 1751: m = self.new(:headers => hash['headers']) 1752: hash.delete('headers') 1753: hash.each do |k,v| 1754: case 1755: when k == 'delivery_handler' 1756: begin 1757: m.delivery_handler = Object.const_get(v) unless v.blank? 1758: rescue NameError 1759: end 1760: when k == 'transport_encoding' 1761: m.transport_encoding(v) 1762: when k == 'multipart_body' 1763: v.map {|part| m.add_part Mail::Part.from_yaml(part) } 1764: when k =~ /^@/ 1765: m.instance_variable_set(k.to_sym, v) 1766: end 1767: end 1768: m 1769: end
You can make an new mail object via a block, passing a string, file or direct assignment.
mail = Mail.new do from 'mikel@test.lindsaar.net' to 'you@test.lindsaar.net' subject 'This is a test email' body File.read('body.txt') end mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
mail = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Hello\r\n\r\nHi there!") mail.body.to_s #=> 'Hi there!' mail.subject #=> 'Hello' mail.to #=> 'mikel@test.lindsaar.net'
mail = Mail.read('path/to/file.eml') mail.body.to_s #=> 'Hi there!' mail.subject #=> 'Hello' mail.to #=> 'mikel@test.lindsaar.net'
You can assign values to a mail object via four approaches:
Message#field_name=(value)
Message#field_name(value)
Message#[‘field_name’]=(value)
Message#[:field_name]=(value)
Examples:
mail = Mail.new mail['from'] = 'mikel@test.lindsaar.net' mail[:to] = 'you@test.lindsaar.net' mail.subject 'This is a test email' mail.body = 'This is a body' mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
# File lib/mail/message.rb, line 100 100: def initialize(*args, &block) 101: @body = nil 102: @body_raw = nil 103: @separate_parts = false 104: @text_part = nil 105: @html_part = nil 106: @errors = nil 107: @header = nil 108: @charset = 'UTF-8' 109: @defaulted_charset = true 110: 111: @perform_deliveries = true 112: @raise_delivery_errors = true 113: 114: @delivery_handler = nil 115: 116: @delivery_method = Mail.delivery_method.dup 117: 118: @transport_encoding = Mail::Encodings.get_encoding('7bit') 119: 120: @mark_for_delete = false 121: 122: if args.flatten.first.respond_to?(:each_pair) 123: init_with_hash(args.flatten.first) 124: else 125: init_with_string(args.flatten[0].to_s.strip) 126: end 127: 128: if block_given? 129: instance_eval(&block) 130: end 131: 132: self 133: end
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]
# File lib/mail/message.rb, line 310 310: def <=>(other) 311: if other.nil? 312: 1 313: else 314: self.date <=> other.date 315: end 316: end
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
# File lib/mail/message.rb, line 349 349: def ==(other) 350: return false unless other.respond_to?(:encoded) 351: 352: if self.message_id && other.message_id 353: result = (self.encoded == other.encoded) 354: else 355: self_message_id, other_message_id = self.message_id, other.message_id 356: self.message_id, other.message_id = '<temp@test>', '<temp@test>' 357: result = self.encoded == other.encoded 358: self.message_id = "<#{self_message_id}>" if self_message_id 359: other.message_id = "<#{other_message_id}>" if other_message_id 360: result 361: end 362: end
Allows you to read an arbitrary header
Example:
mail['foo'] = '1234' mail['foo'].to_s #=> '1234'
# File lib/mail/message.rb, line 1234 1234: def [](name) 1235: header[underscoreize(name)] 1236: end
Allows you to add an arbitrary header
Example:
mail['foo'] = '1234' mail['foo'].to_s #=> '1234'
# File lib/mail/message.rb, line 1216 1216: def []=(name, value) 1217: if name.to_s == 'body' 1218: self.body = value 1219: elsif name.to_s =~ /content[-_]type/ 1220: header[name] = value 1221: elsif name.to_s == 'charset' 1222: self.charset = value 1223: else 1224: header[name] = value 1225: end 1226: end
# File lib/mail/message.rb, line 1483 1483: def action 1484: delivery_status_part and delivery_status_part.action 1485: end
Adds a content type and charset if the body is US-ASCII
Otherwise raises a warning
# File lib/mail/message.rb, line 1377 1377: def add_charset 1378: if !body.empty? 1379: # Only give a warning if this isn't an attachment, has non US-ASCII and the user 1380: # has not specified an encoding explicitly. 1381: if @defaulted_charset && body.raw_source.not_ascii_only? && !self.attachment? 1382: warning = "Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect.\n" 1383: STDERR.puts(warning) 1384: end 1385: header[:content_type].parameters['charset'] = @charset 1386: end 1387: end
Adds a content transfer encoding
Otherwise raises a warning
# File lib/mail/message.rb, line 1392 1392: def add_content_transfer_encoding 1393: if body.only_us_ascii? 1394: header[:content_transfer_encoding] = '7bit' 1395: else 1396: warning = "Non US-ASCII detected and no content-transfer-encoding defined.\nDefaulting to 8bit, set your own if this is incorrect.\n" 1397: STDERR.puts(warning) 1398: header[:content_transfer_encoding] = '8bit' 1399: end 1400: end
Adds a content type and charset if the body is US-ASCII
Otherwise raises a warning
# File lib/mail/message.rb, line 1370 1370: def add_content_type 1371: header[:content_type] = 'text/plain' 1372: end
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.
# File lib/mail/message.rb, line 1353 1353: def add_date(date_val = '') 1354: header['date'] = date_val 1355: end
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 media 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 #
# File lib/mail/message.rb, line 1664 1664: def add_file(values) 1665: convert_to_multipart unless self.multipart? || self.body.decoded.blank? 1666: add_multipart_mixed_header 1667: if values.is_a?(String) 1668: basename = File.basename(values) 1669: filedata = File.open(values, 'rb') { |f| f.read } 1670: else 1671: basename = values[:filename] 1672: filedata = values[:content] || File.open(values[:filename], 'rb') { |f| f.read } 1673: end 1674: self.attachments[basename] = filedata 1675: end
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.
# File lib/mail/message.rb, line 1343 1343: def add_message_id(msg_id_val = '') 1344: header['message-id'] = msg_id_val 1345: end
Creates a new empty Mime Version field and inserts it in the correct order into the Header. The MimeVersion object will automatically generate set itself to ‘1.0’ if you try and encode it or output it to_s without specifying a version yourself.
It will preserve any date you specify if you do.
# File lib/mail/message.rb, line 1363 1363: def add_mime_version(ver_val = '') 1364: header['mime-version'] = ver_val 1365: end
Adds a part to the parts list or creates the part list
# File lib/mail/message.rb, line 1608 1608: def add_part(part) 1609: if !body.multipart? && !self.body.decoded.blank? 1610: @text_part = Mail::Part.new('Content-Type: text/plain;') 1611: @text_part.body = body.decoded 1612: self.body << @text_part 1613: add_multipart_alternate_header 1614: end 1615: add_boundary 1616: self.body << part 1617: end
# File lib/mail/message.rb, line 1824 1824: def all_parts 1825: parts.map { |p| [p, p.all_parts] }.flatten 1826: end
Returns the attachment data if there is any
# File lib/mail/message.rb, line 1815 1815: def attachment 1816: @attachment 1817: end
Returns true if this part is an attachment, false otherwise.
# File lib/mail/message.rb, line 1810 1810: def attachment? 1811: !!find_attachment 1812: end
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.attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
If you do this, then Mail will take the file name and work out the MIME media 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.attachments['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.attachments['filename.jpg'] = {:mime_type => 'application/x-gzip', :encoding => 'SpecialEncoding', :content => file_content }
You can also search for specific attachments:
# By Filename mail.attachments['filename.jpg'] #=> Mail::Part object or nil # or by index mail.attachments[0] #=> Mail::Part (first attachment)
# File lib/mail/message.rb, line 1551 1551: def attachments 1552: parts.attachments 1553: end
Returns the Bcc value of the mail object as an array of strings of address specs.
Example:
mail.bcc = 'Mikel <mikel@test.lindsaar.net>' mail.bcc #=> ['mikel@test.lindsaar.net'] mail.bcc = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.bcc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
Also allows you to set the value by passing a value as a parameter
Example:
mail.bcc 'Mikel <mikel@test.lindsaar.net>' mail.bcc #=> ['mikel@test.lindsaar.net']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.bcc 'Mikel <mikel@test.lindsaar.net>' mail.bcc << 'ada@test.lindsaar.net' mail.bcc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 475 475: def bcc( val = nil ) 476: default :bcc, val 477: end
Sets the Bcc value of the mail object, pass in a string of the field
Example:
mail.bcc = 'Mikel <mikel@test.lindsaar.net>' mail.bcc #=> ['mikel@test.lindsaar.net'] mail.bcc = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.bcc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 487 487: def bcc=( val ) 488: header[:bcc] = val 489: end
Returns an array of addresses (the encoded value) in the Bcc field, if no Bcc field, returns an empty array
# File lib/mail/message.rb, line 1206 1206: def bcc_addrs 1207: bcc ? [bcc].flatten : [] 1208: end
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...
# File lib/mail/message.rb, line 1150 1150: def body(value = nil) 1151: if value 1152: self.body = value 1153: # add_encoding_to_body 1154: else 1155: process_body_raw if @body_raw 1156: @body 1157: end 1158: end
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'
# File lib/mail/message.rb, line 1136 1136: def body=(value) 1137: body_lazy(value) 1138: end
# File lib/mail/message.rb, line 1160 1160: def body_encoding(value) 1161: if value.nil? 1162: body.encoding 1163: else 1164: body.encoding = value 1165: end 1166: end
# File lib/mail/message.rb, line 1168 1168: def body_encoding=(value) 1169: body.encoding = value 1170: end
# File lib/mail/message.rb, line 1479 1479: def bounced? 1480: delivery_status_part and delivery_status_part.bounced? 1481: end
Returns the current boundary for this message part
# File lib/mail/message.rb, line 1508 1508: def boundary 1509: content_type_parameters ? content_type_parameters['boundary'] : nil 1510: end
Returns the Cc value of the mail object as an array of strings of address specs.
Example:
mail.cc = 'Mikel <mikel@test.lindsaar.net>' mail.cc #=> ['mikel@test.lindsaar.net'] mail.cc = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.cc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
Also allows you to set the value by passing a value as a parameter
Example:
mail.cc 'Mikel <mikel@test.lindsaar.net>' mail.cc #=> ['mikel@test.lindsaar.net']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.cc 'Mikel <mikel@test.lindsaar.net>' mail.cc << 'ada@test.lindsaar.net' mail.cc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 516 516: def cc( val = nil ) 517: default :cc, val 518: end
Sets the Cc value of the mail object, pass in a string of the field
Example:
mail.cc = 'Mikel <mikel@test.lindsaar.net>' mail.cc #=> ['mikel@test.lindsaar.net'] mail.cc = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.cc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 528 528: def cc=( val ) 529: header[:cc] = val 530: end
Returns an array of addresses (the encoded value) in the Cc field, if no Cc field, returns an empty array
# File lib/mail/message.rb, line 1200 1200: def cc_addrs 1201: cc ? [cc].flatten : [] 1202: end
Returns the character set defined in the content type field
# File lib/mail/message.rb, line 1423 1423: def charset 1424: if @header 1425: content_type ? content_type_parameters['charset'] : @charset 1426: else 1427: @charset 1428: end 1429: end
Sets the charset to the supplied value.
# File lib/mail/message.rb, line 1432 1432: def charset=(value) 1433: @defaulted_charset = false 1434: @charset = value 1435: @header.charset = value 1436: end
# File lib/mail/message.rb, line 536 536: def comments=( val ) 537: header[:comments] = val 538: end
# File lib/mail/message.rb, line 540 540: def content_description( val = nil ) 541: default :content_description, val 542: end
# File lib/mail/message.rb, line 544 544: def content_description=( val ) 545: header[:content_description] = val 546: end
# File lib/mail/message.rb, line 548 548: def content_disposition( val = nil ) 549: default :content_disposition, val 550: end
# File lib/mail/message.rb, line 552 552: def content_disposition=( val ) 553: header[:content_disposition] = val 554: end
# File lib/mail/message.rb, line 556 556: def content_id( val = nil ) 557: default :content_id, val 558: end
# File lib/mail/message.rb, line 560 560: def content_id=( val ) 561: header[:content_id] = val 562: end
# File lib/mail/message.rb, line 564 564: def content_location( val = nil ) 565: default :content_location, val 566: end
# File lib/mail/message.rb, line 568 568: def content_location=( val ) 569: header[:content_location] = val 570: end
# File lib/mail/message.rb, line 572 572: def content_transfer_encoding( val = nil ) 573: default :content_transfer_encoding, val 574: end
# File lib/mail/message.rb, line 576 576: def content_transfer_encoding=( val ) 577: header[:content_transfer_encoding] = val 578: end
# File lib/mail/message.rb, line 580 580: def content_type( val = nil ) 581: default :content_type, val 582: end
# File lib/mail/message.rb, line 584 584: def content_type=( val ) 585: header[:content_type] = val 586: end
Returns the content type parameters
# File lib/mail/message.rb, line 1455 1455: def content_type_parameters 1456: has_content_type? ? header[:content_type].parameters : nil rescue nil 1457: end
# File lib/mail/message.rb, line 1677 1677: def convert_to_multipart 1678: text = body.decoded 1679: self.body = '' 1680: text_part = Mail::Part.new({:content_type => 'text/plain;', 1681: :body => text}) 1682: text_part.charset = charset unless @defaulted_charset 1683: self.body << text_part 1684: end
# File lib/mail/message.rb, line 588 588: def date( val = nil ) 589: default :date, val 590: end
# File lib/mail/message.rb, line 592 592: def date=( val ) 593: header[:date] = val 594: end
# File lib/mail/message.rb, line 1804 1804: def decode_body 1805: body.decoded 1806: end
# File lib/mail/message.rb, line 1783 1783: def decoded 1784: case 1785: when self.text? 1786: decode_body_as_text 1787: when self.attachment? 1788: decode_body 1789: when !self.multipart? 1790: body.decoded 1791: else 1792: 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.' 1793: end 1794: end
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.
# File lib/mail/message.rb, line 1103 1103: def default( sym, val = nil ) 1104: if val 1105: header[sym] = val 1106: else 1107: header[sym].default if header[sym] 1108: end 1109: end
Delivers an mail object.
Examples:
mail = Mail.read('file.eml') mail.deliver
# File lib/mail/message.rb, line 226 226: def deliver 227: inform_interceptors 228: if delivery_handler 229: delivery_handler.deliver_mail(self) { do_delivery } 230: else 231: do_delivery 232: end 233: inform_observers 234: self 235: end
This method bypasses checking perform_deliveries and raise_delivery_errors, so use with caution.
It still however fires off the intercepters and calls the observers callbacks if they are defined.
Returns self
# File lib/mail/message.rb, line 243 243: def deliver! 244: inform_interceptors 245: response = delivery_method.deliver!(self) 246: inform_observers 247: delivery_method.settings[:return_response] ? response : self 248: end
# File lib/mail/message.rb, line 250 250: def delivery_method(method = nil, settings = {}) 251: unless method 252: @delivery_method 253: else 254: @delivery_method = Configuration.instance.lookup_delivery_method(method).new(settings) 255: end 256: end
returns the part in a multipart/report email that has the content-type delivery-status
# File lib/mail/message.rb, line 1475 1475: def delivery_status_part 1476: @delivery_stats_part ||= parts.select { |p| p.delivery_status_report_part? }.first 1477: end
Returns true if the message is a multipart/report; report-type=delivery-status;
# File lib/mail/message.rb, line 1470 1470: def delivery_status_report? 1471: multipart_report? && content_type_parameters['report-type'] =~ /^delivery-status$/ 1472: end
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 = 'mikel@test.lindsaar.net' mail.cc = 'sam@test.lindsaar.net' mail.bcc = 'bob@test.lindsaar.net' mail.destinations.length #=> 3 mail.destinations.first #=> 'mikel@test.lindsaar.net'
# File lib/mail/message.rb, line 1182 1182: def destinations 1183: [to_addrs, cc_addrs, bcc_addrs].compact.flatten 1184: end
# File lib/mail/message.rb, line 1495 1495: def diagnostic_code 1496: delivery_status_part and delivery_status_part.diagnostic_code 1497: end
# File lib/mail/message.rb, line 1698 1698: def encode! 1699: STDERR.puts("Deprecated in 1.1.0 in favour of :ready_to_send! as it is less confusing with encoding and decoding.") 1700: ready_to_send! 1701: end
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.
# File lib/mail/message.rb, line 1706 1706: def encoded 1707: ready_to_send! 1708: buffer = header.encoded 1709: buffer << "\r\n" 1710: buffer << body.encoded(content_transfer_encoding) 1711: buffer 1712: end
# File lib/mail/message.rb, line 393 393: def envelope_date 394: @envelope ? @envelope.date : nil 395: end
# File lib/mail/message.rb, line 389 389: def envelope_from 390: @envelope ? @envelope.from : nil 391: end
# File lib/mail/message.rb, line 1491 1491: def error_status 1492: delivery_status_part and delivery_status_part.error_status 1493: end
Returns a list of parser errors on the header, each field that had an error will be reparsed as an unstructured field to preserve the data inside, but will not be used for further processing.
It returns a nested array of [field_name, value, original_error_message] per error found.
Example:
message = Mail.new("Content-Transfer-Encoding: weirdo\r\n") message.errors.size #=> 1 message.errors.first[0] #=> "Content-Transfer-Encoding" message.errors.first[1] #=> "weirdo" message.errors.first[3] #=> <The original error message exception>
This is a good first defence on detecting spam by the way. Some spammers send invalid emails to try and get email parsers to give up parsing them.
# File lib/mail/message.rb, line 446 446: def errors 447: header.errors 448: end
Returns the filename of the attachment
# File lib/mail/message.rb, line 1820 1820: def filename 1821: find_attachment 1822: end
# File lib/mail/message.rb, line 1487 1487: def final_recipient 1488: delivery_status_part and delivery_status_part.final_recipient 1489: end
# File lib/mail/message.rb, line 1828 1828: def find_first_mime_type(mt) 1829: all_parts.detect { |p| p.mime_type == mt && !p.attachment? } 1830: end
Returns the From value of the mail object as an array of strings of address specs.
Example:
mail.from = 'Mikel <mikel@test.lindsaar.net>' mail.from #=> ['mikel@test.lindsaar.net'] mail.from = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.from #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
Also allows you to set the value by passing a value as a parameter
Example:
mail.from 'Mikel <mikel@test.lindsaar.net>' mail.from #=> ['mikel@test.lindsaar.net']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.from 'Mikel <mikel@test.lindsaar.net>' mail.from << 'ada@test.lindsaar.net' mail.from #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 633 633: def from( val = nil ) 634: default :from, val 635: end
Sets the From value of the mail object, pass in a string of the field
Example:
mail.from = 'Mikel <mikel@test.lindsaar.net>' mail.from #=> ['mikel@test.lindsaar.net'] mail.from = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.from #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 645 645: def from=( val ) 646: header[:from] = val 647: end
Returns an array of addresses (the encoded value) in the From field, if no From field, returns an empty array
# File lib/mail/message.rb, line 1188 1188: def from_addrs 1189: from ? [from].flatten : [] 1190: end
# File lib/mail/message.rb, line 1555 1555: def has_attachments? 1556: !attachments.empty? 1557: end
# File lib/mail/message.rb, line 1323 1323: def has_charset? 1324: tmp = header[:content_type].parameters rescue nil 1325: !!(has_content_type? && tmp && tmp['charset']) 1326: end
# File lib/mail/message.rb, line 1328 1328: def has_content_transfer_encoding? 1329: header[:content_transfer_encoding] && header[:content_transfer_encoding].errors.blank? 1330: end
# File lib/mail/message.rb, line 1318 1318: def has_content_type? 1319: tmp = header[:content_type].main_type rescue nil 1320: !!tmp 1321: end
Returns true if the message has a Date field, the field may or may not have a value, but the field exists or not.
# File lib/mail/message.rb, line 1308 1308: def has_date? 1309: header.has_date? 1310: end
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.
# File lib/mail/message.rb, line 1302 1302: def has_message_id? 1303: header.has_message_id? 1304: end
Returns true if the message has a Date field, the field may or may not have a value, but the field exists or not.
# File lib/mail/message.rb, line 1314 1314: def has_mime_version? 1315: header.has_mime_version? 1316: end
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...
# File lib/mail/message.rb, line 418 418: def header(value = nil) 419: value ? self.header = value : @header 420: end
Sets the header of the message object.
Example:
mail.header = 'To: mikel@test.lindsaar.net\r\nFrom: Bob@bob.com' mail.header #=> <#Mail::Header
# File lib/mail/message.rb, line 403 403: def header=(value) 404: @header = Mail::Header.new(value, charset) 405: end
Returns an FieldList of all the fields in the header in the order that they appear in the header
# File lib/mail/message.rb, line 1296 1296: def header_fields 1297: header.fields 1298: end
Provides a way to set custom headers, by passing in a hash
# File lib/mail/message.rb, line 423 423: def headers(hash = {}) 424: hash.each_pair do |k,v| 425: header[k] = v 426: end 427: end
Accessor for html_part
# File lib/mail/message.rb, line 1560 1560: def html_part(&block) 1561: if block_given? 1562: @html_part = Mail::Part.new(&block) 1563: add_multipart_alternate_header unless html_part.blank? 1564: add_part(@html_part) 1565: else 1566: @html_part || find_first_mime_type('text/html') 1567: end 1568: end
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.
# File lib/mail/message.rb, line 1584 1584: def html_part=(msg = nil) 1585: if msg 1586: @html_part = msg 1587: else 1588: @html_part = Mail::Part.new('Content-Type: text/html;') 1589: end 1590: add_multipart_alternate_header unless text_part.blank? 1591: add_part(@html_part) 1592: end
# File lib/mail/message.rb, line 649 649: def in_reply_to( val = nil ) 650: default :in_reply_to, val 651: end
# File lib/mail/message.rb, line 653 653: def in_reply_to=( val ) 654: header[:in_reply_to] = val 655: end
# File lib/mail/message.rb, line 216 216: def inform_interceptors 217: Mail.inform_interceptors(self) 218: end
# File lib/mail/message.rb, line 212 212: def inform_observers 213: Mail.inform_observers(self) 214: end
# File lib/mail/message.rb, line 1779 1779: def inspect 1780: "#<#{self.class}:#{self.object_id}, Multipart: #{multipart?}, Headers: #{header.field_summary}>" 1781: end
Returns whether message will be marked for deletion. If so, the message will be deleted at session close (i.e. after # exits), but only if also using the # method, or by calling # with :delete_after_find set to true.
Side-note: Just to be clear, this method will return true even if the message hasn’t yet been marked for delete on the mail server. However, if this method returns true, it *will be* marked on the server after each block yields back to # or #.
# File lib/mail/message.rb, line 1857 1857: def is_marked_for_delete? 1858: return @mark_for_delete 1859: end
# File lib/mail/message.rb, line 657 657: def keywords( val = nil ) 658: default :keywords, val 659: end
# File lib/mail/message.rb, line 661 661: def keywords=( val ) 662: header[:keywords] = val 663: end
Returns the main content type
# File lib/mail/message.rb, line 1439 1439: def main_type 1440: has_content_type? ? header[:content_type].main_type : nil rescue nil 1441: end
Sets whether this message should be deleted at session close (i.e. after #). Message will only be deleted if messages are retrieved using the # method, or by calling # with :delete_after_find set to true.
# File lib/mail/message.rb, line 1844 1844: def mark_for_delete=(value = true) 1845: @mark_for_delete = value 1846: end
# File lib/mail/message.rb, line 1417 1417: def message_content_type 1418: STDERR.puts(":message_content_type is deprecated in Mail 1.4.3. Please use mime_type\n#{caller}") 1419: mime_type 1420: end
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.message_id = '<1234@message.id>' mail.message_id #=> '1234@message.id'
Also allows you to set the Message-ID by passing a string as a parameter
mail.message_id '<1234@message.id>' mail.message_id #=> '1234@message.id'
# File lib/mail/message.rb, line 678 678: def message_id( val = nil ) 679: default :message_id, val 680: end
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.message_id = '<1234@message.id>' mail.message_id #=> '1234@message.id'
# File lib/mail/message.rb, line 687 687: def message_id=( val ) 688: header[:message_id] = val 689: end
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>'
# File lib/mail/message.rb, line 1277 1277: def method_missing(name, *args, &block) 1278: #:nodoc: 1279: # Only take the structured fields, as we could take _anything_ really 1280: # as it could become an optional field... "but therin lies the dark side" 1281: field_name = underscoreize(name).chomp("=") 1282: if Mail::Field::KNOWN_FIELDS.include?(field_name) 1283: if args.empty? 1284: header[field_name] 1285: else 1286: header[field_name] = args.first 1287: end 1288: else 1289: super # otherwise pass it on 1290: end 1291: #:startdoc: 1292: end
Returns the content type parameters
# File lib/mail/message.rb, line 1449 1449: def mime_parameters 1450: STDERR.puts(':mime_parameters is deprecated in Mail 1.4.3, please use :content_type_parameters instead') 1451: content_type_parameters 1452: end
Returns the MIME media type of part we are on, this is taken from the content-type header
# File lib/mail/message.rb, line 1413 1413: def mime_type 1414: content_type ? header[:content_type].string : nil rescue nil 1415: end
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'
# File lib/mail/message.rb, line 704 704: def mime_version( val = nil ) 705: default :mime_version, val 706: end
Sets the MIME version of the email by accepting a string
Example:
mail.mime_version = '1.0' mail.mime_version #=> '1.0'
# File lib/mail/message.rb, line 714 714: def mime_version=( val ) 715: header[:mime_version] = val 716: end
Returns true if the message is multipart
# File lib/mail/message.rb, line 1460 1460: def multipart? 1461: has_content_type? ? !!(main_type =~ /^multipart$/) : false 1462: end
Returns true if the message is a multipart/report
# File lib/mail/message.rb, line 1465 1465: def multipart_report? 1466: multipart? && sub_type =~ /^report$/ 1467: end
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
# File lib/mail/message.rb, line 1629 1629: def part(params = {}) 1630: new_part = Part.new(params) 1631: yield new_part if block_given? 1632: add_part(new_part) 1633: end
Returns a parts list object of all the parts in the message
# File lib/mail/message.rb, line 1513 1513: def parts 1514: body.parts 1515: end
The raw_envelope is the From mikel@test.lindsaar.net 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
# File lib/mail/message.rb, line 385 385: def raw_envelope 386: @raw_envelope 387: end
Provides access to the raw source of the message as it was when it was instantiated. This is set at initialization and so is untouched by the parsers or decoder / encoders
Example:
mail = Mail.new('This is an invalid email message') mail.raw_source #=> "This is an invalid email message"
# File lib/mail/message.rb, line 372 372: def raw_source 373: @raw_source 374: end
# File lib/mail/message.rb, line 1796 1796: def read 1797: if self.attachment? 1798: decode_body 1799: else 1800: raise NoMethodError, 'Can not call read on a part unless it is an attachment.' 1801: end 1802: end
Encodes the message, calls encode on all it’s parts, gets an email message ready to send
# File lib/mail/message.rb, line 1688 1688: def ready_to_send! 1689: identify_and_set_transfer_encoding 1690: parts.sort!([ "text/plain", "text/enriched", "text/html", "multipart/alternative" ]) 1691: parts.each do |part| 1692: part.transport_encoding = transport_encoding 1693: part.ready_to_send! 1694: end 1695: add_required_fields 1696: end
# File lib/mail/message.rb, line 718 718: def received( val = nil ) 719: if val 720: header[:received] = val 721: else 722: header[:received] 723: end 724: end
# File lib/mail/message.rb, line 726 726: def received=( val ) 727: header[:received] = val 728: end
# File lib/mail/message.rb, line 730 730: def references( val = nil ) 731: default :references, val 732: end
# File lib/mail/message.rb, line 734 734: def references=( val ) 735: header[:references] = val 736: end
# File lib/mail/message.rb, line 207 207: def register_for_delivery_notification(observer) 208: STDERR.puts("Message#register_for_delivery_notification is deprecated, please call Mail.register_observer instead") 209: Mail.register_observer(observer) 210: end
# File lib/mail/message.rb, line 1499 1499: def remote_mta 1500: delivery_status_part and delivery_status_part.remote_mta 1501: end
Returns the Resent-Bcc value of the mail object as an array of strings of address specs.
Example:
mail.resent_bcc = 'Mikel <mikel@test.lindsaar.net>' mail.resent_bcc #=> ['mikel@test.lindsaar.net'] mail.resent_bcc = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.resent_bcc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
Also allows you to set the value by passing a value as a parameter
Example:
mail.resent_bcc 'Mikel <mikel@test.lindsaar.net>' mail.resent_bcc #=> ['mikel@test.lindsaar.net']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.resent_bcc 'Mikel <mikel@test.lindsaar.net>' mail.resent_bcc << 'ada@test.lindsaar.net' mail.resent_bcc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 804 804: def resent_bcc( val = nil ) 805: default :resent_bcc, val 806: end
Sets the Resent-Bcc value of the mail object, pass in a string of the field
Example:
mail.resent_bcc = 'Mikel <mikel@test.lindsaar.net>' mail.resent_bcc #=> ['mikel@test.lindsaar.net'] mail.resent_bcc = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.resent_bcc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 816 816: def resent_bcc=( val ) 817: header[:resent_bcc] = val 818: end
Returns the Resent-Cc value of the mail object as an array of strings of address specs.
Example:
mail.resent_cc = 'Mikel <mikel@test.lindsaar.net>' mail.resent_cc #=> ['mikel@test.lindsaar.net'] mail.resent_cc = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.resent_cc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
Also allows you to set the value by passing a value as a parameter
Example:
mail.resent_cc 'Mikel <mikel@test.lindsaar.net>' mail.resent_cc #=> ['mikel@test.lindsaar.net']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.resent_cc 'Mikel <mikel@test.lindsaar.net>' mail.resent_cc << 'ada@test.lindsaar.net' mail.resent_cc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 845 845: def resent_cc( val = nil ) 846: default :resent_cc, val 847: end
Sets the Resent-Cc value of the mail object, pass in a string of the field
Example:
mail.resent_cc = 'Mikel <mikel@test.lindsaar.net>' mail.resent_cc #=> ['mikel@test.lindsaar.net'] mail.resent_cc = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.resent_cc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 857 857: def resent_cc=( val ) 858: header[:resent_cc] = val 859: end
# File lib/mail/message.rb, line 861 861: def resent_date( val = nil ) 862: default :resent_date, val 863: end
# File lib/mail/message.rb, line 865 865: def resent_date=( val ) 866: header[:resent_date] = val 867: end
Returns the Resent-From value of the mail object as an array of strings of address specs.
Example:
mail.resent_from = 'Mikel <mikel@test.lindsaar.net>' mail.resent_from #=> ['mikel@test.lindsaar.net'] mail.resent_from = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.resent_from #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
Also allows you to set the value by passing a value as a parameter
Example:
mail.resent_from ['Mikel <mikel@test.lindsaar.net>'] mail.resent_from #=> 'mikel@test.lindsaar.net'
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.resent_from 'Mikel <mikel@test.lindsaar.net>' mail.resent_from << 'ada@test.lindsaar.net' mail.resent_from #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 894 894: def resent_from( val = nil ) 895: default :resent_from, val 896: end
Sets the Resent-From value of the mail object, pass in a string of the field
Example:
mail.resent_from = 'Mikel <mikel@test.lindsaar.net>' mail.resent_from #=> ['mikel@test.lindsaar.net'] mail.resent_from = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.resent_from #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 906 906: def resent_from=( val ) 907: header[:resent_from] = val 908: end
# File lib/mail/message.rb, line 910 910: def resent_message_id( val = nil ) 911: default :resent_message_id, val 912: end
# File lib/mail/message.rb, line 914 914: def resent_message_id=( val ) 915: header[:resent_message_id] = val 916: end
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 <mikel@test.lindsaar.net>' mail.resent_sender #=> 'mikel@test.lindsaar.net'
Also allows you to set the value by passing a value as a parameter
Example:
mail.resent_sender 'Mikel <mikel@test.lindsaar.net>' mail.resent_sender #=> 'mikel@test.lindsaar.net'
# File lib/mail/message.rb, line 933 933: def resent_sender( val = nil ) 934: default :resent_sender, val 935: end
Sets the Resent-Sender value of the mail object, pass in a string of the field
Example:
mail.sender = 'Mikel <mikel@test.lindsaar.net>' mail.sender #=> 'mikel@test.lindsaar.net'
# File lib/mail/message.rb, line 943 943: def resent_sender=( val ) 944: header[:resent_sender] = val 945: end
Returns the Resent-To value of the mail object as an array of strings of address specs.
Example:
mail.resent_to = 'Mikel <mikel@test.lindsaar.net>' mail.resent_to #=> ['mikel@test.lindsaar.net'] mail.resent_to = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.resent_to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
Also allows you to set the value by passing a value as a parameter
Example:
mail.resent_to 'Mikel <mikel@test.lindsaar.net>' mail.resent_to #=> ['mikel@test.lindsaar.net']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.resent_to 'Mikel <mikel@test.lindsaar.net>' mail.resent_to << 'ada@test.lindsaar.net' mail.resent_to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 972 972: def resent_to( val = nil ) 973: default :resent_to, val 974: end
Sets the Resent-To value of the mail object, pass in a string of the field
Example:
mail.resent_to = 'Mikel <mikel@test.lindsaar.net>' mail.resent_to #=> ['mikel@test.lindsaar.net'] mail.resent_to = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.resent_to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 984 984: def resent_to=( val ) 985: header[:resent_to] = val 986: end
# File lib/mail/message.rb, line 1503 1503: def retryable? 1504: delivery_status_part and delivery_status_part.retryable? 1505: end
Returns the return path of the mail object, or sets it if you pass a string
# File lib/mail/message.rb, line 989 989: def return_path( val = nil ) 990: default :return_path, val 991: end
Sets the return path of the object
# File lib/mail/message.rb, line 994 994: def return_path=( val ) 995: header[:return_path] = val 996: end
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 <mikel@test.lindsaar.net>' mail.sender #=> 'mikel@test.lindsaar.net'
Also allows you to set the value by passing a value as a parameter
Example:
mail.sender 'Mikel <mikel@test.lindsaar.net>' mail.sender #=> 'mikel@test.lindsaar.net'
# File lib/mail/message.rb, line 1012 1012: def sender( val = nil ) 1013: default :sender, val 1014: end
Sets the Sender value of the mail object, pass in a string of the field
Example:
mail.sender = 'Mikel <mikel@test.lindsaar.net>' mail.sender #=> 'mikel@test.lindsaar.net'
# File lib/mail/message.rb, line 1022 1022: def sender=( val ) 1023: header[:sender] = val 1024: end
Sets the envelope from for the email
# File lib/mail/message.rb, line 377 377: def set_envelope( val ) 378: @raw_envelope = val 379: @envelope = Mail::Envelope.new( val ) 380: end
Skips the deletion of this message. All other messages flagged for delete still will be deleted at session close (i.e. when # exits). Only has an effect if you’re using # or # with :delete_after_find set to true.
# File lib/mail/message.rb, line 1836 1836: def skip_deletion 1837: @mark_for_delete = false 1838: end
Returns the sub content type
# File lib/mail/message.rb, line 1444 1444: def sub_type 1445: has_content_type? ? header[:content_type].sub_type : nil rescue nil 1446: end
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"
# File lib/mail/message.rb, line 1041 1041: def subject( val = nil ) 1042: default :subject, val 1043: end
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"
# File lib/mail/message.rb, line 1051 1051: def subject=( val ) 1052: header[:subject] = val 1053: end
# File lib/mail/message.rb, line 1861 1861: def text? 1862: has_content_type? ? !!(main_type =~ /^text$/) : false 1863: end
Accessor for text_part
# File lib/mail/message.rb, line 1571 1571: def text_part(&block) 1572: if block_given? 1573: @text_part = Mail::Part.new(&block) 1574: add_multipart_alternate_header unless html_part.blank? 1575: add_part(@text_part) 1576: else 1577: @text_part || find_first_mime_type('text/plain') 1578: end 1579: end
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.
# File lib/mail/message.rb, line 1597 1597: def text_part=(msg = nil) 1598: if msg 1599: @text_part = msg 1600: else 1601: @text_part = Mail::Part.new('Content-Type: text/plain;') 1602: end 1603: add_multipart_alternate_header unless html_part.blank? 1604: add_part(@text_part) 1605: end
Returns the To value of the mail object as an array of strings of address specs.
Example:
mail.to = 'Mikel <mikel@test.lindsaar.net>' mail.to #=> ['mikel@test.lindsaar.net'] mail.to = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
Also allows you to set the value by passing a value as a parameter
Example:
mail.to 'Mikel <mikel@test.lindsaar.net>' mail.to #=> ['mikel@test.lindsaar.net']
Additionally, you can append new addresses to the returned Array like object.
Example:
mail.to 'Mikel <mikel@test.lindsaar.net>' mail.to << 'ada@test.lindsaar.net' mail.to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 1080 1080: def to( val = nil ) 1081: default :to, val 1082: end
Sets the To value of the mail object, pass in a string of the field
Example:
mail.to = 'Mikel <mikel@test.lindsaar.net>' mail.to #=> ['mikel@test.lindsaar.net'] mail.to = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net' mail.to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
# File lib/mail/message.rb, line 1092 1092: def to=( val ) 1093: header[:to] = val 1094: end
Returns an array of addresses (the encoded value) in the To field, if no To field, returns an empty array
# File lib/mail/message.rb, line 1194 1194: def to_addrs 1195: to ? [to].flatten : [] 1196: end
# File lib/mail/message.rb, line 1775 1775: def to_s 1776: encoded 1777: end
# File lib/mail/message.rb, line 1729 1729: def to_yaml(opts = {}) 1730: hash = {} 1731: hash['headers'] = {} 1732: header.fields.each do |field| 1733: hash['headers'][field.name] = field.value 1734: end 1735: hash['delivery_handler'] = delivery_handler.to_s if delivery_handler 1736: hash['transport_encoding'] = transport_encoding.to_s 1737: special_variables = [:@header, :@delivery_handler, :@transport_encoding] 1738: if multipart? 1739: hash['multipart_body'] = [] 1740: body.parts.map { |part| hash['multipart_body'] << part.to_yaml } 1741: special_variables.push(:@body, :@text_part, :@html_part) 1742: end 1743: (instance_variables.map(&:to_sym) - special_variables).each do |var| 1744: hash[var.to_s] = instance_variable_get(var) 1745: end 1746: hash.to_yaml(opts) 1747: end
# File lib/mail/message.rb, line 596 596: def transport_encoding( val = nil) 597: if val 598: self.transport_encoding = val 599: else 600: @transport_encoding 601: end 602: end
# File lib/mail/message.rb, line 604 604: def transport_encoding=( val ) 605: @transport_encoding = Mail::Encodings.get_encoding(val) 606: end
# File lib/mail/message.rb, line 1714 1714: def without_attachments! 1715: return self unless has_attachments? 1716: 1717: parts.delete_if { |p| p.attachment? } 1718: body_raw = if parts.empty? 1719: '' 1720: else 1721: body.encoded 1722: end 1723: 1724: @body = Mail::Body.new(body_raw) 1725: 1726: self 1727: end
# File lib/mail/message.rb, line 1958 1958: def add_boundary 1959: unless body.boundary && boundary 1960: header['content-type'] = 'multipart/mixed' unless header['content-type'] 1961: header['content-type'].parameters[:boundary] = ContentTypeField.generate_boundary 1962: header['content_type'].parameters[:charset] = @charset 1963: body.boundary = boundary 1964: end 1965: end
# File lib/mail/message.rb, line 1927 1927: def add_encoding_to_body 1928: if has_content_transfer_encoding? 1929: @body.encoding = content_transfer_encoding 1930: end 1931: end
# File lib/mail/message.rb, line 1952 1952: def add_multipart_alternate_header 1953: header['content-type'] = ContentTypeField.with_boundary('multipart/alternative').value 1954: header['content_type'].parameters[:charset] = @charset 1955: body.boundary = boundary 1956: end
# File lib/mail/message.rb, line 1967 1967: def add_multipart_mixed_header 1968: unless header['content-type'] 1969: header['content-type'] = ContentTypeField.with_boundary('multipart/mixed').value 1970: header['content_type'].parameters[:charset] = @charset 1971: body.boundary = boundary 1972: end 1973: end
# File lib/mail/message.rb, line 1941 1941: def add_required_fields 1942: add_multipart_mixed_header unless !body.multipart? 1943: body = nil if body.nil? 1944: add_message_id unless (has_message_id? || self.class == Mail::Part) 1945: add_date unless has_date? 1946: add_mime_version unless has_mime_version? 1947: add_content_type unless has_content_type? 1948: add_charset unless has_charset? 1949: add_content_transfer_encoding unless has_content_transfer_encoding? 1950: end
see comments to body=. We take data and process it lazily
# File lib/mail/message.rb, line 1891 1891: def body_lazy(value) 1892: process_body_raw if @body_raw && value 1893: case 1894: when value == nil || value.length<=0 1895: @body = Mail::Body.new('') 1896: @body_raw = nil 1897: add_encoding_to_body 1898: when @body && @body.multipart? 1899: @body << Mail::Part.new(value) 1900: add_encoding_to_body 1901: else 1902: @body_raw = value 1903: # process_body_raw 1904: end 1905: end
# File lib/mail/message.rb, line 2041 2041: def decode_body_as_text 2042: body_text = decode_body 2043: if charset 2044: if RUBY_VERSION < '1.9' 2045: require 'iconv' 2046: return Iconv.conv("UTF-8//TRANSLIT//IGNORE", charset, body_text) 2047: else 2048: if encoding = Encoding.find(charset) rescue nil 2049: body_text.force_encoding(encoding) 2050: return body_text.encode(Encoding::UTF_8) 2051: end 2052: end 2053: end 2054: body_text 2055: end
# File lib/mail/message.rb, line 2031 2031: def do_delivery 2032: begin 2033: if perform_deliveries 2034: delivery_method.deliver!(self) 2035: end 2036: rescue Exception => e # Net::SMTP errors or sendmail pipe errors 2037: raise e if raise_delivery_errors 2038: end 2039: end
Returns the filename of the attachment (if it exists) or returns nil
# File lib/mail/message.rb, line 2013 2013: def find_attachment 2014: content_type_name = header[:content_type].filename rescue nil 2015: content_disp_name = header[:content_disposition].filename rescue nil 2016: content_loc_name = header[:content_location].location rescue nil 2017: case 2018: when content_type && content_type_name 2019: filename = content_type_name 2020: when content_disposition && content_disp_name 2021: filename = content_disp_name 2022: when content_location && content_loc_name 2023: filename = content_loc_name 2024: else 2025: filename = nil 2026: end 2027: filename = Mail::Encodings.decode_encode(filename, :decode) if filename rescue filename 2028: filename 2029: end
# File lib/mail/message.rb, line 1933 1933: def identify_and_set_transfer_encoding 1934: if body && body.multipart? 1935: self.content_transfer_encoding = @transport_encoding 1936: else 1937: self.content_transfer_encoding = body.get_best_encoding(@transport_encoding) 1938: end 1939: end
# File lib/mail/message.rb, line 1975 1975: def init_with_hash(hash) 1976: passed_in_options = IndifferentHash.new(hash) 1977: self.raw_source = '' 1978: 1979: @header = Mail::Header.new 1980: @body = Mail::Body.new 1981: @body_raw = nil 1982: 1983: # We need to store the body until last, as we need all headers added first 1984: body_content = nil 1985: 1986: passed_in_options.each_pair do |k,v| 1987: k = underscoreize(k).to_sym if k.class == String 1988: if k == :headers 1989: self.headers(v) 1990: elsif k == :body 1991: body_content = v 1992: else 1993: self[k] = v 1994: end 1995: end 1996: 1997: if body_content 1998: self.body = body_content 1999: if has_content_transfer_encoding? 2000: body.encoding = content_transfer_encoding 2001: end 2002: end 2003: end
# File lib/mail/message.rb, line 2005 2005: def init_with_string(string) 2006: self.raw_source = string 2007: set_envelope_header 2008: parse_message 2009: @separate_parts = multipart? 2010: end
2.1. General Description 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).
Additionally, I allow for the case where someone might have put whitespace on the “gap line“
# File lib/mail/message.rb, line 1877 1877: def parse_message 1878: header_part, body_part = raw_source.split(/#{CRLF}#{WSP}*#{CRLF}/, 2) 1879: # index = raw_source.index(/#{CRLF}#{WSP}*#{CRLF}/m, 2) 1880: # self.header = (index) ? header_part[0,index] : nil 1881: # lazy_body ( [raw_source, index+1]) 1882: self.header = header_part 1883: self.body = body_part 1884: end
# File lib/mail/message.rb, line 1908 1908: def process_body_raw 1909: @body = Mail::Body.new(@body_raw) 1910: @body_raw = nil 1911: separate_parts if @separate_parts 1912: 1913: add_encoding_to_body 1914: end
# File lib/mail/message.rb, line 1886 1886: def raw_source=(value) 1887: @raw_source = value.to_crlf 1888: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.