gmail_sender 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ *~
1
2
  *.sw?
2
3
  .DS_Store
3
4
  coverage
data/README.rdoc CHANGED
@@ -7,7 +7,14 @@ A simple gem to send email through gmail
7
7
 
8
8
  g = GmailSender.new("gmail_account_user_name", "gmail_account_password")
9
9
  g.attach('/path/to/document.hz') # you can attach any number of files, but there are limits for total attachments size
10
- g.send("someone@domain.com", "The subject", "The mail body")
10
+ g.send(:to => "someone@domain.com", :subject => "The subject", :content => "The mail body")
11
+
12
+ You can specify the content type which is text/plain by default.
13
+
14
+ g.send(:to => "someone@domain.com",
15
+ :subject => "The subject",
16
+ :content => "<img src='http://upload.wikimedia.org/wikipedia/en/0/0d/Simpsons_FamilyPicture.png'/>",
17
+ :content_type => 'text/html')
11
18
 
12
19
  To use your google apps domain instead of gmail.com include the complete sender email instead of just the user name:
13
20
 
@@ -36,7 +43,15 @@ You can specify some parameters like in this example which doesn't use pipes:
36
43
  You can send attachments by using the -a option (this example assumes that you have a receiver_email set in the ~/.gmail file so the -t is not needed):
37
44
 
38
45
  gmail -c "hi, I'm sending some attachments" -a ./VERSION ./gmail_sender.gemspec
39
-
46
+
47
+ To send html content use the ct option
48
+
49
+ gmail -c "<img src='http://upload.wikimedia.org/wikipedia/en/0/0d/Simpsons_FamilyPicture.png'/>" -ct text/html
50
+
51
+ == Installation
52
+
53
+ sudo gem install gmail_sender
54
+
40
55
  == Requirements
41
56
 
42
57
  tlsmail if running Ruby 1.8.6
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 1.0.0
data/bin/gmail CHANGED
@@ -26,6 +26,12 @@ Choice.options do
26
26
  desc 'Body of the email, uses STDIN if omitted'
27
27
  end
28
28
 
29
+ option :content_type do
30
+ short '-ct'
31
+ long '--content_type=CONTENT_TYPE'
32
+ desc 'Content Type, text/plain by default, use text/html for html'
33
+ end
34
+
29
35
  option :attachments do
30
36
  short '-a'
31
37
  long '--attachments *ATTACHMENTS'
@@ -48,7 +54,8 @@ def params
48
54
  end
49
55
 
50
56
  defaults = {'subject' => 'Sent from command line',
51
- 'sender_domain' => 'gmail.com'}
57
+ 'sender_domain' => 'gmail.com',
58
+ 'content_type' => 'text/plain'}
52
59
 
53
60
  @params = defaults.merge(YAML.load_file(config_file_path)).merge(Choice.choices)
54
61
  end
@@ -61,4 +68,4 @@ if params['attachments']
61
68
  end
62
69
  end
63
70
  content = params['content'] || (params['attachments'] && params['attachments'].join("\n"))
64
- mailer.send(params['receiver_email'], params['subject'], content || STDIN.read)
71
+ mailer.send(:to => params['receiver_email'], :subject => params['subject'], :content => content || STDIN.read, :content_type => params['content_type'])
data/gmail_sender.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{gmail_sender}
8
- s.version = "0.4.0"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Daniel Cadenas"]
12
- s.date = %q{2010-02-21}
12
+ s.date = %q{2010-02-28}
13
13
  s.default_executable = %q{gmail}
14
14
  s.email = %q{dcadenas@gmail.com}
15
15
  s.executables = ["gmail"]
@@ -27,7 +27,9 @@ Gem::Specification.new do |s|
27
27
  "bin/gmail",
28
28
  "gmail_sender.gemspec",
29
29
  "lib/gmail_sender.rb",
30
+ "lib/gmail_sender/error.rb",
30
31
  "lib/gmail_sender/message_stream_writer.rb",
32
+ "lib/gmail_sender/utils.rb",
31
33
  "lib/tls_smtp_patch.rb",
32
34
  "test/gmail_sender_test.rb",
33
35
  "test/message_stream_writer_test.rb",
@@ -36,7 +38,7 @@ Gem::Specification.new do |s|
36
38
  s.homepage = %q{http://github.com/dcadenas/gmail_sender}
37
39
  s.rdoc_options = ["--charset=UTF-8"]
38
40
  s.require_paths = ["lib"]
39
- s.rubygems_version = %q{1.3.5}
41
+ s.rubygems_version = %q{1.3.6}
40
42
  s.summary = %q{A simple gem to send email through gmail}
41
43
  s.test_files = [
42
44
  "test/gmail_sender_test.rb",
@@ -0,0 +1,4 @@
1
+ class GmailSender
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -10,9 +10,9 @@ class GmailSender
10
10
  @boundary = rand(2**256).to_s(16)
11
11
  end
12
12
 
13
- def write(msg_stream, to, subject, content)
13
+ def write(msg_stream, to, subject, content, content_type)
14
14
  write_headers(msg_stream, to, subject)
15
- write_content(msg_stream, content)
15
+ write_content(msg_stream, content, content_type)
16
16
  write_attachments(msg_stream)
17
17
  end
18
18
 
@@ -21,19 +21,19 @@ class GmailSender
21
21
  msg_stream.puts "From: #{@sender_email}"
22
22
  msg_stream.puts "To: #{to}"
23
23
  msg_stream.puts "Subject: #{subject}"
24
+ msg_stream.puts 'MIME-Version: 1.0'
24
25
  unless @attachments.empty?
25
- msg_stream.puts 'MIME-Version: 1.0'
26
26
  msg_stream.puts %{Content-Type: multipart/mixed; boundary="#{@boundary}"}
27
27
  end
28
- msg_stream.puts
29
28
  end
30
29
 
31
- def write_content(msg_stream, content)
30
+ def write_content(msg_stream, content, content_type)
32
31
  unless @attachments.empty?
33
32
  msg_stream.puts "--#{@boundary}" unless @attachments.empty?
34
- msg_stream.puts 'Content-Type: text/plain'
35
33
  end
36
34
 
35
+ msg_stream.puts "Content-Type: #{content_type}"
36
+ msg_stream.puts
37
37
  msg_stream.puts content
38
38
  end
39
39
 
@@ -0,0 +1,7 @@
1
+ class GmailSender
2
+ module Utils
3
+ def self.blank?(string)
4
+ string.nil? || string.strip == ""
5
+ end
6
+ end
7
+ end
data/lib/gmail_sender.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'base64'
2
2
  require 'tls_smtp_patch'
3
3
  require 'gmail_sender/message_stream_writer'
4
+ require 'gmail_sender/utils'
5
+ require 'gmail_sender/error'
4
6
 
5
7
  class GmailSender
6
8
  def initialize(user_or_email, password, net_smtp_class = Net::SMTP, message_stream_writer_class = MessageStreamWriter)
@@ -19,10 +21,17 @@ class GmailSender
19
21
  @message_stream_writer.attachments << file if File.exist?(file)
20
22
  end
21
23
 
22
- def send(to, subject = "", content = "")
24
+ def send(options = {})
25
+ to = options[:to]
26
+ subject = options[:subject] || ""
27
+ content = options[:content] || ""
28
+ content_type = options[:content_type] || "text/plain; charset='utf-8'"
29
+
30
+ raise(Error, "Missing receiver (:to => 'someone@somehost.com')") if Utils.blank?(to)
31
+
23
32
  @net_smtp.start(@sender_domain, @sender_email, @sender_password, :plain) do |smtp|
24
33
  smtp.open_message_stream(@sender_email, [to]) do |msg_stream|
25
- @message_stream_writer.write(msg_stream, to, subject, content)
34
+ @message_stream_writer.write(msg_stream, to, subject, content, content_type)
26
35
  end
27
36
  end
28
37
  end
@@ -23,15 +23,20 @@ class FakedNetSMTP
23
23
  end
24
24
 
25
25
  Expectations do
26
- expect "From: me@gmail.com\nTo: someone@somehost.com\nSubject: hi!\n\nho\n" do
26
+ expect "From: me@gmail.com\nTo: someone@somehost.com\nSubject: hi!\nMIME-Version: 1.0\nContent-Type: text/plain; charset='utf-8'\n\nho\n" do
27
27
  gmail_sender = GmailSender.new("me", "password", FakedNetSMTP)
28
- gmail_sender.send("someone@somehost.com", "hi!", "ho")
28
+ gmail_sender.send(:to => "someone@somehost.com", :subject => "hi!", :content => "ho")
29
29
  FakedNetSMTP.sent_email
30
30
  end
31
31
 
32
- expect "From: me@somehost.com\nTo: someone@someotherhost.com\nSubject: hi!\n\nho\n" do
32
+ expect "From: me@somehost.com\nTo: someone@someotherhost.com\nSubject: hi!\nMIME-Version: 1.0\nContent-Type: text/plain; charset='utf-8'\n\nho\n" do
33
33
  gmail_sender = GmailSender.new("me@somehost.com", "password", FakedNetSMTP)
34
- gmail_sender.send("someone@someotherhost.com", "hi!", "ho")
34
+ gmail_sender.send(:to => "someone@someotherhost.com", :subject => "hi!", :content => "ho")
35
35
  FakedNetSMTP.sent_email
36
36
  end
37
+
38
+ expect GmailSender::Error do
39
+ gmail_sender = GmailSender.new("me@somehost.com", "password", FakedNetSMTP)
40
+ gmail_sender.send(:subject => "hi!", :content => "ho")
41
+ end
37
42
  end
@@ -1,10 +1,10 @@
1
1
  require 'test_helper'
2
2
 
3
3
  Expectations do
4
- expect "From: sender@gmail.com\nTo: to@gmail.com\nSubject: subject\n\nbody\n" do
4
+ expect "From: sender@gmail.com\nTo: to@gmail.com\nSubject: subject\nMIME-Version: 1.0\nContent-Type: text/plain\n\nbody\n" do
5
5
  string_io = StringIO.new
6
6
  message_stream_writer = GmailSender::MessageStreamWriter.new("sender@gmail.com")
7
- message_stream_writer.write(string_io, "to@gmail.com", "subject", "body")
7
+ message_stream_writer.write(string_io, "to@gmail.com", "subject", "body", "text/plain")
8
8
  string_io.string
9
9
  end
10
10
 
@@ -14,9 +14,9 @@ To: to@gmail.com
14
14
  Subject: subject
15
15
  MIME-Version: 1.0
16
16
  Content-Type: multipart\\/mixed; boundary="([a-f0-9]+)"
17
-
18
17
  --\\1
19
18
  Content-Type: text\\/plain
19
+
20
20
  body
21
21
  --\\1
22
22
  Content-Type: application\\/octet-stream; name="first_file.txt"
@@ -40,7 +40,7 @@ MAIL_WITH_ATTACHMENT_REGEXP
40
40
  message_stream_writer.attachments << "second_file.txt"
41
41
 
42
42
  with_files("first_file.txt" => "this is the first file content", "second_file.txt" => "and this is the second file content") do
43
- message_stream_writer.write(string_io, "to@gmail.com", "subject", "body")
43
+ message_stream_writer.write(string_io, "to@gmail.com", "subject", "body", "text/plain")
44
44
  end
45
45
 
46
46
  string_io.string
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gmail_sender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Daniel Cadenas
@@ -9,29 +14,33 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-21 00:00:00 -02:00
17
+ date: 2010-02-28 00:00:00 -02:00
13
18
  default_executable: gmail
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: expectations
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
23
29
  version: "0"
24
- version:
30
+ type: :development
31
+ version_requirements: *id001
25
32
  - !ruby/object:Gem::Dependency
26
33
  name: filetesthelper
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - ">="
32
38
  - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
33
41
  version: "0"
34
- version:
42
+ type: :development
43
+ version_requirements: *id002
35
44
  description:
36
45
  email: dcadenas@gmail.com
37
46
  executables:
@@ -51,7 +60,9 @@ files:
51
60
  - bin/gmail
52
61
  - gmail_sender.gemspec
53
62
  - lib/gmail_sender.rb
63
+ - lib/gmail_sender/error.rb
54
64
  - lib/gmail_sender/message_stream_writer.rb
65
+ - lib/gmail_sender/utils.rb
55
66
  - lib/tls_smtp_patch.rb
56
67
  - test/gmail_sender_test.rb
57
68
  - test/message_stream_writer_test.rb
@@ -69,18 +80,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
80
  requirements:
70
81
  - - ">="
71
82
  - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
72
85
  version: "0"
73
- version:
74
86
  required_rubygems_version: !ruby/object:Gem::Requirement
75
87
  requirements:
76
88
  - - ">="
77
89
  - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
78
92
  version: "0"
79
- version:
80
93
  requirements: []
81
94
 
82
95
  rubyforge_project:
83
- rubygems_version: 1.3.5
96
+ rubygems_version: 1.3.6
84
97
  signing_key:
85
98
  specification_version: 3
86
99
  summary: A simple gem to send email through gmail