Method: Mail.new

Defined in:
lib/mail/mail.rb

.new(*args, &block) ⇒ Object

Allows you to create a new Mail::Message object.

You can make an email via passing a string or passing a block.

For example, the following two examples will create the same email message:

Creating via a string:

string = "To: [email protected]\r\n"
string << "From: [email protected]\r\n"
string << "Subject: This is an email\r\n"
string << "\r\n"
string << "This is the body"
Mail.new(string)

Or creating via a block:

message = Mail.new do
  to '[email protected]'
  from '[email protected]'
  subject 'This is an email'
  body 'This is the body'
end

Or creating via a hash (or hash like object):

message = Mail.new({:to => '[email protected]',
                    'from' => '[email protected]',
                    :subject => 'This is an email',
                    :body => 'This is the body' })

Note, the hash keys can be strings or symbols, the passed in object does not need to be a hash, it just needs to respond to :each_pair and yield each key value pair.

As a side note, you can also create a new email through creating a Mail::Message object directly and then passing in values via string, symbol or direct method calls. See Mail::Message for more information.

mail = Mail.new
mail.to = '[email protected]'
mail[:from] = '[email protected]'
mail['subject'] = 'This is an email'
mail.body = 'This is the body'


50
51
52
# File 'lib/mail/mail.rb', line 50

def self.new(*args, &block)
  Message.new(args, &block)
end