wubmail 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/wubmail +16 -0
  2. data/lib/wubmail.rb +85 -0
  3. metadata +56 -0
data/bin/wubmail ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ require 'wubmail'
3
+
4
+ abort "Usage: #{File.basename($0)} [-s] templatefile peoplecsv" if ARGV.size < 2
5
+
6
+ emailer = WubMailCSV.new(ARGV[-2], ARGV[-1])
7
+
8
+ if ARGV.first == "-s"
9
+ emailer.send! do |recipient, ok|
10
+ puts "#{recipient[:email]}: #{ok ? "Done!" : "FAIL!"}"
11
+ end
12
+ else
13
+ puts emailer.example
14
+ puts
15
+ puts "Use -s to send it right now to #{emailer.recipients.size} recipients"
16
+ end
data/lib/wubmail.rb ADDED
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'erb'
4
+ require 'net/smtp'
5
+ require 'open-uri'
6
+
7
+ class WubMail
8
+ attr_accessor :options
9
+
10
+ def initialize(template, options = {})
11
+ @template = template
12
+ @options = options
13
+ parse
14
+ end
15
+
16
+ # allows template to use "name" instead of "options[:name]"
17
+ def method_missing(method, *args)
18
+ @options[method]
19
+ end
20
+
21
+ # sets attributes smtp_message, headers, from and to
22
+ def parse
23
+ # fetch contents from erb template
24
+ @smtp_message = ERB.new(@template).result(binding)
25
+
26
+ # build a headers hash
27
+ header_lines = @smtp_message.split("\n\n", 2).first.split("\n")
28
+ headers_options = header_lines.collect { |line| line.split(/: ?/, 2) }
29
+ @headers = Hash[*headers_options.flatten]
30
+
31
+ # guess emails from headers
32
+ @from = header 'From'
33
+ @to = header 'To'
34
+ end
35
+
36
+ def header(name)
37
+ @headers[name].gsub(/[<>]/, '').gsub(/^.* /, '').chomp
38
+ end
39
+
40
+ def to_s
41
+ @smtp_message
42
+ end
43
+
44
+ def send!
45
+ Net::SMTP.start('localhost') do |smtp|
46
+ smtp.send_message @smtp_message, @from, @to
47
+ end
48
+ true
49
+ end
50
+ end
51
+
52
+
53
+ class WubMailCSV
54
+ attr_reader :recipients
55
+
56
+ def initialize(template, csv)
57
+ @template = open(template).read
58
+ @csv = open(csv).read
59
+ parse
60
+ end
61
+
62
+ def parse
63
+ # ignore blank lines and comments
64
+ head, *lines = @csv.gsub(/#.*$/, '').grep(/\w/)
65
+ headers = head.split(/,/).collect { |h| h.strip.to_sym }
66
+
67
+ @recipients = lines.collect do |line|
68
+ values = line.split(/,/).collect { |v| v.strip }
69
+ Hash[*headers.zip(values).flatten]
70
+ end
71
+ end
72
+
73
+ def example
74
+ recipient = @recipients[rand(@recipients.size)]
75
+ WubMail.new(@template, recipient).to_s
76
+ end
77
+
78
+ def send!
79
+ @recipients.each do |recipient_hash|
80
+ ok = WubMail.new(@template, recipient_hash).send!
81
+ yield [recipient_hash, ok] if block_given?
82
+ end
83
+ end
84
+ end
85
+
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wubmail
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Sunny Ripert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-13 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Tool to quickly send emailings with a CSV file of users and an ERB template.
17
+ email: negatif@gmail.com
18
+ executables:
19
+ - wubmail
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/wubmail.rb
26
+ - bin/wubmail
27
+ has_rdoc: false
28
+ homepage: http://github.com/sunny/wubmail
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
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.3.5
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Send emails with a CSV and an ERB template
55
+ test_files: []
56
+