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 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: @text_part = nil 101: @html_part = nil 102: @errors = nil 103: @header = nil 104: @charset = 'UTF-8' 105: @defaulted_charset = true 106: 107: @perform_deliveries = true 108: @raise_delivery_errors = true 109: 110: @delivery_handler = nil 111: 112: @delivery_method = Mail.delivery_method.dup 113: 114: @transport_encoding = Mail::Encodings.get_encoding('7bit') 115: 116: if args.flatten.first.respond_to?(:each_pair) 117: init_with_hash(args.flatten.first) 118: else 119: init_with_string(args.flatten[0].to_s.strip) 120: end 121: 122: if block_given? 123: instance_eval(&block) 124: end 125: 126: self 127: 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 265 265: def <=>(other) 266: if other.nil? 267: 1 268: else 269: self.date <=> other.date 270: end 271: 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 304 304: def ==(other) 305: return false unless other.respond_to?(:encoded) 306: 307: if self.message_id && other.message_id 308: result = (self.encoded == other.encoded) 309: else 310: self_message_id, other_message_id = self.message_id, other.message_id 311: self.message_id, other.message_id = '<temp@test>', '<temp@test>' 312: result = self.encoded == other.encoded 313: self.message_id = "<#{self_message_id}>" if self_message_id 314: other.message_id = "<#{other_message_id}>" if other_message_id 315: result 316: end 317: end
Allows you to read an arbitrary header
Example:
mail['foo'] = '1234' mail['foo'].to_s #=> '1234'
# File lib/mail/message.rb, line 1196 1196: def [](name) 1197: header[underscoreize(name)] 1198: end
Allows you to add an arbitrary header
Example:
mail['foo'] = '1234' mail['foo'].to_s #=> '1234'
# File lib/mail/message.rb, line 1178 1178: def []=(name, value) 1179: if name.to_s == 'body' 1180: self.body = value 1181: elsif name.to_s =~ /content[-_]type/ 1182: header[name] = value 1183: elsif name.to_s == 'charset' 1184: self.charset = value 1185: else 1186: header[name] = value 1187: end 1188: 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.read(values) 1630: else 1631: basename = values[:filename] 1632: filedata = values[:content] || File.read(values[:filename]) 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 1719 1719: def all_parts 1720: parts.map { |p| [p, p.all_parts] }.flatten 1721: end
Returns the attachment data if there is any
# File lib/mail/message.rb, line 1710 1710: def attachment 1711: @attachment 1712: end
Returns true if this part is an attachment
# File lib/mail/message.rb, line 1705 1705: def attachment? 1706: find_attachment 1707: 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 430 430: def bcc( val = nil ) 431: default :bcc, val 432: 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 442 442: def bcc=( val ) 443: header[:bcc] = val 444: 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 1168 1168: def bcc_addrs 1169: bcc ? [bcc].flatten : [] 1170: 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 1113 1113: def body(value = nil) 1114: if value 1115: self.body = value 1116: add_encoding_to_body 1117: else 1118: @body 1119: end 1120: 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 1091 1091: def body=(value) 1092: case 1093: when value == nil 1094: @body = Mail::Body.new('') 1095: when @body && @body.multipart? 1096: @body << Mail::Part.new(value) 1097: else 1098: @body = Mail::Body.new(value) 1099: end 1100: add_encoding_to_body 1101: end
# File lib/mail/message.rb, line 1122 1122: def body_encoding(value) 1123: if value.nil? 1124: body.encoding 1125: else 1126: body.encoding = value 1127: end 1128: end
# File lib/mail/message.rb, line 1130 1130: def body_encoding=(value) 1131: body.encoding = value 1132: 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 471 471: def cc( val = nil ) 472: default :cc, val 473: 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 483 483: def cc=( val ) 484: header[:cc] = val 485: 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 1162 1162: def cc_addrs 1163: cc ? [cc].flatten : [] 1164: 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 491 491: def comments=( val ) 492: header[:comments] = val 493: end
# File lib/mail/message.rb, line 495 495: def content_description( val = nil ) 496: default :content_description, val 497: end
# File lib/mail/message.rb, line 499 499: def content_description=( val ) 500: header[:content_description] = val 501: end
# File lib/mail/message.rb, line 503 503: def content_disposition( val = nil ) 504: default :content_disposition, val 505: end
# File lib/mail/message.rb, line 507 507: def content_disposition=( val ) 508: header[:content_disposition] = val 509: end
# File lib/mail/message.rb, line 511 511: def content_id( val = nil ) 512: default :content_id, val 513: end
# File lib/mail/message.rb, line 515 515: def content_id=( val ) 516: header[:content_id] = val 517: end
# File lib/mail/message.rb, line 519 519: def content_location( val = nil ) 520: default :content_location, val 521: end
# File lib/mail/message.rb, line 523 523: def content_location=( val ) 524: header[:content_location] = val 525: end
# File lib/mail/message.rb, line 527 527: def content_transfer_encoding( val = nil ) 528: default :content_transfer_encoding, val 529: end
# File lib/mail/message.rb, line 531 531: def content_transfer_encoding=( val ) 532: header[:content_transfer_encoding] = val 533: end
# File lib/mail/message.rb, line 535 535: def content_type( val = nil ) 536: default :content_type, val 537: end
# File lib/mail/message.rb, line 539 539: def content_type=( val ) 540: header[:content_type] = val 541: 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 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 543 543: def date( val = nil ) 544: default :date, val 545: end
# File lib/mail/message.rb, line 547 547: def date=( val ) 548: header[:date] = val 549: 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 1058 1058: def default( sym, val = nil ) 1059: if val 1060: header[sym] = val 1061: else 1062: header[sym].default if header[sym] 1063: end 1064: end
Delivers an mail object.
Examples:
mail = Mail.read('file.eml') mail.deliver
# File lib/mail/message.rb, line 220 220: def deliver 221: inform_interceptors 222: if delivery_handler 223: delivery_handler.deliver_mail(self) { do_delivery } 224: else 225: do_delivery 226: end 227: inform_observers 228: self 229: 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 237 237: def deliver! 238: delivery_method.deliver!(self) 239: inform_observers 240: self 241: end
# File lib/mail/message.rb, line 243 243: def delivery_method(method = nil, settings = {}) 244: unless method 245: @delivery_method 246: else 247: @delivery_method = Mail::Configuration.instance.lookup_delivery_method(method).new(settings) 248: end 249: 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 1144 1144: def destinations 1145: [to_addrs, cc_addrs, bcc_addrs].compact.flatten 1146: 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 348 348: def envelope_date 349: @envelope ? @envelope.date : nil 350: end
# File lib/mail/message.rb, line 344 344: def envelope_from 345: @envelope ? @envelope.from : nil 346: 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 401 401: def errors 402: header.errors 403: end
Returns the filename of the attachment
# File lib/mail/message.rb, line 1715 1715: def filename 1716: find_attachment 1717: 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 1723 1723: def find_first_mime_type(mt) 1724: all_parts.detect { |p| p.mime_type == mt } 1725: 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 588 588: def from( val = nil ) 589: default :from, val 590: 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 600 600: def from=( val ) 601: header[:from] = val 602: 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 1150 1150: def from_addrs 1151: from ? [from].flatten : [] 1152: end
# File lib/mail/message.rb, line 1515 1515: def has_attachments? 1516: !attachments.empty? 1517: end
# File lib/mail/message.rb, line 1284 1284: def has_charset? 1285: !!(header[:content_type] && header[:content_type].parameters['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 1280 1280: def has_content_type? 1281: !!header[:content_type] 1282: 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 1270 1270: def has_date? 1271: header.has_date? 1272: 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 1264 1264: def has_message_id? 1265: header.has_message_id? 1266: 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 1276 1276: def has_mime_version? 1277: header.has_mime_version? 1278: 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 373 373: def header(value = nil) 374: value ? self.header = value : @header 375: 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 358 358: def header=(value) 359: @header = Mail::Header.new(value, charset) 360: 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 1258 1258: def header_fields 1259: header.fields 1260: end
Provides a way to set custom headers, by passing in a hash
# File lib/mail/message.rb, line 378 378: def headers(hash = {}) 379: hash.each_pair do |k,v| 380: header[k] = v 381: end 382: 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 604 604: def in_reply_to( val = nil ) 605: default :in_reply_to, val 606: end
# File lib/mail/message.rb, line 608 608: def in_reply_to=( val ) 609: header[:in_reply_to] = val 610: end
# File lib/mail/message.rb, line 210 210: def inform_interceptors 211: Mail.inform_interceptors(self) 212: end
# File lib/mail/message.rb, line 206 206: def inform_observers 207: Mail.inform_observers(self) 208: 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
# File lib/mail/message.rb, line 612 612: def keywords( val = nil ) 613: default :keywords, val 614: end
# File lib/mail/message.rb, line 616 616: def keywords=( val ) 617: header[:keywords] = val 618: 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 1401: 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 633 633: def message_id( val = nil ) 634: default :message_id, val 635: 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 642 642: def message_id=( val ) 643: header[:message_id] = val 644: 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 1239 1239: def method_missing(name, *args, &block) 1240: #:nodoc: 1241: # Only take the structured fields, as we could take _anything_ really 1242: # as it could become an optional field... "but therin lies the dark side" 1243: field_name = underscoreize(name).chomp("=") 1244: if Mail::Field::KNOWN_FIELDS.include?(field_name) 1245: if args.empty? 1246: header[field_name] 1247: else 1248: header[field_name] = args.first 1249: end 1250: else 1251: super # otherwise pass it on 1252: end 1253: #:startdoc: 1254: 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 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 659 659: def mime_version( val = nil ) 660: default :mime_version, val 661: 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 669 669: def mime_version=( val ) 670: header[:mime_version] = val 671: 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 340 340: def raw_envelope 341: @raw_envelope 342: 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 327 327: def raw_source 328: @raw_source 329: 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 673 673: def received( val = nil ) 674: if val 675: header[:received] = val 676: else 677: header[:received] 678: end 679: end
# File lib/mail/message.rb, line 681 681: def received=( val ) 682: header[:received] = val 683: end
# File lib/mail/message.rb, line 685 685: def references( val = nil ) 686: default :references, val 687: end
# File lib/mail/message.rb, line 689 689: def references=( val ) 690: header[:references] = val 691: end
# File lib/mail/message.rb, line 201 201: def register_for_delivery_notification(observer) 202: STDERR.puts("Message#register_for_delivery_notification is deprecated, please call Mail.register_observer instead") 203: Mail.register_observer(observer) 204: 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 759 759: def resent_bcc( val = nil ) 760: default :resent_bcc, val 761: 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 771 771: def resent_bcc=( val ) 772: header[:resent_bcc] = val 773: 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 800 800: def resent_cc( val = nil ) 801: default :resent_cc, val 802: 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 812 812: def resent_cc=( val ) 813: header[:resent_cc] = val 814: end
# File lib/mail/message.rb, line 816 816: def resent_date( val = nil ) 817: default :resent_date, val 818: end
# File lib/mail/message.rb, line 820 820: def resent_date=( val ) 821: header[:resent_date] = val 822: 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 849 849: def resent_from( val = nil ) 850: default :resent_from, val 851: 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 861 861: def resent_from=( val ) 862: header[:resent_from] = val 863: end
# File lib/mail/message.rb, line 865 865: def resent_message_id( val = nil ) 866: default :resent_message_id, val 867: end
# File lib/mail/message.rb, line 869 869: def resent_message_id=( val ) 870: header[:resent_message_id] = val 871: 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 888 888: def resent_sender( val = nil ) 889: default :resent_sender, val 890: 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 898 898: def resent_sender=( val ) 899: header[:resent_sender] = val 900: 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 927 927: def resent_to( val = nil ) 928: default :resent_to, val 929: 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 939 939: def resent_to=( val ) 940: header[:resent_to] = val 941: 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 944 944: def return_path( val = nil ) 945: default :return_path, val 946: end
Sets the return path of the object
# File lib/mail/message.rb, line 949 949: def return_path=( val ) 950: header[:return_path] = val 951: 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 967 967: def sender( val = nil ) 968: default :sender, val 969: 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 977 977: def sender=( val ) 978: header[:sender] = val 979: end
Sets the envelope from for the email
# File lib/mail/message.rb, line 332 332: def set_envelope( val ) 333: @raw_envelope = val 334: @envelope = Mail::Envelope.new( val ) 335: 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 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 996 996: def subject( val = nil ) 997: default :subject, val 998: 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 1006 1006: def subject=( val ) 1007: header[:subject] = val 1008: 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 1035 1035: def to( val = nil ) 1036: default :to, val 1037: 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 1047 1047: def to=( val ) 1048: header[:to] = val 1049: 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 1156 1156: def to_addrs 1157: to ? [to].flatten : [] 1158: end
# File lib/mail/message.rb, line 1673 1673: def to_s 1674: encoded 1675: end
# File lib/mail/message.rb, line 1791 1791: def add_boundary 1792: unless body.boundary && boundary 1793: header['content-type'] = 'multipart/mixed' unless header['content-type'] 1794: header['content-type'].parameters[:boundary] = ContentTypeField.generate_boundary 1795: header['content_type'].parameters[:charset] = @charset 1796: body.boundary = boundary 1797: end 1798: end
# File lib/mail/message.rb, line 1760 1760: def add_encoding_to_body 1761: if has_content_transfer_encoding? 1762: body.encoding = content_transfer_encoding 1763: end 1764: end
# File lib/mail/message.rb, line 1785 1785: def add_multipart_alternate_header 1786: header['content-type'] = ContentTypeField.with_boundary('multipart/alternative').value 1787: header['content_type'].parameters[:charset] = @charset 1788: body.boundary = boundary 1789: end
# File lib/mail/message.rb, line 1800 1800: def add_multipart_mixed_header 1801: unless header['content-type'] 1802: header['content-type'] = ContentTypeField.with_boundary('multipart/mixed').value 1803: header['content_type'].parameters[:charset] = @charset 1804: body.boundary = boundary 1805: end 1806: end
# File lib/mail/message.rb, line 1774 1774: def add_required_fields 1775: add_multipart_mixed_header unless !body.multipart? 1776: @body = Mail::Body.new('') if body.nil? 1777: add_message_id unless (has_message_id? || self.class == Mail::Part) 1778: add_date unless has_date? 1779: add_mime_version unless has_mime_version? 1780: add_content_type unless has_content_type? 1781: add_charset unless has_charset? 1782: add_content_transfer_encoding unless has_content_transfer_encoding? 1783: end
# File lib/mail/message.rb, line 1859 1859: def do_delivery 1860: begin 1861: if perform_deliveries 1862: delivery_method.deliver!(self) 1863: end 1864: rescue Exception => e # Net::SMTP errors or sendmail pipe errors 1865: raise e if raise_delivery_errors 1866: end 1867: end
Returns the filename of the attachment (if it exists) or returns nil
# File lib/mail/message.rb, line 1845 1845: def find_attachment 1846: case 1847: when content_type && header[:content_type].filename 1848: filename = header[:content_type].filename 1849: when content_disposition && header[:content_disposition].filename 1850: filename = header[:content_disposition].filename 1851: when content_location && header[:content_location].location 1852: filename = header[:content_location].location 1853: else 1854: filename = nil 1855: end 1856: filename 1857: end
# File lib/mail/message.rb, line 1766 1766: def identify_and_set_transfer_encoding 1767: if body && body.multipart? 1768: self.content_transfer_encoding = @transport_encoding 1769: else 1770: self.content_transfer_encoding = body.get_best_encoding(@transport_encoding) 1771: end 1772: end
# File lib/mail/message.rb, line 1808 1808: def init_with_hash(hash) 1809: passed_in_options = hash.with_indifferent_access 1810: self.raw_source = '' 1811: 1812: @header = Mail::Header.new 1813: @body = Mail::Body.new 1814: 1815: # We need to store the body until last, as we need all headers added first 1816: body_content = nil 1817: 1818: passed_in_options.each_pair do |k,v| 1819: k = underscoreize(k).to_sym if k.class == String 1820: if k == :headers 1821: self.headers(v) 1822: elsif k == :body 1823: body_content = v 1824: else 1825: self[k] = v 1826: end 1827: end 1828: 1829: if body_content 1830: self.body = body_content 1831: if has_content_transfer_encoding? 1832: body.encoding = content_transfer_encoding 1833: end 1834: end 1835: end
# File lib/mail/message.rb, line 1837 1837: def init_with_string(string) 1838: self.raw_source = string 1839: set_envelope_header 1840: parse_message 1841: separate_parts if multipart? 1842: 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 1739 1739: def parse_message 1740: header_part, body_part = raw_source.split(/#{CRLF}#{WSP}*#{CRLF}/, 2) 1741: self.header = header_part 1742: self.body = body_part 1743: end
# File lib/mail/message.rb, line 1745 1745: def raw_source=(value) 1746: @raw_source = value.to_crlf 1747: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.