mail-portertech 2.6.2.edge
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.rdoc +753 -0
- data/CONTRIBUTING.md +60 -0
- data/Dependencies.txt +2 -0
- data/Gemfile +15 -0
- data/MIT-LICENSE +20 -0
- data/README.md +683 -0
- data/Rakefile +29 -0
- data/TODO.rdoc +9 -0
- data/lib/mail.rb +91 -0
- data/lib/mail/attachments_list.rb +104 -0
- data/lib/mail/body.rb +291 -0
- data/lib/mail/check_delivery_params.rb +20 -0
- data/lib/mail/configuration.rb +75 -0
- data/lib/mail/core_extensions/nil.rb +19 -0
- data/lib/mail/core_extensions/object.rb +13 -0
- data/lib/mail/core_extensions/smtp.rb +24 -0
- data/lib/mail/core_extensions/string.rb +43 -0
- data/lib/mail/core_extensions/string/access.rb +145 -0
- data/lib/mail/core_extensions/string/multibyte.rb +78 -0
- data/lib/mail/elements.rb +14 -0
- data/lib/mail/elements/address.rb +270 -0
- data/lib/mail/elements/address_list.rb +51 -0
- data/lib/mail/elements/content_disposition_element.rb +26 -0
- data/lib/mail/elements/content_location_element.rb +21 -0
- data/lib/mail/elements/content_transfer_encoding_element.rb +17 -0
- data/lib/mail/elements/content_type_element.rb +31 -0
- data/lib/mail/elements/date_time_element.rb +22 -0
- data/lib/mail/elements/envelope_from_element.rb +39 -0
- data/lib/mail/elements/message_ids_element.rb +24 -0
- data/lib/mail/elements/mime_version_element.rb +22 -0
- data/lib/mail/elements/phrase_list.rb +16 -0
- data/lib/mail/elements/received_element.rb +26 -0
- data/lib/mail/encodings.rb +304 -0
- data/lib/mail/encodings/7bit.rb +31 -0
- data/lib/mail/encodings/8bit.rb +31 -0
- data/lib/mail/encodings/base64.rb +33 -0
- data/lib/mail/encodings/binary.rb +31 -0
- data/lib/mail/encodings/quoted_printable.rb +39 -0
- data/lib/mail/encodings/transfer_encoding.rb +58 -0
- data/lib/mail/envelope.rb +30 -0
- data/lib/mail/field.rb +247 -0
- data/lib/mail/field_list.rb +33 -0
- data/lib/mail/fields.rb +35 -0
- data/lib/mail/fields/bcc_field.rb +56 -0
- data/lib/mail/fields/cc_field.rb +55 -0
- data/lib/mail/fields/comments_field.rb +41 -0
- data/lib/mail/fields/common/address_container.rb +16 -0
- data/lib/mail/fields/common/common_address.rb +135 -0
- data/lib/mail/fields/common/common_date.rb +35 -0
- data/lib/mail/fields/common/common_field.rb +57 -0
- data/lib/mail/fields/common/common_message_id.rb +48 -0
- data/lib/mail/fields/common/parameter_hash.rb +58 -0
- data/lib/mail/fields/content_description_field.rb +19 -0
- data/lib/mail/fields/content_disposition_field.rb +70 -0
- data/lib/mail/fields/content_id_field.rb +62 -0
- data/lib/mail/fields/content_location_field.rb +42 -0
- data/lib/mail/fields/content_transfer_encoding_field.rb +44 -0
- data/lib/mail/fields/content_type_field.rb +201 -0
- data/lib/mail/fields/date_field.rb +57 -0
- data/lib/mail/fields/from_field.rb +55 -0
- data/lib/mail/fields/in_reply_to_field.rb +56 -0
- data/lib/mail/fields/keywords_field.rb +44 -0
- data/lib/mail/fields/message_id_field.rb +82 -0
- data/lib/mail/fields/mime_version_field.rb +53 -0
- data/lib/mail/fields/optional_field.rb +13 -0
- data/lib/mail/fields/received_field.rb +75 -0
- data/lib/mail/fields/references_field.rb +56 -0
- data/lib/mail/fields/reply_to_field.rb +55 -0
- data/lib/mail/fields/resent_bcc_field.rb +55 -0
- data/lib/mail/fields/resent_cc_field.rb +55 -0
- data/lib/mail/fields/resent_date_field.rb +35 -0
- data/lib/mail/fields/resent_from_field.rb +55 -0
- data/lib/mail/fields/resent_message_id_field.rb +34 -0
- data/lib/mail/fields/resent_sender_field.rb +62 -0
- data/lib/mail/fields/resent_to_field.rb +55 -0
- data/lib/mail/fields/return_path_field.rb +65 -0
- data/lib/mail/fields/sender_field.rb +67 -0
- data/lib/mail/fields/structured_field.rb +51 -0
- data/lib/mail/fields/subject_field.rb +16 -0
- data/lib/mail/fields/to_field.rb +55 -0
- data/lib/mail/fields/unstructured_field.rb +204 -0
- data/lib/mail/header.rb +274 -0
- data/lib/mail/indifferent_hash.rb +146 -0
- data/lib/mail/mail.rb +267 -0
- data/lib/mail/matchers/has_sent_mail.rb +157 -0
- data/lib/mail/message.rb +2160 -0
- data/lib/mail/multibyte.rb +42 -0
- data/lib/mail/multibyte/chars.rb +474 -0
- data/lib/mail/multibyte/exceptions.rb +8 -0
- data/lib/mail/multibyte/unicode.rb +400 -0
- data/lib/mail/multibyte/utils.rb +60 -0
- data/lib/mail/network.rb +14 -0
- data/lib/mail/network/delivery_methods/exim.rb +52 -0
- data/lib/mail/network/delivery_methods/file_delivery.rb +45 -0
- data/lib/mail/network/delivery_methods/sendmail.rb +89 -0
- data/lib/mail/network/delivery_methods/smtp.rb +142 -0
- data/lib/mail/network/delivery_methods/smtp_connection.rb +61 -0
- data/lib/mail/network/delivery_methods/test_mailer.rb +44 -0
- data/lib/mail/network/retriever_methods/base.rb +63 -0
- data/lib/mail/network/retriever_methods/imap.rb +173 -0
- data/lib/mail/network/retriever_methods/pop3.rb +140 -0
- data/lib/mail/network/retriever_methods/test_retriever.rb +43 -0
- data/lib/mail/parsers.rb +26 -0
- data/lib/mail/parsers/address_lists_parser.rb +132 -0
- data/lib/mail/parsers/content_disposition_parser.rb +67 -0
- data/lib/mail/parsers/content_location_parser.rb +35 -0
- data/lib/mail/parsers/content_transfer_encoding_parser.rb +33 -0
- data/lib/mail/parsers/content_type_parser.rb +64 -0
- data/lib/mail/parsers/date_time_parser.rb +36 -0
- data/lib/mail/parsers/envelope_from_parser.rb +45 -0
- data/lib/mail/parsers/message_ids_parser.rb +39 -0
- data/lib/mail/parsers/mime_version_parser.rb +41 -0
- data/lib/mail/parsers/phrase_lists_parser.rb +33 -0
- data/lib/mail/parsers/ragel.rb +17 -0
- data/lib/mail/parsers/ragel/common.rl +184 -0
- data/lib/mail/parsers/ragel/date_time.rl +30 -0
- data/lib/mail/parsers/ragel/parser_info.rb +61 -0
- data/lib/mail/parsers/ragel/ruby.rb +39 -0
- data/lib/mail/parsers/ragel/ruby/machines/address_lists_machine.rb +14864 -0
- data/lib/mail/parsers/ragel/ruby/machines/address_lists_machine.rb.rl +37 -0
- data/lib/mail/parsers/ragel/ruby/machines/content_disposition_machine.rb +751 -0
- data/lib/mail/parsers/ragel/ruby/machines/content_disposition_machine.rb.rl +37 -0
- data/lib/mail/parsers/ragel/ruby/machines/content_location_machine.rb +614 -0
- data/lib/mail/parsers/ragel/ruby/machines/content_location_machine.rb.rl +37 -0
- data/lib/mail/parsers/ragel/ruby/machines/content_transfer_encoding_machine.rb +447 -0
- data/lib/mail/parsers/ragel/ruby/machines/content_transfer_encoding_machine.rb.rl +37 -0
- data/lib/mail/parsers/ragel/ruby/machines/content_type_machine.rb +825 -0
- data/lib/mail/parsers/ragel/ruby/machines/content_type_machine.rb.rl +37 -0
- data/lib/mail/parsers/ragel/ruby/machines/date_time_machine.rb +817 -0
- data/lib/mail/parsers/ragel/ruby/machines/date_time_machine.rb.rl +37 -0
- data/lib/mail/parsers/ragel/ruby/machines/envelope_from_machine.rb +2129 -0
- data/lib/mail/parsers/ragel/ruby/machines/envelope_from_machine.rb.rl +37 -0
- data/lib/mail/parsers/ragel/ruby/machines/message_ids_machine.rb +1570 -0
- data/lib/mail/parsers/ragel/ruby/machines/message_ids_machine.rb.rl +37 -0
- data/lib/mail/parsers/ragel/ruby/machines/mime_version_machine.rb +440 -0
- data/lib/mail/parsers/ragel/ruby/machines/mime_version_machine.rb.rl +37 -0
- data/lib/mail/parsers/ragel/ruby/machines/phrase_lists_machine.rb +564 -0
- data/lib/mail/parsers/ragel/ruby/machines/phrase_lists_machine.rb.rl +37 -0
- data/lib/mail/parsers/ragel/ruby/machines/rb_actions.rl +51 -0
- data/lib/mail/parsers/ragel/ruby/machines/received_machine.rb +5144 -0
- data/lib/mail/parsers/ragel/ruby/machines/received_machine.rb.rl +37 -0
- data/lib/mail/parsers/ragel/ruby/parser.rb.rl.erb +37 -0
- data/lib/mail/parsers/received_parser.rb +47 -0
- data/lib/mail/part.rb +120 -0
- data/lib/mail/parts_list.rb +57 -0
- data/lib/mail/patterns.rb +37 -0
- data/lib/mail/utilities.rb +225 -0
- data/lib/mail/values/unicode_tables.dat +0 -0
- data/lib/mail/version.rb +4 -0
- data/lib/mail/version_specific/ruby_1_8.rb +119 -0
- data/lib/mail/version_specific/ruby_1_9.rb +159 -0
- metadata +276 -0
data/lib/mail/network.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'mail/network/retriever_methods/base'
|
2
|
+
|
3
|
+
module Mail
|
4
|
+
register_autoload :SMTP, 'mail/network/delivery_methods/smtp'
|
5
|
+
register_autoload :FileDelivery, 'mail/network/delivery_methods/file_delivery'
|
6
|
+
register_autoload :Sendmail, 'mail/network/delivery_methods/sendmail'
|
7
|
+
register_autoload :Exim, 'mail/network/delivery_methods/exim'
|
8
|
+
register_autoload :SMTPConnection, 'mail/network/delivery_methods/smtp_connection'
|
9
|
+
register_autoload :TestMailer, 'mail/network/delivery_methods/test_mailer'
|
10
|
+
|
11
|
+
register_autoload :POP3, 'mail/network/retriever_methods/pop3'
|
12
|
+
register_autoload :IMAP, 'mail/network/retriever_methods/imap'
|
13
|
+
register_autoload :TestRetriever, 'mail/network/retriever_methods/test_retriever'
|
14
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Mail
|
2
|
+
|
3
|
+
# A delivery method implementation which sends via exim.
|
4
|
+
#
|
5
|
+
# To use this, first find out where the exim binary is on your computer,
|
6
|
+
# if you are on a mac or unix box, it is usually in /usr/sbin/exim, this will
|
7
|
+
# be your exim location.
|
8
|
+
#
|
9
|
+
# Mail.defaults do
|
10
|
+
# delivery_method :exim
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# Or if your exim binary is not at '/usr/sbin/exim'
|
14
|
+
#
|
15
|
+
# Mail.defaults do
|
16
|
+
# delivery_method :exim, :location => '/absolute/path/to/your/exim'
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# Then just deliver the email as normal:
|
20
|
+
#
|
21
|
+
# Mail.deliver do
|
22
|
+
# to 'mikel@test.lindsaar.net'
|
23
|
+
# from 'ada@test.lindsaar.net'
|
24
|
+
# subject 'testing exim'
|
25
|
+
# body 'testing exim'
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# Or by calling deliver on a Mail message
|
29
|
+
#
|
30
|
+
# mail = Mail.new do
|
31
|
+
# to 'mikel@test.lindsaar.net'
|
32
|
+
# from 'ada@test.lindsaar.net'
|
33
|
+
# subject 'testing exim'
|
34
|
+
# body 'testing exim'
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# mail.deliver!
|
38
|
+
class Exim < Sendmail
|
39
|
+
def initialize(values)
|
40
|
+
self.settings = { :location => '/usr/sbin/exim',
|
41
|
+
:arguments => '-i -t' }.merge(values)
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.call(path, arguments, destinations, mail)
|
45
|
+
popen "#{path} #{arguments}" do |io|
|
46
|
+
io.puts mail.encoded.to_lf
|
47
|
+
io.flush
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'mail/check_delivery_params'
|
2
|
+
|
3
|
+
module Mail
|
4
|
+
|
5
|
+
# FileDelivery class delivers emails into multiple files based on the destination
|
6
|
+
# address. Each file is appended to if it already exists.
|
7
|
+
#
|
8
|
+
# So if you have an email going to fred@test, bob@test, joe@anothertest, and you
|
9
|
+
# set your location path to /path/to/mails then FileDelivery will create the directory
|
10
|
+
# if it does not exist, and put one copy of the email in three files, called
|
11
|
+
# by their message id
|
12
|
+
#
|
13
|
+
# Make sure the path you specify with :location is writable by the Ruby process
|
14
|
+
# running Mail.
|
15
|
+
class FileDelivery
|
16
|
+
include Mail::CheckDeliveryParams
|
17
|
+
|
18
|
+
if RUBY_VERSION >= '1.9.1'
|
19
|
+
require 'fileutils'
|
20
|
+
else
|
21
|
+
require 'ftools'
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(values)
|
25
|
+
self.settings = { :location => './mails' }.merge!(values)
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_accessor :settings
|
29
|
+
|
30
|
+
def deliver!(mail)
|
31
|
+
check_delivery_params(mail)
|
32
|
+
|
33
|
+
if ::File.respond_to?(:makedirs)
|
34
|
+
::File.makedirs settings[:location]
|
35
|
+
else
|
36
|
+
::FileUtils.mkdir_p settings[:location]
|
37
|
+
end
|
38
|
+
|
39
|
+
mail.destinations.uniq.each do |to|
|
40
|
+
::File.open(::File.join(settings[:location], File.basename(to.to_s)), 'a') { |f| "#{f.write(mail.encoded)}\r\n\r\n" }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'mail/check_delivery_params'
|
2
|
+
|
3
|
+
module Mail
|
4
|
+
# A delivery method implementation which sends via sendmail.
|
5
|
+
#
|
6
|
+
# To use this, first find out where the sendmail binary is on your computer,
|
7
|
+
# if you are on a mac or unix box, it is usually in /usr/sbin/sendmail, this will
|
8
|
+
# be your sendmail location.
|
9
|
+
#
|
10
|
+
# Mail.defaults do
|
11
|
+
# delivery_method :sendmail
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# Or if your sendmail binary is not at '/usr/sbin/sendmail'
|
15
|
+
#
|
16
|
+
# Mail.defaults do
|
17
|
+
# delivery_method :sendmail, :location => '/absolute/path/to/your/sendmail'
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# Then just deliver the email as normal:
|
21
|
+
#
|
22
|
+
# Mail.deliver do
|
23
|
+
# to 'mikel@test.lindsaar.net'
|
24
|
+
# from 'ada@test.lindsaar.net'
|
25
|
+
# subject 'testing sendmail'
|
26
|
+
# body 'testing sendmail'
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# Or by calling deliver on a Mail message
|
30
|
+
#
|
31
|
+
# mail = Mail.new do
|
32
|
+
# to 'mikel@test.lindsaar.net'
|
33
|
+
# from 'ada@test.lindsaar.net'
|
34
|
+
# subject 'testing sendmail'
|
35
|
+
# body 'testing sendmail'
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# mail.deliver!
|
39
|
+
class Sendmail
|
40
|
+
include Mail::CheckDeliveryParams
|
41
|
+
|
42
|
+
def initialize(values)
|
43
|
+
self.settings = { :location => '/usr/sbin/sendmail',
|
44
|
+
:arguments => '-i' }.merge(values)
|
45
|
+
end
|
46
|
+
|
47
|
+
attr_accessor :settings
|
48
|
+
|
49
|
+
def deliver!(mail)
|
50
|
+
smtp_from, smtp_to, message = check_delivery_params(mail)
|
51
|
+
|
52
|
+
from = "-f #{self.class.shellquote(smtp_from)}"
|
53
|
+
to = smtp_to.map { |_to| self.class.shellquote(_to) }.join(' ')
|
54
|
+
|
55
|
+
arguments = "#{settings[:arguments]} #{from} --"
|
56
|
+
self.class.call(settings[:location], arguments, to, message)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.call(path, arguments, destinations, encoded_message)
|
60
|
+
popen "#{path} #{arguments} #{destinations}" do |io|
|
61
|
+
io.puts encoded_message.to_lf
|
62
|
+
io.flush
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
if RUBY_VERSION < '1.9.0'
|
67
|
+
def self.popen(command, &block)
|
68
|
+
IO.popen "#{command} 2>&1", 'w+', &block
|
69
|
+
end
|
70
|
+
else
|
71
|
+
def self.popen(command, &block)
|
72
|
+
IO.popen command, 'w+', :err => :out, &block
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# The following is an adaptation of ruby 1.9.2's shellwords.rb file,
|
77
|
+
# it is modified to include '+' in the allowed list to allow for
|
78
|
+
# sendmail to accept email addresses as the sender with a + in them.
|
79
|
+
def self.shellquote(address)
|
80
|
+
# Process as a single byte sequence because not all shell
|
81
|
+
# implementations are multibyte aware.
|
82
|
+
#
|
83
|
+
# A LF cannot be escaped with a backslash because a backslash + LF
|
84
|
+
# combo is regarded as line continuation and simply ignored. Strip it.
|
85
|
+
escaped = address.gsub(/([^A-Za-z0-9_\s\+\-.,:\/@])/n, "\\\\\\1").gsub("\n", '')
|
86
|
+
%("#{escaped}")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'mail/check_delivery_params'
|
2
|
+
|
3
|
+
module Mail
|
4
|
+
# == Sending Email with SMTP
|
5
|
+
#
|
6
|
+
# Mail allows you to send emails using SMTP. This is done by wrapping Net::SMTP in
|
7
|
+
# an easy to use manner.
|
8
|
+
#
|
9
|
+
# === Sending via SMTP server on Localhost
|
10
|
+
#
|
11
|
+
# Sending locally (to a postfix or sendmail server running on localhost) requires
|
12
|
+
# no special setup. Just to Mail.deliver &block or message.deliver! and it will
|
13
|
+
# be sent in this method.
|
14
|
+
#
|
15
|
+
# === Sending via MobileMe
|
16
|
+
#
|
17
|
+
# Mail.defaults do
|
18
|
+
# delivery_method :smtp, { :address => "smtp.me.com",
|
19
|
+
# :port => 587,
|
20
|
+
# :domain => 'your.host.name',
|
21
|
+
# :user_name => '<username>',
|
22
|
+
# :password => '<password>',
|
23
|
+
# :authentication => 'plain',
|
24
|
+
# :enable_starttls_auto => true }
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# === Sending via GMail
|
28
|
+
#
|
29
|
+
# Mail.defaults do
|
30
|
+
# delivery_method :smtp, { :address => "smtp.gmail.com",
|
31
|
+
# :port => 587,
|
32
|
+
# :domain => 'your.host.name',
|
33
|
+
# :user_name => '<username>',
|
34
|
+
# :password => '<password>',
|
35
|
+
# :authentication => 'plain',
|
36
|
+
# :enable_starttls_auto => true }
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# === Certificate verification
|
40
|
+
#
|
41
|
+
# When using TLS, some mail servers provide certificates that are self-signed
|
42
|
+
# or whose names do not exactly match the hostname given in the address.
|
43
|
+
# OpenSSL will reject these by default. The best remedy is to use the correct
|
44
|
+
# hostname or update the certificate authorities trusted by your ruby. If
|
45
|
+
# that isn't possible, you can control this behavior with
|
46
|
+
# an :openssl_verify_mode setting. Its value may be either an OpenSSL
|
47
|
+
# verify mode constant (OpenSSL::SSL::VERIFY_NONE), or a string containing
|
48
|
+
# the name of an OpenSSL verify mode (none, peer, client_once,
|
49
|
+
# fail_if_no_peer_cert).
|
50
|
+
#
|
51
|
+
# === Others
|
52
|
+
#
|
53
|
+
# Feel free to send me other examples that were tricky
|
54
|
+
#
|
55
|
+
# === Delivering the email
|
56
|
+
#
|
57
|
+
# Once you have the settings right, sending the email is done by:
|
58
|
+
#
|
59
|
+
# Mail.deliver do
|
60
|
+
# to 'mikel@test.lindsaar.net'
|
61
|
+
# from 'ada@test.lindsaar.net'
|
62
|
+
# subject 'testing sendmail'
|
63
|
+
# body 'testing sendmail'
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# Or by calling deliver on a Mail message
|
67
|
+
#
|
68
|
+
# mail = Mail.new do
|
69
|
+
# to 'mikel@test.lindsaar.net'
|
70
|
+
# from 'ada@test.lindsaar.net'
|
71
|
+
# subject 'testing sendmail'
|
72
|
+
# body 'testing sendmail'
|
73
|
+
# end
|
74
|
+
#
|
75
|
+
# mail.deliver!
|
76
|
+
class SMTP
|
77
|
+
include Mail::CheckDeliveryParams
|
78
|
+
|
79
|
+
def initialize(values)
|
80
|
+
self.settings = { :address => "localhost",
|
81
|
+
:port => 25,
|
82
|
+
:domain => 'localhost.localdomain',
|
83
|
+
:user_name => nil,
|
84
|
+
:password => nil,
|
85
|
+
:authentication => nil,
|
86
|
+
:enable_starttls_auto => true,
|
87
|
+
:openssl_verify_mode => nil,
|
88
|
+
:ssl => nil,
|
89
|
+
:tls => nil
|
90
|
+
}.merge!(values)
|
91
|
+
end
|
92
|
+
|
93
|
+
attr_accessor :settings
|
94
|
+
|
95
|
+
# Send the message via SMTP.
|
96
|
+
# The from and to attributes are optional. If not set, they are retrieve from the Message.
|
97
|
+
def deliver!(mail)
|
98
|
+
smtp_from, smtp_to, message = check_delivery_params(mail)
|
99
|
+
|
100
|
+
smtp = Net::SMTP.new(settings[:address], settings[:port])
|
101
|
+
if settings[:tls] || settings[:ssl]
|
102
|
+
if smtp.respond_to?(:enable_tls)
|
103
|
+
smtp.enable_tls(ssl_context)
|
104
|
+
end
|
105
|
+
elsif settings[:enable_starttls_auto]
|
106
|
+
if smtp.respond_to?(:enable_starttls_auto)
|
107
|
+
smtp.enable_starttls_auto(ssl_context)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
response = nil
|
112
|
+
smtp.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication]) do |smtp_obj|
|
113
|
+
response = smtp_obj.sendmail(message, smtp_from, smtp_to)
|
114
|
+
end
|
115
|
+
|
116
|
+
if settings[:return_response]
|
117
|
+
response
|
118
|
+
else
|
119
|
+
self
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
# Allow SSL context to be configured via settings, for Ruby >= 1.9
|
127
|
+
# Just returns openssl verify mode for Ruby 1.8.x
|
128
|
+
def ssl_context
|
129
|
+
openssl_verify_mode = settings[:openssl_verify_mode]
|
130
|
+
|
131
|
+
if openssl_verify_mode.kind_of?(String)
|
132
|
+
openssl_verify_mode = "OpenSSL::SSL::VERIFY_#{openssl_verify_mode.upcase}".constantize
|
133
|
+
end
|
134
|
+
|
135
|
+
context = Net::SMTP.default_ssl_context
|
136
|
+
context.verify_mode = openssl_verify_mode
|
137
|
+
context.ca_path = settings[:ca_path] if settings[:ca_path]
|
138
|
+
context.ca_file = settings[:ca_file] if settings[:ca_file]
|
139
|
+
context
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'mail/check_delivery_params'
|
2
|
+
|
3
|
+
module Mail
|
4
|
+
# == Sending Email with SMTP
|
5
|
+
#
|
6
|
+
# Mail allows you to send emails using an open SMTP connection. This is done by
|
7
|
+
# passing a created Net::SMTP object. This way we can get better performance to
|
8
|
+
# our local mail server by reducing the number of connections at any one time.
|
9
|
+
#
|
10
|
+
# === Sending via SMTP server on Localhost
|
11
|
+
#
|
12
|
+
# To send mail open a connection with Net::Smtp using any options you like
|
13
|
+
# === Delivering the email
|
14
|
+
#
|
15
|
+
# Once you have the settings right, sending the email is done by:
|
16
|
+
#
|
17
|
+
# smtp_conn = Net::SMTP.start(settings[:address], settings[:port])
|
18
|
+
# Mail.defaults do
|
19
|
+
# delivery_method :smtp_connection, { :connection => smtp_conn }
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# Mail.deliver do
|
23
|
+
# to 'mikel@test.lindsaar.net'
|
24
|
+
# from 'ada@test.lindsaar.net'
|
25
|
+
# subject 'testing sendmail'
|
26
|
+
# body 'testing sendmail'
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# Or by calling deliver on a Mail message
|
30
|
+
#
|
31
|
+
# mail = Mail.new do
|
32
|
+
# to 'mikel@test.lindsaar.net'
|
33
|
+
# from 'ada@test.lindsaar.net'
|
34
|
+
# subject 'testing sendmail'
|
35
|
+
# body 'testing sendmail'
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# mail.deliver!
|
39
|
+
class SMTPConnection
|
40
|
+
include Mail::CheckDeliveryParams
|
41
|
+
|
42
|
+
def initialize(values)
|
43
|
+
raise ArgumentError.new('A Net::SMTP object is required for this delivery method') if values[:connection].nil?
|
44
|
+
self.smtp = values[:connection]
|
45
|
+
self.settings = values
|
46
|
+
end
|
47
|
+
|
48
|
+
attr_accessor :smtp
|
49
|
+
attr_accessor :settings
|
50
|
+
|
51
|
+
# Send the message via SMTP.
|
52
|
+
# The from and to attributes are optional. If not set, they are retrieve from the Message.
|
53
|
+
def deliver!(mail)
|
54
|
+
smtp_from, smtp_to, message = check_delivery_params(mail)
|
55
|
+
response = smtp.sendmail(message, smtp_from, smtp_to)
|
56
|
+
|
57
|
+
settings[:return_response] ? response : self
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'mail/check_delivery_params'
|
2
|
+
|
3
|
+
module Mail
|
4
|
+
# The TestMailer is a bare bones mailer that does nothing. It is useful
|
5
|
+
# when you are testing.
|
6
|
+
#
|
7
|
+
# It also provides a template of the minimum methods you require to implement
|
8
|
+
# if you want to make a custom mailer for Mail
|
9
|
+
class TestMailer
|
10
|
+
include Mail::CheckDeliveryParams
|
11
|
+
|
12
|
+
# Provides a store of all the emails sent with the TestMailer so you can check them.
|
13
|
+
def TestMailer.deliveries
|
14
|
+
@@deliveries ||= []
|
15
|
+
end
|
16
|
+
|
17
|
+
# Allows you to over write the default deliveries store from an array to some
|
18
|
+
# other object. If you just want to clear the store,
|
19
|
+
# call TestMailer.deliveries.clear.
|
20
|
+
#
|
21
|
+
# If you place another object here, please make sure it responds to:
|
22
|
+
#
|
23
|
+
# * << (message)
|
24
|
+
# * clear
|
25
|
+
# * length
|
26
|
+
# * size
|
27
|
+
# * and other common Array methods
|
28
|
+
def TestMailer.deliveries=(val)
|
29
|
+
@@deliveries = val
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize(values)
|
33
|
+
@settings = values.dup
|
34
|
+
end
|
35
|
+
|
36
|
+
attr_accessor :settings
|
37
|
+
|
38
|
+
def deliver!(mail)
|
39
|
+
check_delivery_params(mail)
|
40
|
+
Mail::TestMailer.deliveries << mail
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|