gmail_sender 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -6,6 +6,7 @@ A simple gem to send email through gmail
6
6
  require 'gmail_sender'
7
7
 
8
8
  g = GmailSender.new("gmail_account_user_name", "gmail_account_password")
9
+ g.attach('/path/to/document.hz') # you can attach any number of files, but there are limits for total attachments size
9
10
  g.send("someone@domain.com", "The subject", "The mail body")
10
11
 
11
12
  == Command line usage
@@ -27,6 +28,10 @@ To send your directory list to the default receiver:
27
28
  You can specify some parameters like in this example which doesn't use pipes:
28
29
 
29
30
  gmail -t somebody@gmail.com -s "This is the subject" -c "This is the email content"
31
+
32
+ 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):
33
+
34
+ gmail -c "hi, I'm sending some attachments" -a ./VERSION ./gmail_sender.gemspec
30
35
 
31
36
  == Requirements
32
37
 
@@ -36,3 +41,4 @@ tlsmail if running Ruby 1.8.6
36
41
 
37
42
  * Daniel Cadenas - Maintainer
38
43
  * Felipe Coury
44
+ * iwakura
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/bin/gmail CHANGED
@@ -25,6 +25,13 @@ Choice.options do
25
25
  long '--content=MAIL_CONTENTS'
26
26
  desc 'Body of the email, uses STDIN if omitted'
27
27
  end
28
+
29
+ option :attachments do
30
+ short '-a'
31
+ long '--attachments *ATTACHMENTS'
32
+ desc "A list of space separated list of files you wish to attach."
33
+ end
34
+
28
35
  end
29
36
 
30
37
  def params
@@ -48,4 +55,8 @@ end
48
55
 
49
56
 
50
57
  mailer = GmailSender.new(params['sender_user'], params['sender_password'], params['sender_domain'])
51
- mailer.send(params['receiver_email'], params['subject'], params['content'] || STDIN.read)
58
+ params['attachments'].each do |attachment|
59
+ mailer.attach(attachment)
60
+ end
61
+ content = params['content'] || (params['attachments'] && params['attachments'].join("\n"))
62
+ mailer.send(params['receiver_email'], params['subject'], content || STDIN.read)
data/gmail_sender.gemspec CHANGED
@@ -1,18 +1,21 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
1
4
  # -*- encoding: utf-8 -*-
2
5
 
3
6
  Gem::Specification.new do |s|
4
7
  s.name = %q{gmail_sender}
5
- s.version = "0.1.2"
8
+ s.version = "0.3.0"
6
9
 
7
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Daniel Cadenas", "Felipe Coury"]
9
- s.date = %q{2009-05-25}
10
- s.email = %q{dcadenas@gmail.com felipe.coury@gmail.com}
11
- s.bindir = "bin"
11
+ s.authors = ["Daniel Cadenas"]
12
+ s.date = %q{2009-12-24}
13
+ s.default_executable = %q{gmail}
14
+ s.email = %q{dcadenas@gmail.com}
12
15
  s.executables = ["gmail"]
13
16
  s.extra_rdoc_files = [
14
17
  "LICENSE",
15
- "README.rdoc"
18
+ "README.rdoc"
16
19
  ]
17
20
  s.files = [
18
21
  ".document",
@@ -21,8 +24,8 @@ Gem::Specification.new do |s|
21
24
  "README.rdoc",
22
25
  "Rakefile",
23
26
  "VERSION",
24
- "gmail_sender.gemspec",
25
27
  "bin/gmail",
28
+ "gmail_sender.gemspec",
26
29
  "lib/gmail_sender.rb",
27
30
  "lib/tls_smtp_patch.rb",
28
31
  "test/gmail_sender_test.rb",
@@ -31,8 +34,8 @@ Gem::Specification.new do |s|
31
34
  s.homepage = %q{http://github.com/dcadenas/gmail_sender}
32
35
  s.rdoc_options = ["--charset=UTF-8"]
33
36
  s.require_paths = ["lib"]
34
- s.rubygems_version = %q{1.3.3}
35
- s.summary = s.description = %q{A simple gem to send email through gmail}
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{A simple gem to send email through gmail}
36
39
  s.test_files = [
37
40
  "test/gmail_sender_test.rb",
38
41
  "test/test_helper.rb"
@@ -43,9 +46,9 @@ Gem::Specification.new do |s|
43
46
  s.specification_version = 3
44
47
 
45
48
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
- s.add_runtime_dependency(%q<choice>, [">= 0"])
47
49
  else
48
50
  end
49
51
  else
50
52
  end
51
53
  end
54
+
data/lib/gmail_sender.rb CHANGED
@@ -1,28 +1,69 @@
1
+ require 'base64'
1
2
  require "tls_smtp_patch"
2
3
 
3
4
  class GmailSender
5
+
6
+ ATTACHMENT_READ_PORTION = 150360 # you may change this, but must be multiply to 3
7
+
4
8
  def initialize(user, password, domain="gmail.com")
5
9
  @sender_domain = domain
6
10
  @sender_password = password
7
11
  @sender_email = "#{user}@#{domain}"
8
12
  @net_smtp = Net::SMTP.new("smtp.gmail.com", 587)
9
- @net_smtp.enable_starttls
13
+ @net_smtp.enable_starttls
14
+ @attachments = []
15
+ @boundary = rand(2**256).to_s(16)
16
+ end
17
+
18
+ def attach(file)
19
+ @attachments << file if File.exist?(file)
10
20
  end
11
21
 
12
- def send(to, subject, content)
22
+ def send(to, subject = "", content = "")
13
23
  @net_smtp.start(@sender_domain, @sender_email, @sender_password, :plain) do |smtp|
14
- msg = create_message(to, subject, content)
15
- smtp.send_message(msg, @sender_email, to)
24
+ smtp.open_message_stream(@sender_email, [to]) do |msg_stream|
25
+ set_message_stream(msg_stream, to, subject, content)
26
+ end
27
+ end
28
+ end
29
+
30
+ def set_message_stream(msg_stream, to, subject, content)
31
+ set_headers(msg_stream, to, subject)
32
+ set_content(msg_stream, content)
33
+ set_attachments(msg_stream)
34
+ end
35
+
36
+ private
37
+ def set_headers(msg_stream, to, subject)
38
+ msg_stream.puts "From: #{@sender_email}"
39
+ msg_stream.puts "To: #{to}"
40
+ msg_stream.puts "Subject: #{subject}"
41
+ unless @attachments.empty?
42
+ msg_stream.puts 'MIME-Version: 1.0'
43
+ msg_stream.puts %{Content-Type: multipart/mixed; boundary="#{@boundary}"}
44
+ end
45
+ msg_stream.puts
46
+ unless @attachments.empty?
47
+ msg_stream.puts "--#{@boundary}"
48
+ msg_stream.puts 'Content-Type: text/plain'
16
49
  end
17
50
  end
18
51
 
19
- def create_message(to, subject, content)
20
- <<MSG
21
- From: #@sender_email
22
- To: #{to}
23
- Subject: #{subject}
52
+ def set_content(msg_stream, content)
53
+ msg_stream.puts content
54
+ end
24
55
 
25
- #{content}
26
- MSG
56
+ def set_attachments(msg_stream)
57
+ @attachments.each do |file|
58
+ msg_stream.puts "--#{@boundary}"
59
+ msg_stream.puts %{Content-Type: application/octet-stream; name="#{File.basename(file)}"}
60
+ msg_stream.puts %{Content-Disposition: attachment; filename="#{File.basename(file)}"}
61
+ msg_stream.puts 'Content-Transfer-Encoding: base64'
62
+ msg_stream.puts "Content-ID: <#{File.basename(file)}>"
63
+ msg_stream.puts
64
+ File.open(file) do |fd|
65
+ msg_stream.puts Base64.encode64(fd.read(ATTACHMENT_READ_PORTION)) until fd.eof?
66
+ end
67
+ end
27
68
  end
28
69
  end
@@ -3,6 +3,8 @@ require 'test_helper'
3
3
  Expectations do
4
4
  expect "From: gmail_account_name@gmail.com\nTo: to@gmail.com\nSubject: subject\n\nbody\n" do
5
5
  gmail_sender = GmailSender.new("gmail_account_name", "gmail_account_password")
6
- gmail_sender.create_message("to@gmail.com", "subject", "body")
6
+ string_io = StringIO.new
7
+ gmail_sender.set_message_stream(string_io, "to@gmail.com", "subject", "body")
8
+ string_io.string
7
9
  end
8
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gmail_sender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Cadenas
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-02 00:00:00 -02:00
12
+ date: 2009-12-24 00:00:00 -02:00
13
13
  default_executable: gmail
14
14
  dependencies: []
15
15