Parent

Methods

Included Modules

Class Index [+]

Quicksearch

Mail::Message

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:

Per RFC2822

 2.1. General Description

  At the most basic level, a message is a series of characters.  A
  message that is conformant with this standard is comprised of
  characters with values in the range 1 through 127 and interpreted as
  US-ASCII characters [ASCII].  For brevity, this document sometimes
  refers to this range of characters as simply "US-ASCII characters".

  Note: This standard specifies that messages are made up of characters
  in the US-ASCII range of 1 through 127.  There are other documents,
  specifically the MIME document series [RFC2045, RFC2046, RFC2047,
  RFC2048, RFC2049], that extend this standard to allow for values
  outside of that range.  Discussion of those mechanisms is not within
  the scope of this standard.

  Messages are divided into lines of characters.  A line is a series of
  characters that is delimited with the two characters carriage-return
  and line-feed; that is, the carriage return (CR) character (ASCII
  value 13) followed immediately by the line feed (LF) character (ASCII
  value 10).  (The carriage-return/line-feed pair is usually written in
  this document as "CRLF".)

  A message consists of header fields (collectively called "the header
  of the message") followed, optionally, by a body.  The header is a
  sequence of lines of characters with special syntax as defined in
  this standard. The body is simply a sequence of characters that
  follows the header and is separated from the header by an empty line
  (i.e., a line with nothing preceding the CRLF).

Attributes

delivery_handler[RW]

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.

perform_deliveries[RW]

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

raise_delivery_errors[RW]

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

Public Class Methods

new(*args, &block) click to toggle source

Making an email

You can make an new mail object via a block, passing a string, file or direct assignment.

Making an email via a block

 mail = Mail.new do
      from '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@...

Making an email via passing a string

 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'

Making an email from a file

 mail = Mail.read('path/to/file.eml')
 mail.body.to_s #=> 'Hi there!'
 mail.subject   #=> 'Hello'
 mail.to        #=> 'mikel@test.lindsaar.net'

Making an email via assignment

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

Public Instance Methods

<=>(other) click to toggle source

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
==(other) click to toggle source

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
[](name) click to toggle source

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
[]=(name, value) click to toggle source

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
action() click to toggle source
      # File lib/mail/message.rb, line 1443
1443:     def action
1444:       delivery_status_part and delivery_status_part.action
1445:     end
add_charset() click to toggle source

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
add_content_transfer_encoding() click to toggle source

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
add_content_type() click to toggle source

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
add_date(date_val = '') click to toggle source

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
add_file(values) click to toggle source

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
add_message_id(msg_id_val = '') click to toggle source

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
add_mime_version(ver_val = '') click to toggle source

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
add_part(part) click to toggle source

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
all_parts() click to toggle source
      # File lib/mail/message.rb, line 1720
1720:     def all_parts
1721:       parts.map { |p| [p, p.all_parts] }.flatten
1722:     end
attachment() click to toggle source

Returns the attachment data if there is any

      # File lib/mail/message.rb, line 1711
1711:     def attachment
1712:       @attachment
1713:     end
attachment?() click to toggle source

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
attachments() click to toggle source

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
bcc( val = nil ) click to toggle source

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
bcc=( val ) click to toggle source

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
bcc_addrs() click to toggle source

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
body(value = nil) click to toggle source

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
body=(value) click to toggle source

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
body_encoding(value) click to toggle source
      # 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
body_encoding=(value) click to toggle source
      # File lib/mail/message.rb, line 1128
1128:     def body_encoding=(value)
1129:         body.encoding = value
1130:     end
bounced?() click to toggle source
      # File lib/mail/message.rb, line 1439
1439:     def bounced?
1440:       delivery_status_part and delivery_status_part.bounced?
1441:     end
boundary() click to toggle source

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
cc( val = nil ) click to toggle source

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
cc=( val ) click to toggle source

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
cc_addrs() click to toggle source

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
charset() click to toggle source

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
charset=(value) click to toggle source

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
comments( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 492
492:     def comments( val = nil )
493:       default :comments, val
494:     end
comments=( val ) click to toggle source
     # File lib/mail/message.rb, line 496
496:     def comments=( val )
497:       header[:comments] = val
498:     end
content_description( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 500
500:     def content_description( val = nil )
501:       default :content_description, val
502:     end
content_description=( val ) click to toggle source
     # File lib/mail/message.rb, line 504
504:     def content_description=( val )
505:       header[:content_description] = val
506:     end
content_disposition( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 508
508:     def content_disposition( val = nil )
509:       default :content_disposition, val
510:     end
content_disposition=( val ) click to toggle source
     # File lib/mail/message.rb, line 512
512:     def content_disposition=( val )
513:       header[:content_disposition] = val
514:     end
content_id( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 516
516:     def content_id( val = nil )
517:       default :content_id, val
518:     end
content_id=( val ) click to toggle source
     # File lib/mail/message.rb, line 520
520:     def content_id=( val )
521:       header[:content_id] = val
522:     end
content_location( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 524
524:     def content_location( val = nil )
525:       default :content_location, val
526:     end
content_location=( val ) click to toggle source
     # File lib/mail/message.rb, line 528
528:     def content_location=( val )
529:       header[:content_location] = val
530:     end
content_transfer_encoding( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 532
532:     def content_transfer_encoding( val = nil )
533:       default :content_transfer_encoding, val
534:     end
content_transfer_encoding=( val ) click to toggle source
     # File lib/mail/message.rb, line 536
536:     def content_transfer_encoding=( val )
537:       header[:content_transfer_encoding] = val
538:     end
content_type( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 540
540:     def content_type( val = nil )
541:       default :content_type, val
542:     end
content_type=( val ) click to toggle source
     # File lib/mail/message.rb, line 544
544:     def content_type=( val )
545:       header[:content_type] = val
546:     end
content_type_parameters() click to toggle source

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
convert_to_multipart() click to toggle source
      # 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
date( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 548
548:     def date( val = nil )
549:       default :date, val
550:     end
date=( val ) click to toggle source
     # File lib/mail/message.rb, line 552
552:     def date=( val )
553:       header[:date] = val
554:     end
decode_body() click to toggle source
      # File lib/mail/message.rb, line 1700
1700:     def decode_body
1701:       body.decoded
1702:     end
decoded() click to toggle source
      # 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
default( sym, val = nil ) click to toggle source

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
deliver() click to toggle source

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
deliver!() click to toggle source

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
delivery_method(method = nil, settings = {}) click to toggle source
     # 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
delivery_status_part() click to toggle source

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
delivery_status_report?() click to toggle source

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
destinations() click to toggle source

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
diagnostic_code() click to toggle source
      # File lib/mail/message.rb, line 1455
1455:     def diagnostic_code
1456:       delivery_status_part and delivery_status_part.diagnostic_code
1457:     end
encode!() click to toggle source
      # 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
encoded() click to toggle source

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
envelope_date() click to toggle source
     # File lib/mail/message.rb, line 353
353:     def envelope_date
354:       @envelope ? @envelope.date : nil
355:     end
envelope_from() click to toggle source
     # File lib/mail/message.rb, line 349
349:     def envelope_from
350:       @envelope ? @envelope.from : nil
351:     end
error_status() click to toggle source
      # File lib/mail/message.rb, line 1451
1451:     def error_status
1452:       delivery_status_part and delivery_status_part.error_status
1453:     end
errors() click to toggle source

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
filename() click to toggle source

Returns the filename of the attachment

      # File lib/mail/message.rb, line 1716
1716:     def filename
1717:       find_attachment
1718:     end
final_recipient() click to toggle source
      # File lib/mail/message.rb, line 1447
1447:     def final_recipient
1448:       delivery_status_part and delivery_status_part.final_recipient
1449:     end
find_first_mime_type(mt) click to toggle source
      # 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
from( val = nil ) click to toggle source

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
from=( val ) click to toggle source

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
from_addrs() click to toggle source

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
has_attachments?() click to toggle source
      # File lib/mail/message.rb, line 1515
1515:     def has_attachments?
1516:       !attachments.empty?
1517:     end
has_charset?() click to toggle source
      # 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
has_content_transfer_encoding?() click to toggle source
      # 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
has_content_type?() click to toggle source
      # 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
has_date?() click to toggle source

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
has_message_id?() click to toggle source

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
has_mime_version?() click to toggle source

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
header(value = nil) click to toggle source

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
header=(value) click to toggle source

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
header_fields() click to toggle source

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
headers(hash = {}) click to toggle source

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
html_part(&block) click to toggle source

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
html_part=(msg = nil) click to toggle source

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
in_reply_to( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 609
609:     def in_reply_to( val = nil )
610:       default :in_reply_to, val
611:     end
in_reply_to=( val ) click to toggle source
     # File lib/mail/message.rb, line 613
613:     def in_reply_to=( val )
614:       header[:in_reply_to] = val
615:     end
inform_interceptors() click to toggle source
     # File lib/mail/message.rb, line 215
215:     def inform_interceptors
216:       Mail.inform_interceptors(self)
217:     end
inform_observers() click to toggle source
     # File lib/mail/message.rb, line 211
211:     def inform_observers
212:       Mail.inform_observers(self)
213:     end
inspect() click to toggle source
      # File lib/mail/message.rb, line 1677
1677:     def inspect
1678:       "#<#{self.class}:#{self.object_id}, Multipart: #{multipart?}, Headers: #{header.field_summary}>"
1679:     end
is_marked_for_delete?() click to toggle source

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
keywords( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 617
617:     def keywords( val = nil )
618:       default :keywords, val
619:     end
keywords=( val ) click to toggle source
     # File lib/mail/message.rb, line 621
621:     def keywords=( val )
622:       header[:keywords] = val
623:     end
main_type() click to toggle source

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
mark_for_delete=(value = true) click to toggle source

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
message_content_type() click to toggle source
      # 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
message_id( val = nil ) click to toggle source

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
message_id=( val ) click to toggle source

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(name, *args, &block) click to toggle source

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
mime_parameters() click to toggle source

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
mime_type() click to toggle source

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
mime_version( val = nil ) click to toggle source

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
mime_version=( val ) click to toggle source

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
multipart?() click to toggle source

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
multipart_report?() click to toggle source

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
part(params = {}) click to toggle source

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
parts() click to toggle source

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
raw_envelope() click to toggle source

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
raw_source() click to toggle source

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
read() click to toggle source
      # 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
ready_to_send!() click to toggle source

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
received( val = nil ) click to toggle source
     # 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
received=( val ) click to toggle source
     # File lib/mail/message.rb, line 686
686:     def received=( val )
687:       header[:received] = val
688:     end
references( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 690
690:     def references( val = nil )
691:       default :references, val
692:     end
references=( val ) click to toggle source
     # File lib/mail/message.rb, line 694
694:     def references=( val )
695:       header[:references] = val
696:     end
register_for_delivery_notification(observer) click to toggle source
     # 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
remote_mta() click to toggle source
      # File lib/mail/message.rb, line 1459
1459:     def remote_mta
1460:       delivery_status_part and delivery_status_part.remote_mta
1461:     end
reply_to( val = nil ) click to toggle source

Returns the Reply-To value of the mail object as an array of strings of address specs.

Example:

 mail.reply_to = 'Mikel <mikel@test.lindsaar.net>'
 mail.reply_to #=> ['mikel@test.lindsaar.net']
 mail.reply_to = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net'
 mail.reply_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.reply_to 'Mikel <mikel@test.lindsaar.net>'
 mail.reply_to #=> ['mikel@test.lindsaar.net']

Additionally, you can append new addresses to the returned Array like object.

Example:

 mail.reply_to 'Mikel <mikel@test.lindsaar.net>'
 mail.reply_to << 'ada@test.lindsaar.net'
 mail.reply_to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
     # File lib/mail/message.rb, line 723
723:     def reply_to( val = nil )
724:       default :reply_to, val
725:     end
reply_to=( val ) click to toggle source

Sets the Reply-To value of the mail object, pass in a string of the field

Example:

 mail.reply_to = 'Mikel <mikel@test.lindsaar.net>'
 mail.reply_to #=> ['mikel@test.lindsaar.net']
 mail.reply_to = 'Mikel <mikel@test.lindsaar.net>, ada@test.lindsaar.net'
 mail.reply_to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
     # File lib/mail/message.rb, line 735
735:     def reply_to=( val )
736:       header[:reply_to] = val
737:     end
resent_bcc( val = nil ) click to toggle source

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
resent_bcc=( val ) click to toggle source

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
resent_cc( val = nil ) click to toggle source

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
resent_cc=( val ) click to toggle source

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
resent_date( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 821
821:     def resent_date( val = nil )
822:       default :resent_date, val
823:     end
resent_date=( val ) click to toggle source
     # File lib/mail/message.rb, line 825
825:     def resent_date=( val )
826:       header[:resent_date] = val
827:     end
resent_from( val = nil ) click to toggle source

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
resent_from=( val ) click to toggle source

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
resent_message_id( val = nil ) click to toggle source
     # File lib/mail/message.rb, line 870
870:     def resent_message_id( val = nil )
871:       default :resent_message_id, val
872:     end
resent_message_id=( val ) click to toggle source
     # File lib/mail/message.rb, line 874
874:     def resent_message_id=( val )
875:       header[:resent_message_id] = val
876:     end
resent_sender( val = nil ) click to toggle source

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
resent_sender=( val ) click to toggle source

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
resent_to( val = nil ) click to toggle source

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
resent_to=( val ) click to toggle source

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
retryable?() click to toggle source
      # File lib/mail/message.rb, line 1463
1463:     def retryable?
1464:       delivery_status_part and delivery_status_part.retryable?
1465:     end
return_path( val = nil ) click to toggle source

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
return_path=( val ) click to toggle source

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
sender( val = nil ) click to toggle source

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
sender=( val ) click to toggle source

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
set_envelope( val ) click to toggle source

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
skip_deletion() click to toggle source

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
sub_type() click to toggle source

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
subject( val = nil ) click to toggle source

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
subject=( val ) click to toggle source

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
text_part(&block) click to toggle source

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
text_part=(msg = nil) click to toggle source

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
to( val = nil ) click to toggle source

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
to=( val ) click to toggle source

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
to_addrs() click to toggle source

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
to_s() click to toggle source
      # File lib/mail/message.rb, line 1673
1673:     def to_s
1674:       encoded
1675:     end
transport_encoding( val = nil) click to toggle source
     # File lib/mail/message.rb, line 556
556:     def transport_encoding( val = nil)
557:       if val
558:         self.transport_encoding = val
559:       else
560:         @transport_encoding
561:       end
562:     end
transport_encoding=( val ) click to toggle source
     # File lib/mail/message.rb, line 564
564:     def transport_encoding=( val )
565:       @transport_encoding = Mail::Encodings.get_encoding(val)
566:     end

Private Instance Methods

add_boundary() click to toggle source
      # 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
add_encoding_to_body() click to toggle source
      # 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
add_multipart_alternate_header() click to toggle source
      # 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
add_multipart_mixed_header() click to toggle source
      # 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
add_required_fields() click to toggle source
      # 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
body_lazy(value, index) click to toggle source

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
do_delivery() click to toggle source
      # 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
find_attachment() click to toggle source

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
identify_and_set_transfer_encoding() click to toggle source
      # 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
init_with_hash(hash) click to toggle source
      # 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
init_with_string(string) click to toggle source
      # 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
parse_message() click to toggle source
 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
process_body_raw() click to toggle source
      # 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
raw_source=(value) click to toggle source
      # File lib/mail/message.rb, line 1778
1778:     def raw_source=(value)
1779:       @raw_source = value.to_crlf
1780:     end
separate_parts() click to toggle source
      # File lib/mail/message.rb, line 1818
1818:     def separate_parts
1819:       body.split!(boundary)
1820:     end
set_envelope_header() click to toggle source
      # File lib/mail/message.rb, line 1811
1811:     def set_envelope_header
1812:       if match_data = raw_source.to_s.match(/\AFrom\s(#{TEXT}+)#{CRLF}(.*)/)
1813:         set_envelope(match_data[1])
1814:         self.raw_source = match_data[2]
1815:       end
1816:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.