Returns the main content type
# File lib/mail/message.rb, line 1399 1399: def main_type 1400: has_content_type? ? header[:content_type].main_type : nil rescue nil 1401: end
Object
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.
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
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 98 98: def initialize(*args, &block) 99: @body = nil 100: @body_raw = nil 101: @body_raw_index = nil 102: @separate_parts = false 103: @text_part = nil 104: @html_part = nil 105: @errors = nil 106: @header = nil 107: @charset = 'UTF-8' 108: @defaulted_charset = true 109: 110: @perform_deliveries = true 111: @raise_delivery_errors = true 112: 113: @delivery_handler = nil 114: 115: @delivery_method = Mail.delivery_method.dup 116: 117: @transport_encoding = Mail::Encodings.get_encoding('7bit') 118: 119: @mark_for_delete = false 120: 121: if args.flatten.first.respond_to?(:each_pair) 122: init_with_hash(args.flatten.first) 123: else 124: init_with_string(args.flatten[0].to_s.strip) 125: end 126: 127: if block_given? 128: instance_eval(&block) 129: end 130: 131: self 132: 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 270 270: def <=>(other) 271: if other.nil? 272: 1 273: else 274: self.date <=> other.date 275: end 276: 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 309 309: def ==(other) 310: return false unless other.respond_to?(:encoded) 311: 312: if self.message_id && other.message_id 313: result = (self.encoded == other.encoded) 314: else 315: self_message_id, other_message_id = self.message_id, other.message_id 316: self.message_id, other.message_id = '<temp@test>', '<temp@test>' 317: result = self.encoded == other.encoded 318: self.message_id = "<#{self_message_id}>" if self_message_id 319: other.message_id = "<#{other_message_id}>" if other_message_id 320: result 321: end 322: end
Allows you to read an arbitrary header
Example:
mail['foo'] = '1234' mail['foo'].to_s #=> '1234'
# File lib/mail/message.rb, line 1194 1194: def [](name) 1195: header[underscoreize(name)] 1196: end
Allows you to add an arbitrary header
Example:
mail['foo'] = '1234' mail['foo'].to_s #=> '1234'
# File lib/mail/message.rb, line 1176 1176: def []=(name, value) 1177: if name.to_s == 'body' 1178: self.body = value 1179: elsif name.to_s =~ /content[-_]type/ 1180: header[name] = value 1181: elsif name.to_s == 'charset' 1182: self.charset = value 1183: else 1184: header[name] = value 1185: end 1186: end
# File lib/mail/message.rb, line 1443 1443: def action 1444: delivery_status_part and delivery_status_part.action 1445: end
Adds a content type and charset if the body is US-ASCII
Otherwise raises a warning
# File lib/mail/message.rb, line 1337 1337: def add_charset 1338: if !body.empty? 1339: # Only give a warning if this isn't an attachment, has non US-ASCII and the user 1340: # has not specified an encoding explicitly. 1341: if @defaulted_charset && body.raw_source.not_ascii_only? && !self.attachment? 1342: warning = "Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect.\n" 1343: STDERR.puts(warning) 1344: end 1345: header[:content_type].parameters['charset'] = @charset 1346: end 1347: end
Adds a content transfer encoding
Otherwise raises a warning
# File lib/mail/message.rb, line 1352 1352: def add_content_transfer_encoding 1353: if body.only_us_ascii? 1354: header[:content_transfer_encoding] = '7bit' 1355: else 1356: warning = "Non US-ASCII detected and no content-transfer-encoding defined.\nDefaulting to 8bit, set your own if this is incorrect.\n" 1357: STDERR.puts(warning) 1358: header[:content_transfer_encoding] = '8bit' 1359: end 1360: end
Adds a content type and charset if the body is US-ASCII
Otherwise raises a warning
# File lib/mail/message.rb, line 1330 1330: def add_content_type 1331: header[:content_type] = 'text/plain' 1332: 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 1313 1313: def add_date(date_val = '') 1314: header['date'] = date_val 1315: 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 1624 1624: def add_file(values) 1625: convert_to_multipart unless self.multipart? || self.body.decoded.blank? 1626: add_multipart_mixed_header 1627: if values.is_a?(String) 1628: basename = File.basename(values) 1629: filedata = File.open(values, 'rb') { |f| f.read } 1630: else 1631: basename = values[:filename] 1632: filedata = values[:content] || File.open(values[:filename], 'rb') { |f| f.read } 1633: end 1634: self.attachments[basename] = filedata 1635: 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 1303 1303: def add_message_id(msg_id_val = '') 1304: header['message-id'] = msg_id_val 1305: 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 1323 1323: def add_mime_version(ver_val = '') 1324: header['mime-version'] = ver_val 1325: end
Adds a part to the parts list or creates the part list
# File lib/mail/message.rb, line 1568 1568: def add_part(part) 1569: if !body.multipart? && !self.body.decoded.blank? 1570: @text_part = Mail::Part.new('Content-Type: text/plain;') 1571: @text_part.body = body.decoded 1572: self.body << @text_part 1573: add_multipart_alternate_header 1574: end 1575: add_boundary 1576: self.body << part 1577: end
# File lib/mail/message.rb, line 1720 1720: def all_parts 1721: parts.map { |p| [p, p.all_parts] }.flatten 1722: end
Returns the attachment data if there is any
# File lib/mail/message.rb, line 1711 1711: def attachment 1712: @attachment 1713: end
Returns true if this part is an attachment, false otherwise.
# File lib/mail/message.rb, line 1706 1706: def attachment? 1707: !!find_attachment 1708: 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 1511 1511: def attachments 1512: parts.attachments 1513: 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 435 435: def bcc( val = nil ) 436: default :bcc, val 437: 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 447 447: def bcc=( val ) 448: header[:bcc] = val 449: 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 1166 1166: def bcc_addrs 1167: bcc ? [bcc].flatten : [] 1168: 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 1110 1110: def body(value = nil) 1111: if value 1112: self.body = value 1113: # add_encoding_to_body 1114: else 1115: process_body_raw if @body_raw 1116: @body 1117: end 1118: 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 1096 1096: def body=(value) 1097: body_lazy(value, 0) 1098: end
# File lib/mail/message.rb, line 1120 1120: def body_encoding(value) 1121: if value.nil? 1122: body.encoding 1123: else 1124: body.encoding = value 1125: end 1126: end
# File lib/mail/message.rb, line 1128 1128: def body_encoding=(value) 1129: body.encoding = value 1130: end
# File lib/mail/message.rb, line 1439 1439: def bounced? 1440: delivery_status_part and delivery_status_part.bounced? 1441: end
Returns the current boundary for this message part
# File lib/mail/message.rb, line 1468 1468: def boundary 1469: content_type_parameters ? content_type_parameters['boundary'] : nil 1470: 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 476 476: def cc( val = nil ) 477: default :cc, val 478: 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 488 488: def cc=( val ) 489: header[:cc] = val 490: 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 1160 1160: def cc_addrs 1161: cc ? [cc].flatten : [] 1162: end
Returns the character set defined in the content type field
# File lib/mail/message.rb, line 1383 1383: def charset 1384: if @header 1385: content_type ? content_type_parameters['charset'] : @charset 1386: else 1387: @charset 1388: end 1389: end
Sets the charset to the supplied value.
# File lib/mail/message.rb, line 1392 1392: def charset=(value) 1393: @defaulted_charset = false 1394: @charset = value 1395: @header.charset = value 1396: end
# File lib/mail/message.rb, line 496 496: def comments=( val ) 497: header[:comments] = val 498: end
# File lib/mail/message.rb, line 500 500: def content_description( val = nil ) 501: default :content_description, val 502: end
# File lib/mail/message.rb, line 504 504: def content_description=( val ) 505: header[:content_description] = val 506: end
# File lib/mail/message.rb, line 508 508: def content_disposition( val = nil ) 509: default :content_disposition, val 510: end
# File lib/mail/message.rb, line 512 512: def content_disposition=( val ) 513: header[:content_disposition] = val 514: end
# File lib/mail/message.rb, line 516 516: def content_id( val = nil ) 517: default :content_id, val 518: end
# File lib/mail/message.rb, line 520 520: def content_id=( val ) 521: header[:content_id] = val 522: end
# File lib/mail/message.rb, line 524 524: def content_location( val = nil ) 525: default :content_location, val 526: end
# File lib/mail/message.rb, line 528 528: def content_location=( val ) 529: header[:content_location] = val 530: end
# File lib/mail/message.rb, line 532 532: def content_transfer_encoding( val = nil ) 533: default :content_transfer_encoding, val 534: end
# File lib/mail/message.rb, line 536 536: def content_transfer_encoding=( val ) 537: header[:content_transfer_encoding] = val 538: end
# File lib/mail/message.rb, line 540 540: def content_type( val = nil ) 541: default :content_type, val 542: end
# File lib/mail/message.rb, line 544 544: def content_type=( val ) 545: header[:content_type] = val 546: end
Returns the content type parameters
# File lib/mail/message.rb, line 1415 1415: def content_type_parameters 1416: has_content_type? ? header[:content_type].parameters : nil rescue nil 1417: end
# File lib/mail/message.rb, line 1637 1637: def convert_to_multipart 1638: text = body.decoded 1639: self.body = '' 1640: text_part = Mail::Part.new({:content_type => 'text/plain;', 1641: :body => text}) 1642: self.body << text_part 1643: end
# File lib/mail/message.rb, line 548 548: def date( val = nil ) 549: default :date, val 550: end
# File lib/mail/message.rb, line 552 552: def date=( val ) 553: header[:date] = val 554: end
# File lib/mail/message.rb, line 1700 1700: def decode_body 1701: body.decoded 1702: end
# File lib/mail/message.rb, line 1681 1681: def decoded 1682: case 1683: when self.attachment? 1684: decode_body 1685: when !self.multipart? 1686: body.decoded 1687: else 1688: 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.' 1689: end 1690: 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 1063 1063: def default( sym, val = nil ) 1064: if val 1065: header[sym] = val 1066: else 1067: header[sym].default if header[sym] 1068: end 1069: end
Delivers an mail object.
Examples:
mail = Mail.read('file.eml') mail.deliver
# File lib/mail/message.rb, line 225 225: def deliver 226: inform_interceptors 227: if delivery_handler 228: delivery_handler.deliver_mail(self) { do_delivery } 229: else 230: do_delivery 231: end 232: inform_observers 233: self 234: end
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
# File lib/mail/message.rb, line 242 242: def deliver! 243: delivery_method.deliver!(self) 244: inform_observers 245: self 246: end
# File lib/mail/message.rb, line 248 248: def delivery_method(method = nil, settings = {}) 249: unless method 250: @delivery_method 251: else 252: @delivery_method = Configuration.instance.lookup_delivery_method(method).new(settings) 253: end 254: end
returns the part in a multipart/report email that has the content-type delivery-status
# File lib/mail/message.rb, line 1435 1435: def delivery_status_part 1436: @delivery_stats_part ||= parts.select { |p| p.delivery_status_report_part? }.first 1437: end
Returns true if the message is a multipart/report; report-type=delivery-status;
# File lib/mail/message.rb, line 1430 1430: def delivery_status_report? 1431: multipart_report? && content_type_parameters['report-type'] =~ /^delivery-status$/ 1432: 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 1142 1142: def destinations 1143: [to_addrs, cc_addrs, bcc_addrs].compact.flatten 1144: end
# File lib/mail/message.rb, line 1455 1455: def diagnostic_code 1456: delivery_status_part and delivery_status_part.diagnostic_code 1457: end
# File lib/mail/message.rb, line 1657 1657: def encode! 1658: STDERR.puts("Deprecated in 1.1.0 in favour of :ready_to_send! as it is less confusing with encoding and decoding.") 1659: ready_to_send! 1660: 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 1665 1665: def encoded 1666: ready_to_send! 1667: buffer = header.encoded 1668: buffer << "\r\n" 1669: buffer << body.encoded(content_transfer_encoding) 1670: buffer 1671: end
# File lib/mail/message.rb, line 353 353: def envelope_date 354: @envelope ? @envelope.date : nil 355: end
# File lib/mail/message.rb, line 349 349: def envelope_from 350: @envelope ? @envelope.from : nil 351: end
# File lib/mail/message.rb, line 1451 1451: def error_status 1452: delivery_status_part and delivery_status_part.error_status 1453: 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 406 406: def errors 407: header.errors 408: end
Returns the filename of the attachment
# File lib/mail/message.rb, line 1716 1716: def filename 1717: find_attachment 1718: end
# File lib/mail/message.rb, line 1447 1447: def final_recipient 1448: delivery_status_part and delivery_status_part.final_recipient 1449: end
# File lib/mail/message.rb, line 1724 1724: def find_first_mime_type(mt) 1725: all_parts.detect { |p| p.mime_type == mt } 1726: 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 593 593: def from( val = nil ) 594: default :from, val 595: 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 605 605: def from=( val ) 606: header[:from] = val 607: 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 1148 1148: def from_addrs 1149: from ? [from].flatten : [] 1150: end
# File lib/mail/message.rb, line 1515 1515: def has_attachments? 1516: !attachments.empty? 1517: end
# File lib/mail/message.rb, line 1283 1283: def has_charset? 1284: tmp = header[:content_type].parameters rescue nil 1285: !!(has_content_type? && tmp && tmp['charset']) 1286: end
# File lib/mail/message.rb, line 1288 1288: def has_content_transfer_encoding? 1289: header[:content_transfer_encoding] && header[:content_transfer_encoding].errors.blank? 1290: end
# File lib/mail/message.rb, line 1278 1278: def has_content_type? 1279: tmp = header[:content_type].main_type rescue nil 1280: !!tmp 1281: 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 1268 1268: def has_date? 1269: header.has_date? 1270: 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 1262 1262: def has_message_id? 1263: header.has_message_id? 1264: 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 1274 1274: def has_mime_version? 1275: header.has_mime_version? 1276: 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 378 378: def header(value = nil) 379: value ? self.header = value : @header 380: 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 363 363: def header=(value) 364: @header = Mail::Header.new(value, charset) 365: 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 1256 1256: def header_fields 1257: header.fields 1258: end
Provides a way to set custom headers, by passing in a hash
# File lib/mail/message.rb, line 383 383: def headers(hash = {}) 384: hash.each_pair do |k,v| 385: header[k] = v 386: end 387: end
Accessor for html_part
# File lib/mail/message.rb, line 1520 1520: def html_part(&block) 1521: if block_given? 1522: @html_part = Mail::Part.new(&block) 1523: add_multipart_alternate_header unless html_part.blank? 1524: add_part(@html_part) 1525: else 1526: @html_part || find_first_mime_type('text/html') 1527: end 1528: 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 1544 1544: def html_part=(msg = nil) 1545: if msg 1546: @html_part = msg 1547: else 1548: @html_part = Mail::Part.new('Content-Type: text/html;') 1549: end 1550: add_multipart_alternate_header unless text_part.blank? 1551: add_part(@html_part) 1552: end
# File lib/mail/message.rb, line 609 609: def in_reply_to( val = nil ) 610: default :in_reply_to, val 611: end
# File lib/mail/message.rb, line 613 613: def in_reply_to=( val ) 614: header[:in_reply_to] = val 615: end
# File lib/mail/message.rb, line 215 215: def inform_interceptors 216: Mail.inform_interceptors(self) 217: end
# File lib/mail/message.rb, line 211 211: def inform_observers 212: Mail.inform_observers(self) 213: end
# File lib/mail/message.rb, line 1677 1677: def inspect 1678: "#<#{self.class}:#{self.object_id}, Multipart: #{multipart?}, Headers: #{header.field_summary}>" 1679: 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 1753 1753: def is_marked_for_delete? 1754: return @mark_for_delete 1755: end
# File lib/mail/message.rb, line 617 617: def keywords( val = nil ) 618: default :keywords, val 619: end
# File lib/mail/message.rb, line 621 621: def keywords=( val ) 622: header[:keywords] = val 623: end
Returns the main content type
# File lib/mail/message.rb, line 1399 1399: def main_type 1400: has_content_type? ? header[:content_type].main_type : nil rescue nil 1401: 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 1740 1740: def mark_for_delete=(value = true) 1741: @mark_for_delete = value 1742: end
# File lib/mail/message.rb, line 1377 1377: def message_content_type 1378: STDERR.puts(":message_content_type is deprecated in Mail 1.4.3. Please use mime_type\n#{caller}") 1379: mime_type 1380: 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 638 638: def message_id( val = nil ) 639: default :message_id, val 640: 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 647 647: def message_id=( val ) 648: header[:message_id] = val 649: 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 1237 1237: def method_missing(name, *args, &block) 1238: #:nodoc: 1239: # Only take the structured fields, as we could take _anything_ really 1240: # as it could become an optional field... "but therin lies the dark side" 1241: field_name = underscoreize(name).chomp("=") 1242: if Mail::Field::KNOWN_FIELDS.include?(field_name) 1243: if args.empty? 1244: header[field_name] 1245: else 1246: header[field_name] = args.first 1247: end 1248: else 1249: super # otherwise pass it on 1250: end 1251: #:startdoc: 1252: end
Returns the content type parameters
# File lib/mail/message.rb, line 1409 1409: def mime_parameters 1410: STDERR.puts(':mime_parameters is deprecated in Mail 1.4.3, please use :content_type_parameters instead') 1411: content_type_parameters 1412: 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 1373 1373: def mime_type 1374: content_type ? header[:content_type].string : nil rescue nil 1375: 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 664 664: def mime_version( val = nil ) 665: default :mime_version, val 666: 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 674 674: def mime_version=( val ) 675: header[:mime_version] = val 676: end
Returns true if the message is multipart
# File lib/mail/message.rb, line 1420 1420: def multipart? 1421: has_content_type? ? !!(main_type =~ /^multipart$/) : false 1422: end
Returns true if the message is a multipart/report
# File lib/mail/message.rb, line 1425 1425: def multipart_report? 1426: multipart? && sub_type =~ /^report$/ 1427: 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 1589 1589: def part(params = {}) 1590: new_part = Part.new(params) 1591: yield new_part if block_given? 1592: add_part(new_part) 1593: end
Returns a parts list object of all the parts in the message
# File lib/mail/message.rb, line 1473 1473: def parts 1474: body.parts 1475: 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 345 345: def raw_envelope 346: @raw_envelope 347: 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 332 332: def raw_source 333: @raw_source 334: end
# File lib/mail/message.rb, line 1692 1692: def read 1693: if self.attachment? 1694: decode_body 1695: else 1696: raise NoMethodError, 'Can not call read on a part unless it is an attachment.' 1697: end 1698: end
Encodes the message, calls encode on all it’s parts, gets an email message ready to send
# File lib/mail/message.rb, line 1647 1647: def ready_to_send! 1648: identify_and_set_transfer_encoding 1649: parts.sort!([ "text/plain", "text/enriched", "text/html", "multipart/alternative" ]) 1650: parts.each do |part| 1651: part.transport_encoding = transport_encoding 1652: part.ready_to_send! 1653: end 1654: add_required_fields 1655: end
# File lib/mail/message.rb, line 678 678: def received( val = nil ) 679: if val 680: header[:received] = val 681: else 682: header[:received] 683: end 684: end
# File lib/mail/message.rb, line 686 686: def received=( val ) 687: header[:received] = val 688: end
# File lib/mail/message.rb, line 690 690: def references( val = nil ) 691: default :references, val 692: end
# File lib/mail/message.rb, line 694 694: def references=( val ) 695: header[:references] = val 696: end
# File lib/mail/message.rb, line 206 206: def register_for_delivery_notification(observer) 207: STDERR.puts("Message#register_for_delivery_notification is deprecated, please call Mail.register_observer instead") 208: Mail.register_observer(observer) 209: end
# File lib/mail/message.rb, line 1459 1459: def remote_mta 1460: delivery_status_part and delivery_status_part.remote_mta 1461: 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 764 764: def resent_bcc( val = nil ) 765: default :resent_bcc, val 766: 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 776 776: def resent_bcc=( val ) 777: header[:resent_bcc] = val 778: 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 805 805: def resent_cc( val = nil ) 806: default :resent_cc, val 807: 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 817 817: def resent_cc=( val ) 818: header[:resent_cc] = val 819: end
# File lib/mail/message.rb, line 821 821: def resent_date( val = nil ) 822: default :resent_date, val 823: end
# File lib/mail/message.rb, line 825 825: def resent_date=( val ) 826: header[:resent_date] = val 827: 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 854 854: def resent_from( val = nil ) 855: default :resent_from, val 856: 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 866 866: def resent_from=( val ) 867: header[:resent_from] = val 868: end
# File lib/mail/message.rb, line 870 870: def resent_message_id( val = nil ) 871: default :resent_message_id, val 872: end
# File lib/mail/message.rb, line 874 874: def resent_message_id=( val ) 875: header[:resent_message_id] = val 876: 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 893 893: def resent_sender( val = nil ) 894: default :resent_sender, val 895: 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 903 903: def resent_sender=( val ) 904: header[:resent_sender] = val 905: 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 932 932: def resent_to( val = nil ) 933: default :resent_to, val 934: 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 944 944: def resent_to=( val ) 945: header[:resent_to] = val 946: end
# File lib/mail/message.rb, line 1463 1463: def retryable? 1464: delivery_status_part and delivery_status_part.retryable? 1465: end
Returns the return path of the mail object, or sets it if you pass a string
# File lib/mail/message.rb, line 949 949: def return_path( val = nil ) 950: default :return_path, val 951: end
Sets the return path of the object
# File lib/mail/message.rb, line 954 954: def return_path=( val ) 955: header[:return_path] = val 956: 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 972 972: def sender( val = nil ) 973: default :sender, val 974: 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 982 982: def sender=( val ) 983: header[:sender] = val 984: end
Sets the envelope from for the email
# File lib/mail/message.rb, line 337 337: def set_envelope( val ) 338: @raw_envelope = val 339: @envelope = Mail::Envelope.new( val ) 340: 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 1732 1732: def skip_deletion 1733: @mark_for_delete = false 1734: end
Returns the sub content type
# File lib/mail/message.rb, line 1404 1404: def sub_type 1405: has_content_type? ? header[:content_type].sub_type : nil rescue nil 1406: 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 1001 1001: def subject( val = nil ) 1002: default :subject, val 1003: 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 1011 1011: def subject=( val ) 1012: header[:subject] = val 1013: end
Accessor for text_part
# File lib/mail/message.rb, line 1531 1531: def text_part(&block) 1532: if block_given? 1533: @text_part = Mail::Part.new(&block) 1534: add_multipart_alternate_header unless html_part.blank? 1535: add_part(@text_part) 1536: else 1537: @text_part || find_first_mime_type('text/plain') 1538: end 1539: 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 1557 1557: def text_part=(msg = nil) 1558: if msg 1559: @text_part = msg 1560: else 1561: @text_part = Mail::Part.new('Content-Type: text/plain;') 1562: end 1563: add_multipart_alternate_header unless html_part.blank? 1564: add_part(@text_part) 1565: 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 1040 1040: def to( val = nil ) 1041: default :to, val 1042: 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 1052 1052: def to=( val ) 1053: header[:to] = val 1054: 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 1154 1154: def to_addrs 1155: to ? [to].flatten : [] 1156: end
# File lib/mail/message.rb, line 1673 1673: def to_s 1674: encoded 1675: end
# File lib/mail/message.rb, line 1853 1853: def add_boundary 1854: unless body.boundary && boundary 1855: header['content-type'] = 'multipart/mixed' unless header['content-type'] 1856: header['content-type'].parameters[:boundary] = ContentTypeField.generate_boundary 1857: header['content_type'].parameters[:charset] = @charset 1858: body.boundary = boundary 1859: end 1860: end
# File lib/mail/message.rb, line 1822 1822: def add_encoding_to_body 1823: if has_content_transfer_encoding? 1824: @body.encoding = content_transfer_encoding 1825: end 1826: end
# File lib/mail/message.rb, line 1847 1847: def add_multipart_alternate_header 1848: header['content-type'] = ContentTypeField.with_boundary('multipart/alternative').value 1849: header['content_type'].parameters[:charset] = @charset 1850: body.boundary = boundary 1851: end
# File lib/mail/message.rb, line 1862 1862: def add_multipart_mixed_header 1863: unless header['content-type'] 1864: header['content-type'] = ContentTypeField.with_boundary('multipart/mixed').value 1865: header['content_type'].parameters[:charset] = @charset 1866: body.boundary = boundary 1867: end 1868: end
# File lib/mail/message.rb, line 1836 1836: def add_required_fields 1837: add_multipart_mixed_header unless !body.multipart? 1838: body = nil if body.nil? 1839: add_message_id unless (has_message_id? || self.class == Mail::Part) 1840: add_date unless has_date? 1841: add_mime_version unless has_mime_version? 1842: add_content_type unless has_content_type? 1843: add_charset unless has_charset? 1844: add_content_transfer_encoding unless has_content_transfer_encoding? 1845: end
see comments to body=. We take data starting from index and process it lazily
# File lib/mail/message.rb, line 1783 1783: def body_lazy(value, index) 1784: process_body_raw if @body_raw && value 1785: case 1786: when value == nil || value.length<=index 1787: @body = Mail::Body.new('') 1788: @body_raw = nil 1789: @body_raw_index = nil 1790: add_encoding_to_body 1791: when @body && @body.multipart? 1792: @body << Mail::Part.new(value[index, value.length-index]) 1793: add_encoding_to_body 1794: else 1795: @body_raw = value 1796: @body_raw_index = index 1797: # process_body_raw 1798: end 1799: end
# File lib/mail/message.rb, line 1925 1925: def do_delivery 1926: begin 1927: if perform_deliveries 1928: delivery_method.deliver!(self) 1929: end 1930: rescue Exception => e # Net::SMTP errors or sendmail pipe errors 1931: raise e if raise_delivery_errors 1932: end 1933: end
Returns the filename of the attachment (if it exists) or returns nil
# File lib/mail/message.rb, line 1908 1908: def find_attachment 1909: content_type_name = header[:content_type].filename rescue nil 1910: content_disp_name = header[:content_disposition].filename rescue nil 1911: content_loc_name = header[:content_location].location rescue nil 1912: case 1913: when content_type && content_type_name 1914: filename = content_type_name 1915: when content_disposition && content_disp_name 1916: filename = content_disp_name 1917: when content_location && content_loc_name 1918: filename = content_loc_name 1919: else 1920: filename = nil 1921: end 1922: filename 1923: end
# File lib/mail/message.rb, line 1828 1828: def identify_and_set_transfer_encoding 1829: if body && body.multipart? 1830: self.content_transfer_encoding = @transport_encoding 1831: else 1832: self.content_transfer_encoding = body.get_best_encoding(@transport_encoding) 1833: end 1834: end
# File lib/mail/message.rb, line 1870 1870: def init_with_hash(hash) 1871: passed_in_options = hash.with_indifferent_access 1872: self.raw_source = '' 1873: 1874: @header = Mail::Header.new 1875: @body = Mail::Body.new 1876: @body_raw = nil 1877: 1878: # We need to store the body until last, as we need all headers added first 1879: body_content = nil 1880: 1881: passed_in_options.each_pair do |k,v| 1882: k = underscoreize(k).to_sym if k.class == String 1883: if k == :headers 1884: self.headers(v) 1885: elsif k == :body 1886: body_content = v 1887: else 1888: self[k] = v 1889: end 1890: end 1891: 1892: if body_content 1893: self.body = body_content 1894: if has_content_transfer_encoding? 1895: body.encoding = content_transfer_encoding 1896: end 1897: end 1898: end
# File lib/mail/message.rb, line 1900 1900: def init_with_string(string) 1901: self.raw_source = string 1902: set_envelope_header 1903: parse_message 1904: @separate_parts = multipart? 1905: 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 1769 1769: def parse_message 1770: header_part, body_part = raw_source.split(/#{CRLF}#{WSP}*#{CRLF}/, 2) 1771: # index = raw_source.index(/#{CRLF}#{WSP}*#{CRLF}/m, 2) 1772: # self.header = (index) ? header_part[0,index] : nil 1773: # lazy_body ( [raw_source, index+1]) 1774: self.header = header_part 1775: self.body = body_part 1776: end
# File lib/mail/message.rb, line 1802 1802: def process_body_raw 1803: @body = Mail::Body.new(@body_raw[@body_raw_index, @body_raw.length-@body_raw_index]) 1804: @body_raw = nil 1805: @body_raw_index = nil 1806: separate_parts if @separate_parts 1807: 1808: add_encoding_to_body 1809: end
# File lib/mail/message.rb, line 1778 1778: def raw_source=(value) 1779: @raw_source = value.to_crlf 1780: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.