Module: Mail

Defined in:
lib/mail.rb,
lib/mail/body.rb,
lib/mail/mail.rb,
lib/mail/part.rb,
lib/mail/field.rb,
lib/mail/header.rb,
lib/mail/message.rb,
lib/mail/version.rb,
lib/mail/envelope.rb,
lib/mail/patterns.rb,
lib/mail/utilities.rb,
lib/mail/attachment.rb,
lib/mail/field_list.rb,
lib/mail/configuration.rb,
lib/mail/fields/cc_field.rb,
lib/mail/fields/to_field.rb,
lib/mail/parsers/rfc2045.rb,
lib/mail/parsers/rfc2822.rb,
lib/mail/elements/address.rb,
lib/mail/encodings/base64.rb,
lib/mail/fields/bcc_field.rb,
lib/mail/parsers/received.rb,
lib/mail/fields/date_field.rb,
lib/mail/fields/from_field.rb,
lib/mail/parsers/date_time.rb,
lib/mail/encodings/encodings.rb,
lib/mail/fields/sender_field.rb,
lib/mail/network/deliverable.rb,
lib/mail/network/retrievable.rb,
lib/mail/parsers/message_ids.rb,
lib/mail/elements/phrase_list.rb,
lib/mail/fields/subject_field.rb,
lib/mail/parsers/content_type.rb,
lib/mail/parsers/mime_version.rb,
lib/mail/parsers/phrase_lists.rb,
lib/mail/elements/address_list.rb,
lib/mail/fields/comments_field.rb,
lib/mail/fields/keywords_field.rb,
lib/mail/fields/optional_field.rb,
lib/mail/fields/received_field.rb,
lib/mail/fields/reply_to_field.rb,
lib/mail/parsers/address_lists.rb,
lib/mail/parsers/envelope_from.rb,
lib/mail/fields/resent_cc_field.rb,
lib/mail/fields/resent_to_field.rb,
lib/mail/fields/content_id_field.rb,
lib/mail/fields/message_id_field.rb,
lib/mail/fields/references_field.rb,
lib/mail/fields/resent_bcc_field.rb,
lib/mail/fields/structured_field.rb,
lib/mail/fields/in_reply_to_field.rb,
lib/mail/fields/resent_date_field.rb,
lib/mail/fields/resent_from_field.rb,
lib/mail/fields/return_path_field.rb,
lib/mail/parsers/content_location.rb,
lib/mail/parsers/rfc2822_obsolete.rb,
lib/mail/elements/received_element.rb,
lib/mail/fields/common/common_date.rb,
lib/mail/fields/content_type_field.rb,
lib/mail/fields/mime_version_field.rb,
lib/mail/fields/unstructured_field.rb,
lib/mail/version_specific/ruby_1_8.rb,
lib/mail/version_specific/ruby_1_9.rb,
lib/mail/elements/date_time_element.rb,
lib/mail/encodings/quoted_printable.rb,
lib/mail/fields/common/common_field.rb,
lib/mail/fields/resent_sender_field.rb,
lib/mail/parsers/content_disposition.rb,
lib/mail/elements/message_ids_element.rb,
lib/mail/fields/common/common_address.rb,
lib/mail/fields/common/parameter_hash.rb,
lib/mail/elements/content_type_element.rb,
lib/mail/elements/mime_version_element.rb,
lib/mail/fields/content_location_field.rb,
lib/mail/network/delivery_methods/smtp.rb,
lib/mail/elements/envelope_from_element.rb,
lib/mail/fields/resent_message_id_field.rb,
lib/mail/network/retriever_methods/imap.rb,
lib/mail/network/retriever_methods/pop3.rb,
lib/mail/fields/common/common_message_id.rb,
lib/mail/fields/content_description_field.rb,
lib/mail/fields/content_disposition_field.rb,
lib/mail/elements/content_location_element.rb,
lib/mail/network/delivery_methods/sendmail.rb,
lib/mail/parsers/content_transfer_encoding.rb,
lib/mail/elements/content_disposition_element.rb,
lib/mail/network/delivery_methods/test_mailer.rb,
lib/mail/fields/content_transfer_encoding_field.rb,
lib/mail/network/delivery_methods/file_delivery.rb,
lib/mail/elements/content_transfer_encoding_element.rb

Defined Under Namespace

Modules: AddressLists, CommonAddress, CommonDate, CommonField, CommonMessageId, ContentDisposition, ContentLocation, ContentTransferEncoding, ContentType, DateTime, Deliverable, Encodings, EnvelopeFrom, MessageIds, MimeVersion, Patterns, PhraseLists, RFC2045, RFC2822, RFC2822Obsolete, Received, Retrievable, Utilities, VERSION Classes: Address, AddressList, AddressListsParser, Attachment, BccField, Body, CcField, CommentsField, Configuration, ContentDescriptionField, ContentDispositionElement, ContentDispositionField, ContentDispositionParser, ContentIdField, ContentLocationElement, ContentLocationField, ContentLocationParser, ContentTransferEncodingElement, ContentTransferEncodingField, ContentTransferEncodingParser, ContentTypeElement, ContentTypeField, ContentTypeParser, DateField, DateTimeElement, DateTimeParser, Envelope, EnvelopeFromElement, EnvelopeFromParser, Field, FieldList, FileDelivery, FromField, Header, IMAP, InReplyToField, KeywordsField, Message, MessageIdField, MessageIdsElement, MessageIdsParser, MimeVersionElement, MimeVersionField, MimeVersionParser, OptionalField, POP3, ParameterHash, Part, PhraseList, PhraseListsParser, RFC2045Parser, RFC2822ObsoleteParser, RFC2822Parser, ReceivedElement, ReceivedField, ReceivedParser, ReferencesField, ReplyToField, ResentBccField, ResentCcField, ResentDateField, ResentFromField, ResentMessageIdField, ResentSenderField, ResentToField, ReturnPathField, Ruby18, Ruby19, SMTP, SenderField, Sendmail, StructuredField, SubjectField, TestMailer, ToField, UnstructuredField

Constant Summary collapse

RubyVer =
Mail::Ruby18

Class Method Summary collapse

Class Method Details

.all(*args, &block) ⇒ Object

Receive all emails from a Pop3 server. See Mail::POP3 for a complete documentation.



140
141
142
# File 'lib/mail/mail.rb', line 140

def Mail.all(*args, &block)
  Retrievable.all(*args, &block)
end

.defaults(&block) ⇒ Object

Set the default configuration to send and receive emails. The defaults are global, allowing you to just call them once and use them everywhere. if port values are omitted from the SMTP and POP3 method calls, then it is assumed to use the default ports of 25 and 110 respectively.

You call defaults in a block, then you can set the basic host and port, by passing arguments, also, if the method you are using required more information (like pop3), then pass a block to it and call methods like user, pass, enable_tls etc.

The arguments and block are both optional.

Mail.defaults do
  smtp 'smtp.myhost.fr', 587
  pop3 'pop.myhost.fr' do
    user 'bernardo'
    pass 'mypass'
    enable_tls
  end
end

You will also want to specify a delivery and retriever type, the defaults are SMTP and POP3. You set the types by passing a symbol:

Mail.defaults do
  retriever_method :pop3
  delivery_method :smtp
end

The only implemented methods at the moment are :pop3, :smtp and :test

You can also specify your own delivery class which must respond to :deliver! or a retriever class which must respond to :get_messages

Mail.defaults do
  retriever_method MyOwnRetriever.new
end

Once you have set defaults, you can call Mail.deliver to send an email through the Mail.deliver method.



93
94
95
96
97
98
99
# File 'lib/mail/mail.rb', line 93

def Mail.defaults(&block)
  if block_given?
    Mail::Configuration.instance.defaults(&block)
  else
    Mail::Configuration.instance
  end
end

.deliver(*args, &block) ⇒ Object

Send an email using the default configuration. You do need to set a default configuration first before you use Mail.deliver, if you don’t, an appropriate error will be raised telling you to.

If you do not specify a delivery type, SMTP will be used.

Mail.deliver do
 to '[email protected]'
 from '[email protected]'
 subject 'This is a test email'
 body 'Not much to say here'
end

You can also do:

mail = Mail.read('email.eml')
mail.deliver!

And your email object will be created and sent.



120
121
122
123
124
# File 'lib/mail/mail.rb', line 120

def Mail.deliver(*args, &block)
  mail = Mail.new(args, &block)
  Deliverable.perform_delivery!(mail)
  mail
end

.deliveriesObject

Provides a store of all the emails sent



162
163
164
# File 'lib/mail/mail.rb', line 162

def Mail.deliveries
  @@deliveries ||= []
end

.find(*args, &block) ⇒ Object

Find emails in a Pop3 server. See Mail::POP3 for a complete documentation.



153
154
155
# File 'lib/mail/mail.rb', line 153

def Mail.find(*args, &block)
  Retrievable.find(*args, &block)
end

.first(*args, &block) ⇒ Object

Receive the first email(s) from a Pop3 server. See Mail::POP3 for a complete documentation.



128
129
130
# File 'lib/mail/mail.rb', line 128

def Mail.first(*args, &block)
  Retrievable.first(*args, &block)
end

.get_all_mail(*args, &block) ⇒ Object

Receive all emails from a Pop3 server. DEPRECATED: please use Mail.all instead.



146
147
148
149
# File 'lib/mail/mail.rb', line 146

def Mail.get_all_mail(*args, &block)
  warn "Mail.get_all_mail is deprecated. Please use Mail.all instead."
  all(*args, &block)
end

.last(*args, &block) ⇒ Object

Receive the first email(s) from a Pop3 server. See Mail::POP3 for a complete documentation.



134
135
136
# File 'lib/mail/mail.rb', line 134

def Mail.last(*args, &block)
  Retrievable.last(*args, &block)
end

.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\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'


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

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

.read(filename) ⇒ Object



157
158
159
# File 'lib/mail/mail.rb', line 157

def Mail.read(filename)
  Mail.new(File.read(filename))
end