forked from lobbywatch/lobbywatch
/
mail_notification.php
109 lines (84 loc) · 3.07 KB
/
mail_notification.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
<?php
// Call: echo "Body" | php -f mail_notification.php -- -sSubj -ttest@lobbywatch.ch sql/zb_delta_20170606.sql sql/ws_parlament_ch_sync_20170601.sql
// require 'PHPMailerAutoload.php';
require_once dirname(__FILE__) . '/vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
require_once dirname(__FILE__) . '/public_html/settings/settings.php';
require_once dirname(__FILE__) . '/public_html/common/utils.php';
global $mail_connection;
$verbose = 0;
$quiet = false;
$mail = new PHPMailer;
// $mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $mail_connection['host']; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $mail_connection['username']; // SMTP username
$mail->Password = $mail_connection['password']; // SMTP password
$mail->SMTPSecure = $mail_connection['secure']; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $mail_connection['port']; // TCP port to connect to
$mail->setFrom('admin@lobbywatch.ch', 'Lobbywatch Script');
// $mail->addReplyTo('info@example.com', 'Information');
$mail->isHTML(true); // Set email format to HTML
$mail->CharSet = 'UTF-8';
// $mail->ContentType = 'text/plain';
$options = getopt('s:v::ht:q',array('help'));
$argx = 0;
while (++$argx < $argc && preg_match('/^-/', $argv[$argx])); # (no loop body)
$arguments = array_slice($argv, $argx);
// var_dump($options);
if (isset($options['v'])) {
if ($options['v']) {
$verbose = $options['v'];
} else {
$verbose = 1;
}
}
if (isset($options['s'])) {
$mail->Subject = $options['s'];
}
if (isset($options['t'])) {
$emails = explode(',', $options['t']);
foreach($emails as $email) {
$mail->addAddress($email);
}
}
if (isset($options['q'])) {
$quiet = true;
$verbose = 0;
}
if (isset($options['h']) || isset($options['help'])) {
print("mail_notification [OPTIONS] ATTACHMENT1 ATTACHMENT2 ...
Reads body from stdin.
Options:
-s subject Subject
-t emails Comma separted email addresses
-v[level] Verbose, optional level, 1 = default
-q Quiet
-h, --help This help
");
exit(0);
}
$body = '';
while($f = fgets(STDIN)){
$body .= $f;
}
if ($verbose > 0) print("Body:\n" . $body);
// $mail->Body = "<pre>" . htmlspecialchars($body) . "</pre>";
$mail->Body = "<pre>$body</pre>";
$mail->AltBody = $body;
foreach($arguments as $argument) {
$mail->addAttachment($argument); // Add attachments
}
$emails = [];
foreach($mail->GetToAddresses() as $email) {
$emails[] = $email[0];
}
if (!$quiet) print("Sending to " . implode(", ", $emails) . " ...\n");
if(!$mail->send()) {
print('Message could not be sent.');
print('Mailer Error: ' . $mail->ErrorInfo);
exit(1);
} else if (!$quiet) {
print('Message has been sent');
}
print("\n");