-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend_emails.php
116 lines (92 loc) · 2.96 KB
/
send_emails.php
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
110
111
112
113
114
115
116
<?php
require './vendor/autoload.php';
use \Stampie\Adapter\Guzzle;
use \Stampie\Mailer\Postmark;
use \Guzzle\Service\Client;
use \Stampie\Message;
use \Stampie\Identity;
$config = require "./config.php";
$dry_run = false;
$override_email = false;
if (count($argv) > 1) {
if (filter_var($argv[1], FILTER_VALIDATE_EMAIL) !== false) {
$override_email = $argv[1];
} else {
$dry_run = true;
}
}
if ($dry_run) {
print "!! ASSUMING DRY RUN!\n";
} else if ($override_email) {
print "!! OVERRIDING TO-EMAIL TO {$override_email}\n";
}
$adapter = new Guzzle(new Client());
$mailer = new Postmark($adapter, $config['postmark']['token']);
abstract class LSPMessage extends Message {
protected $config;
public function setConfig($config) {
$this->config = $config;
}
public function getConfig() {
return $this->config;
}
}
class AcceptanceEmail extends LSPMessage {
public $vars = [
'[[name]]' => '',
'[[sessions]]' => '',
];
public function getFrom() { return $this->getConfig()['from']; }
public function getBcc() { return $this->getConfig()['bcc']; }
public function getSubject() { return $this->getConfig()['acceptance']['subject']; }
public function getText() {
return str_replace(
array_keys($this->vars),
array_values($this->vars),
$this->getConfig()['acceptance']['template']
);
}
}
class DenialEmail extends LSPMessage {
public $vars = [
'[[name]]' => '',
];
public function getFrom() { return $this->getConfig()['from']; }
public function getBcc() { return $this->getConfig()['bcc']; }
public function getSubject() { return $this->getConfig()['rejection']['subject']; }
public function getText() {
return str_replace(
array_keys($this->vars),
array_values($this->vars),
$this->getConfig()['rejection']['template']
);
}
}
function sendEmails($csvfile, $messageClass, $mailer, $config) {
global $dry_run;
global $override_email;
$f = fopen($csvfile, "r");
while($row = fgetcsv($f)) {
$name = implode(' ', array_map('trim', [$row[0], $row[1]]));
$email = trim($row[2]);
$sessions = "";
if (isset($row[3])) {
foreach (explode("|", $row[3]) as $session) {
$sessions .= " - $session\n";
}
}
$identity = new Identity($override_email ?: $email);
$identity->setName($name);
$message = new $messageClass($identity);
$message->setConfig($config);
$message->vars['[[name]]'] = $name;
$message->vars['[[sessions]]'] = $sessions;
echo " * Sending mail to $name <$email>" . ($override_email ? " (overridden)\n" : "\n");
if (!$dry_run) $mailer->send($message);
}
fclose($f);
}
echo "SENDING ACCEPTANCE EMAILS\n";
sendEmails("./acceptance.csv", "AcceptanceEmail", $mailer, $config);
echo "SENDING DENAIL EMAILS\n";
sendEmails("./rejection.csv", "DenialEmail", $mailer, $config);