This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
forked from johndbritton/mechanicalmooc
-
Notifications
You must be signed in to change notification settings - Fork 6
/
seq-email.rb
112 lines (97 loc) · 3.8 KB
/
seq-email.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
require 'rest_client'
require 'multimap'
require 'active_model'
require 'html2markdown'
$LOAD_PATH << '.'
require 'mooc'
class SequenceEmail
include ActiveModel::Validations
attr_accessor :sequence, :tags, :subject, :body
validates_presence_of :tags, :subject, :body, :sequence
def initialize(output_stream = $stdout)
@tags = []
@output_stream = output_stream
end
def send!
errors.clear
return errors unless valid?
if @sequence == "sequence_1"
individual_users = User.all :group_work => false, :round => 1
send_email_to_users( individual_users.collect{|u| u.email} , "bqtde")
send_email_to_groups( 2..170 )
elsif @sequence == "sequence_2"
sequence2_users = User.all :group_work => false, :round => 2
send_email_to_users( sequence2_users.collect{|u| u.email} , "br67o")
send_email_to_groups( 171..187 )
elsif @sequence == "sequence_3_all"
sequence3_users = User.all :round => 3, :group_work => false, :group_confirmation => false
send_email_to_users( sequence3_users.collect{|u| u.email} , "brmcg")
send_email_to_groups( 188..209 )
elsif @sequence == "sequence_3_groups"
send_email_to_groups( 188..209 )
elsif @sequence == "sequence_4"
sequence4_users = User.all :round => 4
send_email_to_users( sequence4_users.collect{|u| u.email} , "bsesr")
else
@output_stream.puts "Not a valid sequence"
return false
end
end
def send_email_to(email_address)
data = Multimap.new
data[:from] = "The Machine <the-machine@mechanicalmooc.org>"
data[:subject] = @subject
data["o:tag"] = @tags && @tags.map{|t| t.to_s }.join(" ")
data[:to] = email_address
data[:html] = @body
page = HTMLPage.new :contents => @body
data[:text] = page.markdown
RestClient.post("https://api:#{ENV['MAILGUN_API_KEY']}"\
"@api.mailgun.net/v2/mechanicalmooc.org/messages", data)
end
private
def send_email_to_users(email_addresses, campaign_id)
email_addresses.each do |email_address|
data = Multimap.new
data[:from] = "The Machine <the-machine@mechanicalmooc.org>"
data[:subject] = @subject
data["o:tag"] = @tags && @tags.map{|t| t.to_s }.join(" ")
data[:to] = email_address
data[:html] = @body
page = HTMLPage.new :contents => @body
data[:text] = page.markdown
# data["o:testmode"] = "true"
data["o:tag"] = "production"
data["o:campaign"] = campaign_id
data["o:tracking"] = "yes"
data["o:tracking-clicks"] = "yes"
data["o:tracking-opens"] = "yes"
@output_stream << "Sending email to " + data[:to].join(', ') + "... "
@output_stream << RestClient.post("https://api:#{ENV['MAILGUN_API_KEY']}"\
"@api.mailgun.net/v2/mechanicalmooc.org/messages", data)
@output_stream << "\n<br />"
end
end
def send_email_to_groups(group_numbers)
group_numbers.each do |group_number|
data = Multimap.new
data[:from] = "The Machine <the-machine@mechanicalmooc.org>"
data[:subject] = @subject
data[:to] = "python-#{group_number}@mechanicalmooc.org"
data[:html] = @body
page = HTMLPage.new :contents => @body
data[:text] = page.markdown
data["o:tag"] = "production"
# data["o:testmode"] = "true"
data["o:tag"] = @tags && @tags.map{|t| t.to_s }.join(" ")
data["o:tracking"] = "yes"
data["o:tracking-clicks"] = "yes"
data["o:tracking-opens"] = "yes"
@output_stream << "Sending email to " + data[:to].join(', ') + "... "
@output_stream << RestClient.post("https://api:#{ENV['MAILGUN_API_KEY']}"\
"@api.mailgun.net/v2/mechanicalmooc.org/messages", data)
@output_stream << "\n<br />"
end
end
end