alexmchale-gmail-client 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +0 -0
- data/VERSION.yml +4 -0
- data/lib/gmail.rb +156 -0
- metadata +56 -0
data/README.markdown
ADDED
File without changes
|
data/VERSION.yml
ADDED
data/lib/gmail.rb
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'net/smtp'
|
3
|
+
require 'base64'
|
4
|
+
|
5
|
+
class GMail
|
6
|
+
def initialize(username, password)
|
7
|
+
@username = username
|
8
|
+
@password = password
|
9
|
+
end
|
10
|
+
|
11
|
+
def start(to, subject, body, body_type = 'text/plain')
|
12
|
+
@to = to.to_s
|
13
|
+
@subject = subject.to_s
|
14
|
+
@body = body.to_s
|
15
|
+
@body_type = body_type
|
16
|
+
@attachments = []
|
17
|
+
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_data(name, data, type)
|
22
|
+
@attachments << { :name => name, :data => data, :type => type }
|
23
|
+
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def add_file(filename, content_type)
|
28
|
+
add_data File.basename(File.expand_path(filename)), File.read(filename), content_type
|
29
|
+
end
|
30
|
+
|
31
|
+
def add_jpeg(filename, data = nil)
|
32
|
+
name = File.basename filename
|
33
|
+
data ||= File.read(filename)
|
34
|
+
type = 'image/jpeg'
|
35
|
+
|
36
|
+
add_data name, data, type
|
37
|
+
end
|
38
|
+
|
39
|
+
def compose
|
40
|
+
boundary = rand(2**128).to_s(16)
|
41
|
+
|
42
|
+
if @attachments.length > 0
|
43
|
+
attachments = @attachments.map do |attachment|
|
44
|
+
"--#{boundary}\r\n" +
|
45
|
+
"Content-Type: #{attachment[:type]}; name=\"#{attachment[:name]}\"\r\n" +
|
46
|
+
"Content-Disposition: attachment; filename=\"#{attachment[:name]}\"\r\n" +
|
47
|
+
"Content-Transfer-Encoding: base64\r\n" +
|
48
|
+
"\r\n" +
|
49
|
+
Base64.encode64(attachment[:data])
|
50
|
+
end.compact.join
|
51
|
+
|
52
|
+
"Date: #{Time.now.to_s}\r\n" +
|
53
|
+
"From: #{@username}\r\n" +
|
54
|
+
"To: #{@to}\r\n" +
|
55
|
+
"Subject: #{@subject}\r\n" +
|
56
|
+
"MIME-Version: 1.0\r\n" +
|
57
|
+
"Content-Type: multipart/mixed; boundary=\"#{boundary}\"\r\n" +
|
58
|
+
"\r\n" +
|
59
|
+
"--#{boundary}\r\n" +
|
60
|
+
"Content-Type: text/plain\r\n" +
|
61
|
+
"\r\n" +
|
62
|
+
"#{@body}\r\n" +
|
63
|
+
"\r\n" +
|
64
|
+
attachments +
|
65
|
+
"--#{boundary}--\r\n" +
|
66
|
+
"\r\n.\r\n"
|
67
|
+
else
|
68
|
+
"Date: #{Time.now.to_s}\r\n" +
|
69
|
+
"From: #{@username}\r\n" +
|
70
|
+
"To: #{@to}\r\n" +
|
71
|
+
"Subject: #{@subject}\r\n" +
|
72
|
+
"Content-Type: text/plain\r\n" +
|
73
|
+
"\r\n" +
|
74
|
+
"#{@body}\r\n" +
|
75
|
+
"\r\n.\r\n"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def dispatch
|
80
|
+
Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com', @username, @password, :plain) do |smtp|
|
81
|
+
smtp.send_message compose, @username, @to
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def send(to, subject, body, content_type = 'text/plan')
|
86
|
+
Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com', @username, @password, :plain) do |smtp|
|
87
|
+
msg = "From: #{@username}\r\nTo: #{to}\r\nSubject: #{subject}\r\nContent-Type: #{content_type}\r\n\r\n#{body}"
|
88
|
+
smtp.send_message msg, @username, to
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Net::SMTP monkeypatching was taken from:
|
94
|
+
# http://www.stephenchu.com/2006/06/how-to-use-gmail-smtp-server-to-send.html
|
95
|
+
Net::SMTP.class_eval do
|
96
|
+
private
|
97
|
+
def do_start(helodomain, user, secret, authtype)
|
98
|
+
raise IOError, 'SMTP session already started' if @started
|
99
|
+
check_auth_args(user, secret) if (user or secret)
|
100
|
+
|
101
|
+
sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
|
102
|
+
@socket = Net::InternetMessageIO.new(sock)
|
103
|
+
@socket.read_timeout = 60 #@read_timeout
|
104
|
+
@socket.debug_output = STDERR #@debug_output
|
105
|
+
|
106
|
+
check_response(critical { recv_response() })
|
107
|
+
do_helo(helodomain)
|
108
|
+
|
109
|
+
raise 'openssl library not installed' unless defined?(OpenSSL)
|
110
|
+
starttls
|
111
|
+
ssl = OpenSSL::SSL::SSLSocket.new(sock)
|
112
|
+
ssl.sync_close = true
|
113
|
+
ssl.connect
|
114
|
+
@socket = Net::InternetMessageIO.new(ssl)
|
115
|
+
@socket.read_timeout = 60 #@read_timeout
|
116
|
+
@socket.debug_output = STDERR #@debug_output
|
117
|
+
do_helo(helodomain)
|
118
|
+
|
119
|
+
authenticate user, secret, authtype if user
|
120
|
+
@started = true
|
121
|
+
ensure
|
122
|
+
unless @started
|
123
|
+
# authentication failed, cancel connection.
|
124
|
+
@socket.close if not @started and @socket and not @socket.closed?
|
125
|
+
@socket = nil
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def do_helo(helodomain)
|
130
|
+
begin
|
131
|
+
if @esmtp
|
132
|
+
ehlo helodomain
|
133
|
+
else
|
134
|
+
helo helodomain
|
135
|
+
end
|
136
|
+
rescue Net::ProtocolError
|
137
|
+
if @esmtp
|
138
|
+
@esmtp = false
|
139
|
+
@error_occured = false
|
140
|
+
retry
|
141
|
+
end
|
142
|
+
raise
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def starttls
|
147
|
+
getok('STARTTLS')
|
148
|
+
end
|
149
|
+
|
150
|
+
def quit
|
151
|
+
begin
|
152
|
+
getok('QUIT')
|
153
|
+
rescue EOFError
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alexmchale-gmail-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex McHale
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-10 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: alexmchale@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
files:
|
25
|
+
- VERSION.yml
|
26
|
+
- README.markdown
|
27
|
+
- lib/gmail.rb
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://github.com/alexmchale/gmail-client
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options:
|
32
|
+
- --inline-source
|
33
|
+
- --charset=UTF-8
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.2.0
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
summary: A client to use GMail for sending email.
|
55
|
+
test_files: []
|
56
|
+
|