email_sender 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +165 -0
- data/README.rdoc +89 -0
- data/VERSION +1 -0
- data/lib/email_sender.rb +264 -0
- metadata +51 -0
data/LICENSE
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.rdoc
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
= EmailSender Library
|
2
|
+
|
3
|
+
EmailSender is an easy to use library to send email through SMTP server based on Net::SMTP library.
|
4
|
+
|
5
|
+
require 'email_sender'
|
6
|
+
|
7
|
+
mailer = EmailSender.new(server: "smtp.mail.com", from: "sender@mail.com")
|
8
|
+
mailer.send(to: "receiver@mail.com", subject: "Test mail!", content: "This is a test mail!")
|
9
|
+
|
10
|
+
== Usage
|
11
|
+
|
12
|
+
You need specified the connection parameters to SMTP server on initialization:
|
13
|
+
|
14
|
+
# simple unsafe connection
|
15
|
+
mailer = EmailSender.new(server: "smtp.mail.com", from: "Sender Name <sender@mail.com>")
|
16
|
+
|
17
|
+
# encripted and authenticated connection
|
18
|
+
mailer = EmailSender.new(server: "smtp.mail.com", port: 465, from: "Sender Name <sender@mail.com>",
|
19
|
+
enctype: :ssl_tls, authtype: :plain, username: "user.name", password: "secret")
|
20
|
+
|
21
|
+
Easier way to using Gmail account:
|
22
|
+
|
23
|
+
# minimal configuration
|
24
|
+
mailer = EmailSender.new_gmail(username: "user.name", password: "secret")
|
25
|
+
mailer = EmailSender.new_gmail(from: "user.name", password: "secret")
|
26
|
+
|
27
|
+
# with full sender name
|
28
|
+
mailer = EmailSender.new_gmail(from: "Sender Name <user.name@gmail.com>", password: "secret")
|
29
|
+
|
30
|
+
# with another sender email address and another domain (non gmail.com) account
|
31
|
+
mailer = EmailSender.new_gmail(from: "Sender Name <sender@othermail.com>",
|
32
|
+
username: "user.name@company.com", password: "secret")
|
33
|
+
|
34
|
+
You can modify easily the parameters of mailer object:
|
35
|
+
|
36
|
+
mailer = EmailSender.new(server: "smtp.mail.com", from: "sender@mail.com")
|
37
|
+
|
38
|
+
# modify parameters
|
39
|
+
mailer.renew(server: "smtp.mail.com", from: "Sender Name <sender@mail.com>")
|
40
|
+
|
41
|
+
# modify parameters to a Gmail account
|
42
|
+
mailer.renew_gmail(from: "Sender Name <user.name@gmail.com>", password: "secret")
|
43
|
+
|
44
|
+
Send the mail to the +:to+, +:cc+ and +:bcc+ addresses with attachment:
|
45
|
+
|
46
|
+
mailer.send(to: "Receiver Name <receiver@mail.com>", subject: "Test mail!",
|
47
|
+
content: "This is a test mail!", attachment: "/path/to/file")
|
48
|
+
|
49
|
+
The +:to+, +:cc+ and +:bcc+ keys accept an email address array so you can send the message to many receivers.
|
50
|
+
And the +:attachment+ key accepts also file path array so you can attach more file:
|
51
|
+
|
52
|
+
mailer.send(to: ["Receiver Name 1 <receiver1@mail.com>", "Receiver Name 2 <receiver2@mail.com>"],
|
53
|
+
cc: ["Receiver Name 3 <receiver3@mail.com>", "Receiver Name 4 <receiver4@mail.com>"],
|
54
|
+
bcc: [receiver5@mail.com, "receiver6@mail.com", "receiver7@mail.com"],
|
55
|
+
subject: "Test mail!", content: "This is a test mail!",
|
56
|
+
attachment: ["/path/to/file1", "/path/to/file2", "/path/to/file3"])
|
57
|
+
|
58
|
+
If there are not specified the +:to+, +:cc+ and +:bcc+ addresses on sending use the initialized default addresses:
|
59
|
+
|
60
|
+
# create a mailer with default addresses
|
61
|
+
mailer = EmailSender.new(server: "smtp.mail.com", from: "Sender Name <user.name@gmail.com>",
|
62
|
+
to: "Receiver Name 1 <receiver1@mail.com>",
|
63
|
+
cc: ["Receiver Name 2 <receiver2@mail.com>", "Receiver Name 3 <receiver3@mail.com>"])
|
64
|
+
|
65
|
+
# send email to the default addresses
|
66
|
+
mailer.send(subject: "Test mail!", content: "This is a test mail!")
|
67
|
+
|
68
|
+
You can specify the content type which is +'text/plain'+ by default:
|
69
|
+
|
70
|
+
mailer.send(to: "Receiver Name <receiver@mail.com>", subject: "The Ruby programming language",
|
71
|
+
content: "<img src='http://upload.wikimedia.org/wikipedia/commons/7/73/Ruby_logo.svg'/>",
|
72
|
+
conttype: "text/html")
|
73
|
+
|
74
|
+
The library has implemented threadsafe and support the character encoded strings of Ruby.
|
75
|
+
|
76
|
+
== Installation
|
77
|
+
|
78
|
+
sudo gem install email_sender
|
79
|
+
|
80
|
+
== Requirements
|
81
|
+
|
82
|
+
ruby1.9.1
|
83
|
+
|
84
|
+
== License
|
85
|
+
|
86
|
+
Copyright (C) 2012 Peter Bakonyi
|
87
|
+
|
88
|
+
EmailSender is free software; you can redistribute it and/or modify it under the terms of the
|
89
|
+
GNU Lesser General Public License (LGPL) version 3.0 as published by the Free Software Foundation.
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.2
|
data/lib/email_sender.rb
ADDED
@@ -0,0 +1,264 @@
|
|
1
|
+
# EmailSender library - Easy to use email sending library via SMTP
|
2
|
+
# Copyright (C) 2012 Péter Bakonyi <bakonyi.peter@gmail.com>
|
3
|
+
#
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU Lesser General Public License
|
6
|
+
# version 3.0 as published by the Free Software Foundation.
|
7
|
+
#
|
8
|
+
# This program is distributed in the hope that it will be useful,
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
# GNU Lesser General Public License version 3.0 for more details.
|
12
|
+
#
|
13
|
+
# You should have received a copy of the GNU Lesser General Public
|
14
|
+
# License along with the papyrus library. If not, see
|
15
|
+
# <http://www.gnu.org/licenses/>.
|
16
|
+
|
17
|
+
|
18
|
+
require 'openssl'
|
19
|
+
require 'net/smtp'
|
20
|
+
|
21
|
+
|
22
|
+
# == Usage
|
23
|
+
# mailer = EmailSender.new(server: "smtp.mail.com", from: "sender@mail.com")
|
24
|
+
# mailer = EmailSender.new(server: "smtp.mail.com", port: 465, from: Sender Name <"sender@mail.com">,
|
25
|
+
# enctype: :ssl_tls, authtype: :plain, username: "user.name", password: "secret")
|
26
|
+
#
|
27
|
+
# mailer = EmailSender.new_gmail(username: "user.name", password: "secret")
|
28
|
+
# mailer = EmailSender.new_gmail(from: "Sender Name <user.name@gmail.com>", password: "secret")
|
29
|
+
#
|
30
|
+
# mailer.send(to: "receiver@mail.com", subject: "Test mail!", content: "This is a test mail!")
|
31
|
+
# mailer.send(to: "Receiver Name 1 <receiver1@mail.com>", cc: "Receiver Name 2 <receiver2@mail.com>",
|
32
|
+
# subject: "Test mail!", content: "This is a test mail!", attachment: "/path/to/file")
|
33
|
+
# See the README for more useful examples!
|
34
|
+
class EmailSender
|
35
|
+
# Major version number
|
36
|
+
VERSION_MAJOR = 1
|
37
|
+
# Minor version number
|
38
|
+
VERSION_MINOR = 0
|
39
|
+
# Tiny version number
|
40
|
+
VERSION_TINY = 2
|
41
|
+
# Version number
|
42
|
+
VERSION_CODE = (VERSION_MAJOR << 16) | (VERSION_MINOR << 8) | VERSION_TINY
|
43
|
+
# Version string
|
44
|
+
VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_TINY}".freeze
|
45
|
+
|
46
|
+
ATTACHMENT_READ_CACHE = 116736 # multiple to 57 #:nodoc:
|
47
|
+
|
48
|
+
Settings = Struct.new(:server, :port, :domain, :esmtp, :enctype, :authtype,
|
49
|
+
:username, :password, :from_addr, :to_addrs, :cc_addrs, :bcc_addrs) #:nodoc:
|
50
|
+
|
51
|
+
# Create a new mailer object and initialize connection parameters to SMTP server.
|
52
|
+
# +-----------------------------------------------------------------------------------+
|
53
|
+
# | Initialization Parameters |
|
54
|
+
# +------------+-------------------------------------------+--------------------------+
|
55
|
+
# |Key |Possible Values |Default Value |
|
56
|
+
# +------------+-------------------------------------------+--------------------------+
|
57
|
+
# |:server |<string> | |
|
58
|
+
# |:port |<number> |25, 465 or 587 |
|
59
|
+
# |:domain |<string> |-> domain of from address |
|
60
|
+
# |:esmtp |:enabled/true, :disabled/false |:enabled |
|
61
|
+
# |:enctype |:none, :starttls_auto, :starttls, :ssl_tls |:none |
|
62
|
+
# |:authtype |:none, :plain, :login, :cram_md5 |:none |
|
63
|
+
# |:username |<string> |nil |
|
64
|
+
# |:password |<string> |nil |
|
65
|
+
# |:from |<string> | |
|
66
|
+
# |:to |<string>, <array of string> |[] |
|
67
|
+
# |:cc |<string>, <array of string> |[] |
|
68
|
+
# |:bcc |<string>, <array of string> |[] |
|
69
|
+
# +------------+-------------------------------------------+--------------------------+
|
70
|
+
def initialize(params)
|
71
|
+
renew(params)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Update the connection parameters to SMTP server.
|
75
|
+
# See the ::new method for initialization parameters.
|
76
|
+
def renew(params)
|
77
|
+
settings = Settings.new
|
78
|
+
settings.from_addr = parse_addr(params[:from])
|
79
|
+
settings.to_addrs = parse_addrs(*params[:to])
|
80
|
+
settings.cc_addrs = parse_addrs(*params[:cc])
|
81
|
+
settings.bcc_addrs = parse_addrs(*params[:bcc])
|
82
|
+
settings.domain = params[:domain] || settings.from_addr.last.split('@').last
|
83
|
+
settings.esmtp = params[:esmtp] != :disabled and params[:esmtp] != false
|
84
|
+
settings.enctype = params[:enctype]
|
85
|
+
authtype = params[:authtype]
|
86
|
+
username = params[:username]
|
87
|
+
password = params[:password]
|
88
|
+
if password and username and authtype and authtype != :none
|
89
|
+
settings.authtype = authtype
|
90
|
+
settings.username = username.encode(Encoding::UTF_8)
|
91
|
+
settings.password = password.encode(Encoding::UTF_8)
|
92
|
+
end
|
93
|
+
settings.server = params[:server].encode(Encoding::UTF_8)
|
94
|
+
settings.port = params[:port] || case settings.enctype
|
95
|
+
when :starttls then Net::SMTP.default_submission_port
|
96
|
+
when :ssl_tls then Net::SMTP.default_tls_port
|
97
|
+
else Net::SMTP.default_port
|
98
|
+
end
|
99
|
+
@settings = settings
|
100
|
+
end
|
101
|
+
|
102
|
+
# Create a new mailer object and initialize connection parameters to Gmail server.
|
103
|
+
# +-----------------------------------------------------------------------------------+
|
104
|
+
# | Gmail Initialization Parameters |
|
105
|
+
# +------------+-------------------------------------------+--------------------------+
|
106
|
+
# |Key |Possible Values |Default Value |
|
107
|
+
# +------------+-------------------------------------------+--------------------------+
|
108
|
+
# |:username |<string> |-> from address |
|
109
|
+
# |:password |<string> | |
|
110
|
+
# |:from |<string> |-> username parameter |
|
111
|
+
# |:to |<string>, <array of string> |[] |
|
112
|
+
# |:cc |<string>, <array of string> |[] |
|
113
|
+
# |:bcc |<string>, <array of string> |[] |
|
114
|
+
# +------------+-------------------------------------------+--------------------------+
|
115
|
+
def self.new_gmail(params)
|
116
|
+
instance = allocate()
|
117
|
+
instance.renew_gmail(params)
|
118
|
+
instance
|
119
|
+
end
|
120
|
+
|
121
|
+
# Update the connection parameters to Gmail server.
|
122
|
+
# See the ::new_gmail method for Gmail initialization parameters.
|
123
|
+
def renew_gmail(params)
|
124
|
+
params = params.dup
|
125
|
+
params[:server] = 'smtp.gmail.com'
|
126
|
+
params[:port] = 465
|
127
|
+
params[:domain] = 'gmail.com'
|
128
|
+
params[:esmtp] = :enabled
|
129
|
+
params[:enctype] = :ssl_tls
|
130
|
+
params[:authtype] = :plain
|
131
|
+
username = params[:username]
|
132
|
+
from = params[:from]
|
133
|
+
if username
|
134
|
+
username = username.encode(Encoding::UTF_8)
|
135
|
+
params[:username] = username << '@gmail.com' unless username.include?('@')
|
136
|
+
params[:from] = username unless from
|
137
|
+
end
|
138
|
+
if from
|
139
|
+
from_name, from_addr = parse_addr(from)
|
140
|
+
unless from_addr.include?('@')
|
141
|
+
from_addr << '@gmail.com'
|
142
|
+
params[:from] = from_name ? "#{from_name} <#{from_addr}>" : from_addr
|
143
|
+
end
|
144
|
+
params[:username] = from_addr unless username
|
145
|
+
end
|
146
|
+
renew(params)
|
147
|
+
end
|
148
|
+
|
149
|
+
# Send an email with the specified parameters.
|
150
|
+
# +-----------------------------------------------------------------------------------+
|
151
|
+
# | Sending Parameters |
|
152
|
+
# +------------+-------------------------------------------+--------------------------+
|
153
|
+
# |Key |Possible Values |Default Value |
|
154
|
+
# +------------+-------------------------------------------+--------------------------+
|
155
|
+
# |:to |<string>, <array of string> |\ values of |
|
156
|
+
# |:cc |<string>, <array of string> | > initialization |
|
157
|
+
# |:bcc |<string>, <array of string> |/ parameters |
|
158
|
+
# |:subject |<string> |'' |
|
159
|
+
# |:content |<string> |-> subject parameter |
|
160
|
+
# |:conttype |<string> |'text/plain' |
|
161
|
+
# |:attachment |<string>, <array of string> |[] |
|
162
|
+
# +------------+-------------------------------------------+--------------------------+
|
163
|
+
# See the README for more information!
|
164
|
+
def send(params={})
|
165
|
+
settings = @settings
|
166
|
+
subject = params[:subject] || ''
|
167
|
+
content = params[:content] || subject
|
168
|
+
conttype = params[:conttype] || 'text/plain'
|
169
|
+
attachment = *params[:attachment]
|
170
|
+
to = *params[:to]
|
171
|
+
cc = *params[:cc]
|
172
|
+
bcc = *params[:bcc]
|
173
|
+
if to.empty? and cc.empty? and bcc.empty?
|
174
|
+
to_addrs = settings.to_addrs
|
175
|
+
cc_addrs = settings.cc_addrs
|
176
|
+
bcc_addrs = settings.bcc_addrs
|
177
|
+
else
|
178
|
+
to_addrs = parse_addrs(*to)
|
179
|
+
cc_addrs = parse_addrs(*cc)
|
180
|
+
bcc_addrs = parse_addrs(*bcc)
|
181
|
+
end
|
182
|
+
unless to_addrs.empty?
|
183
|
+
subject = subject.encode(Encoding::UTF_8)
|
184
|
+
content = content.encode(Encoding::UTF_8)
|
185
|
+
smtp = Net::SMTP.new(settings.server, settings.port)
|
186
|
+
smtp.disable_starttls
|
187
|
+
smtp.disable_tls
|
188
|
+
case settings.enctype
|
189
|
+
when :starttls_auto then smtp.enable_starttls_auto
|
190
|
+
when :starttls then smtp.enable_starttls
|
191
|
+
when :ssl_tls then smtp.enable_tls
|
192
|
+
end
|
193
|
+
smtp.esmtp = settings.esmtp
|
194
|
+
message_id = nil
|
195
|
+
smtp.start(settings.domain, settings.username, settings.password, settings.authtype) do
|
196
|
+
from_name, from_addr = settings.from_addr
|
197
|
+
smtp.open_message_stream(from_addr, *to_addrs.keys, *cc_addrs.keys, *bcc_addrs.keys) do |stream|
|
198
|
+
now = Time.now
|
199
|
+
random_id = '%04x%04x.%08x.%08x.%08x' % [Process.pid & 0xFFFF, Thread.current.object_id & 0xFFFF,
|
200
|
+
now.tv_sec & 0xFFFF_FFFF, now.tv_nsec, rand(0x1_0000_0000)]
|
201
|
+
message_id = "#{random_id}@#{settings.domain}"
|
202
|
+
boundary = "boundary0_#{random_id}"
|
203
|
+
stream.puts("Message-ID: <#{message_id}>")
|
204
|
+
stream.puts(now.strftime('Date: %a, %d %b %Y %H:%M:%S %z'))
|
205
|
+
stream.puts("From: " << (from_name ? (from_name.ascii_only? ? "#{from_name} <#{from_addr}>" :
|
206
|
+
"=?utf-8?B?#{[from_name].pack('m0')}?= <#{from_addr}>") : from_addr))
|
207
|
+
to_str = ''
|
208
|
+
to_addrs.each do |addr, name|
|
209
|
+
to = name ? "#{name.ascii_only? ? name : "=?utf-8?B?#{[name].pack('m0')}?="} <#{addr}>" : addr
|
210
|
+
to_str << (to_str.empty? ? "To: #{to}" : ",\n\t#{to}")
|
211
|
+
end
|
212
|
+
stream.puts(to_str) unless to_str.empty?
|
213
|
+
cc_str = ''
|
214
|
+
cc_addrs.each do |addr, name|
|
215
|
+
cc = name ? "#{name.ascii_only? ? name : "=?utf-8?B?#{[name].pack('m0')}?="} <#{addr}>" : addr
|
216
|
+
cc_str << (cc_str.empty? ? "CC: #{cc}" : ",\n\t#{cc}")
|
217
|
+
end
|
218
|
+
stream.puts(cc_str) unless cc_str.empty?
|
219
|
+
stream.puts("Subject: " << (subject.ascii_only? ? subject : "=?utf-8?B?#{[subject].pack('m0')}?="))
|
220
|
+
stream.puts("MIME-Version: 1.0")
|
221
|
+
unless attachment.empty?
|
222
|
+
stream.puts("Content-Type: multipart/mixed; boundary=\"#{boundary}\"")
|
223
|
+
stream.puts
|
224
|
+
stream.puts("This is a multi-part message in MIME format.")
|
225
|
+
stream.puts("--#{boundary}")
|
226
|
+
end
|
227
|
+
stream.puts("Content-Type: #{conttype}; charset=#{content.ascii_only? ? 'us-ascii' : 'utf-8'}")
|
228
|
+
stream.puts("Content-Transfer-Encoding: base64")
|
229
|
+
stream.puts
|
230
|
+
stream.print([content].pack('m57'))
|
231
|
+
unless attachment.empty?
|
232
|
+
attachment.each do |file|
|
233
|
+
file = file.encode(Encoding::UTF_8)
|
234
|
+
basename = File.basename(file)
|
235
|
+
filename = basename.ascii_only? ? basename : "=?utf-8?B?#{[basename].pack('m0')}?="
|
236
|
+
stream.puts("--#{boundary}")
|
237
|
+
stream.puts("Content-Type: application/octet-stream; name=\"#{filename}\"")
|
238
|
+
stream.puts("Content-Transfer-Encoding: base64")
|
239
|
+
stream.puts("Content-Disposition: attachment; filename=\"#{filename}\"")
|
240
|
+
stream.puts
|
241
|
+
File.open(file) { |io| stream.print([io.read(ATTACHMENT_READ_CACHE)].pack('m57')) until io.eof? }
|
242
|
+
end
|
243
|
+
stream.puts("--#{boundary}--")
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
message_id
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
private
|
252
|
+
def parse_addr(str)
|
253
|
+
str = str.encode(Encoding::UTF_8)
|
254
|
+
str.scan(/\A\s*(.*?)\s*<([^<]*?)>\s*\z/).first || [nil, (str.strip!; str)]
|
255
|
+
end
|
256
|
+
def parse_addrs(*strs)
|
257
|
+
addrs = {}
|
258
|
+
strs.each do |str|
|
259
|
+
name, addr = parse_addr(str)
|
260
|
+
addrs[addr] = name
|
261
|
+
end
|
262
|
+
addrs
|
263
|
+
end
|
264
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: email_sender
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Peter Bakonyi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: EmailSender is a easy to use library to send email through SMTP server
|
15
|
+
based on Net::SMTP library.
|
16
|
+
email: bakonyi.peter@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files:
|
20
|
+
- README.rdoc
|
21
|
+
files:
|
22
|
+
- LICENSE
|
23
|
+
- VERSION
|
24
|
+
- README.rdoc
|
25
|
+
- lib/email_sender.rb
|
26
|
+
homepage: https://github.com/BPeti/email_sender
|
27
|
+
licenses:
|
28
|
+
- LGPL-3
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 1.9.1
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 1.8.11
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: Easy to use library to send email through any SMTP server.
|
51
|
+
test_files: []
|