ruby-mailchimp 0.1.0
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/lib/mail_chimp.rb +205 -0
- metadata +54 -0
data/lib/mail_chimp.rb
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/imap'
|
3
|
+
require 'rubygems'
|
4
|
+
gem 'actionmailer'
|
5
|
+
require 'action_mailer'
|
6
|
+
|
7
|
+
#
|
8
|
+
# ruby-mailchimp
|
9
|
+
#
|
10
|
+
# ruby-mailchimp is designed to allow you to use Ruby to subscribe or unsubscribe
|
11
|
+
# users to an existing MailChimp (http://www.mailchimp.com) account. You will need the following:
|
12
|
+
# - MailChimp Account: MailChimp is free to sign up for and only charges for sent messages
|
13
|
+
# - MailChimp List ID: You can get this from the id field of MailChimp's signup form code.
|
14
|
+
#
|
15
|
+
# Author:: C. G. Brown (mailto: cgbrown@projectlocker.com)
|
16
|
+
# Copyright:: Copyright (c) 2007 One Percent Software/ProjectLocker
|
17
|
+
# License:: Apache Software License
|
18
|
+
#
|
19
|
+
# Example Usage:
|
20
|
+
# - Subscribing a User: -
|
21
|
+
# bonobo = MailChimp.new
|
22
|
+
# bonobo.subscribe_user("MyListID", "testuser@nowhere.com")
|
23
|
+
#
|
24
|
+
# - Subscribing a User with other data: -
|
25
|
+
# bonobo = MailChimp.new
|
26
|
+
# bonobo.subscribe_user("MyListID", "otheruser@nowhere.com", {:FNAME=>'Barnaby', :LNAME=>'Jones'})
|
27
|
+
#
|
28
|
+
# - Unsubscribing a User -
|
29
|
+
# bonobo = MailChimp.new
|
30
|
+
# bonobo.unsubscribe_user("MyListID", "thirduser@nowhere.com")
|
31
|
+
#
|
32
|
+
# - Checking for Incoming Subscribes/Unsubscribes -
|
33
|
+
# bonobo = MailChimp.new
|
34
|
+
# bonobo.initialize_inbox("my.mailserver.com", "myusername", "mypassword")
|
35
|
+
# subscribes, unsubscribes = bonobo.check_inbox()
|
36
|
+
# subscribes.each do |subscriber|
|
37
|
+
# email = subscriber[:email]
|
38
|
+
# ip = subscriber[:ip]
|
39
|
+
# signup_date = subscriber[:date]
|
40
|
+
# #Do what you like with the data here
|
41
|
+
# end
|
42
|
+
# unsubscribes.each do |unsubscriber|
|
43
|
+
# email = unsubscriber[:email]
|
44
|
+
# ip = unsubscriber[:ip]
|
45
|
+
# signup_date = unsubscriber[:date]
|
46
|
+
# #Do what you like with the data here
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
class MailChimp
|
50
|
+
|
51
|
+
attr_accessor :email_server, :email_user, :email_password
|
52
|
+
|
53
|
+
#
|
54
|
+
# Stores the inbox attributes to allow connection by check_inbox
|
55
|
+
#
|
56
|
+
def initialize_inbox(server, user, password)
|
57
|
+
self.email_server = server
|
58
|
+
self.email_user = user
|
59
|
+
self.email_password = password
|
60
|
+
end
|
61
|
+
|
62
|
+
#
|
63
|
+
# Adds a user to the specified MailChimp list.
|
64
|
+
# list_id - The MailChimp List ID
|
65
|
+
# email - The user's e-mail address
|
66
|
+
# additional_fields - An optional hash containing other fields to pass in.
|
67
|
+
# The key for each field should be the name of the field as it would be specified in a form.
|
68
|
+
#
|
69
|
+
def subscribe_user(list_id, email, additional_fields=nil)
|
70
|
+
additional_fields = Hash::new if (additional_fields.nil?)
|
71
|
+
additional_fields['EMAIL'] = email
|
72
|
+
additional_fields['id'] = list_id
|
73
|
+
return subscribe_users([additional_fields])
|
74
|
+
end
|
75
|
+
|
76
|
+
#
|
77
|
+
# Executes a batch subscribe of multiple users.
|
78
|
+
# hash_array - An array of hash lines
|
79
|
+
#
|
80
|
+
def subscribe_users(hash_array)
|
81
|
+
post_to_mailchimp(hash_array, "subscribe.phtml")
|
82
|
+
end
|
83
|
+
|
84
|
+
#
|
85
|
+
# Removes a user from the specified MailChimp list.
|
86
|
+
# list_id - The MailChimp List ID
|
87
|
+
# email - The user's e-mail address
|
88
|
+
#
|
89
|
+
def unsubscribe_user(list_id, email)
|
90
|
+
additional_fields = Hash::new if (additional_fields.nil?)
|
91
|
+
additional_fields['EMAIL'] = email
|
92
|
+
additional_fields['id'] = list_id
|
93
|
+
return unsubscribe_users([additional_fields])
|
94
|
+
end
|
95
|
+
|
96
|
+
#
|
97
|
+
# Executes a batch unsubscribe of multiple users.
|
98
|
+
# hash_array - An array of hash lines
|
99
|
+
#
|
100
|
+
def unsubscribe_users(hash_array)
|
101
|
+
post_to_mailchimp(hash_array, "unsub.phtml")
|
102
|
+
end
|
103
|
+
|
104
|
+
#
|
105
|
+
# Reads IMAP mail from the mailbox initialized with initialize_inbox.
|
106
|
+
# Returns a subscribers array containing hashes of user data, and an
|
107
|
+
# unsubscriber's array containing hashes of user data. Email, IP, and date as
|
108
|
+
# a string are returned to the caller.
|
109
|
+
#
|
110
|
+
#
|
111
|
+
def check_inbox(delete=nil, mode=nil)
|
112
|
+
delete = false if (delete.nil?)
|
113
|
+
if (!mode.nil? && mode.upcase != "IMAP")
|
114
|
+
STDERR.write("Mode #{mode} not supported. Only IMAP supported at this time.")
|
115
|
+
elsif (email_server.nil? || email_user.nil? || email_password.nil?)
|
116
|
+
STDERR.write("You must initialize the mailbox before checking it. Try initialize_inbox.")
|
117
|
+
else
|
118
|
+
subscribes = Array::new
|
119
|
+
unsubscribes = Array::new
|
120
|
+
imap = Net::IMAP.new(email_server)
|
121
|
+
imap.authenticate('LOGIN', email_user, email_password)
|
122
|
+
imap.select('INBOX')
|
123
|
+
imap.search(['ALL']).each do |message_id|
|
124
|
+
msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
|
125
|
+
stored_value = Processor.receive(msg)
|
126
|
+
subscribes << stored_value if (!stored_value.nil? && stored_value[:type] == :subscription)
|
127
|
+
unsubscribes << stored_value if (!stored_value.nil? && stored_value[:type] == :unsubscription)
|
128
|
+
#Mark message as deleted and it will be removed from storage when user session closes
|
129
|
+
imap.store(message_id, "+FLAGS", [:Deleted]) if (delete == true)
|
130
|
+
end
|
131
|
+
|
132
|
+
imap.expunge if (delete == true)
|
133
|
+
|
134
|
+
imap.disconnect
|
135
|
+
return [subscribes, unsubscribes]
|
136
|
+
end
|
137
|
+
return nil
|
138
|
+
end
|
139
|
+
|
140
|
+
protected
|
141
|
+
|
142
|
+
#
|
143
|
+
# Handles internal posting to MailChimp, given a processor page.
|
144
|
+
#
|
145
|
+
def post_to_mailchimp(hash_array, page)
|
146
|
+
hash_array.each do |additional_fields|
|
147
|
+
http = Net::HTTP.new("www.mailchimp.com")
|
148
|
+
headers = {
|
149
|
+
"Content-type" => "application/x-www-form-urlencoded",
|
150
|
+
"User-Agent" => "MCSubscriber ruby-0.1"
|
151
|
+
}
|
152
|
+
response, body = http.post("/unsub.phtml", hash_to_uri(additional_fields), headers)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
#
|
157
|
+
# Converts a hash to a data string suitable for HTTP POST or GET
|
158
|
+
#
|
159
|
+
def hash_to_uri(inhash)
|
160
|
+
result_str = ""
|
161
|
+
inhash.each { |key, val|
|
162
|
+
result_str << "#{key}=#{val}&"
|
163
|
+
}
|
164
|
+
result_str.chop unless inhash.size == 0
|
165
|
+
end
|
166
|
+
|
167
|
+
class Processor < ActionMailer::Base
|
168
|
+
def receive(msg)
|
169
|
+
p msg.subject
|
170
|
+
p msg.subject =~ /New Subscribe to/
|
171
|
+
p msg.subject =~ /Unsubscribe from/
|
172
|
+
#Process all new subscriptions
|
173
|
+
is_subscription = !((msg.subject =~ /New Subscribe to/).nil?)
|
174
|
+
is_unsubscription = !((msg.subject =~ /Unsubscribe from/).nil?)
|
175
|
+
p "status = #{is_subscription} #{is_unsubscription}"
|
176
|
+
if is_subscription || is_unsubscription
|
177
|
+
stored_value = Hash::new
|
178
|
+
if (is_subscription)
|
179
|
+
stored_value[:list] = msg.subject.gsub("New Subscribe to ", "")
|
180
|
+
stored_value[:type] = :subscription
|
181
|
+
else
|
182
|
+
stored_value[:list] = msg.subject.gsub("Unsubscribe from ", "")
|
183
|
+
stored_value[:type] = :unsubscription
|
184
|
+
end
|
185
|
+
msg.body.each_line do |parseline|
|
186
|
+
field, value = parseline.split(":", 2)
|
187
|
+
if (!value.nil?)
|
188
|
+
field = field.strip
|
189
|
+
value = value.strip
|
190
|
+
#TODO: Deal with other fields
|
191
|
+
if (field == "Email Address")
|
192
|
+
stored_value[:email] = value
|
193
|
+
elsif (field == "Signup IP")
|
194
|
+
stored_value[:ip] = value
|
195
|
+
elsif (field == "Date")
|
196
|
+
stored_value[:date] = value
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
return stored_value
|
201
|
+
end
|
202
|
+
return nil
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: ruby-mailchimp
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2007-05-10 00:00:00 -04:00
|
8
|
+
summary: Allows programmatic subscription and unsubscription to MailChimp. Requires MailChimp account.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: cgbrown@projectlocker.com
|
12
|
+
homepage: http://www.projectlocker.com
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: mail_chimp
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- C. G. Brown
|
31
|
+
files:
|
32
|
+
- lib/mail_chimp.rb
|
33
|
+
test_files: []
|
34
|
+
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
extra_rdoc_files: []
|
38
|
+
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
requirements: []
|
44
|
+
|
45
|
+
dependencies:
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: actionmailer
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.3.2
|
54
|
+
version:
|