-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresendFromCache.php
More file actions
89 lines (77 loc) · 3.32 KB
/
Copy pathresendFromCache.php
File metadata and controls
89 lines (77 loc) · 3.32 KB
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
<?php
// ============================================================================
// SiVote Home Sender — re-send one invitation from the local record
// ----------------------------------------------------------------------------
// If a voter didn't receive their email, re-send the SAME code to them using
// the SENSITIVE_MAPPED_CODES file written by sendInvitesToEmailsWithCache.php.
// This file is intentionally self-contained.
//
// Run from this folder:
// php resendFromCache.php --cache_file=SENSITIVE_MAPPED_CODES_....csv \
// --email=name@example.org --template=ballot.txt --subject="Ballot"
// ============================================================================
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
// --- 1. SMTP settings ---
if (!file_exists('config.ini')) {
die("Missing config.ini — copy config.ini.example to config.ini and fill it in." . PHP_EOL);
}
$config = parse_ini_file('config.ini');
// --- 2. Read and check the command-line options ---
$options = getopt('', ['cache_file:', 'email:', 'template:', 'subject:']);
if (
empty($options['cache_file'])
|| empty($options['email'])
|| empty($options['template'])
|| empty($options['subject'])
) {
die('Usage: php resendFromCache.php --cache_file=SENSITIVE_MAPPED_CODES_....csv --email=name@example.org --template=ballot.txt --subject="Ballot"' . PHP_EOL);
}
if (!file_exists($options['cache_file']) || !file_exists($options['template'])) {
die("Cache file or template does not exist." . PHP_EOL);
}
// --- 3. Load the local record (each line is "email,code,url") ---
$cacheData = array_map(fn($line) => str_getcsv($line, ',', '"', ''), file($options['cache_file']));
// --- 4. Find the voter's row and re-send their original code ---
$template = file_get_contents($options['template']);
$found = false;
foreach ($cacheData as $row) {
if (($row[0] ?? null) === $options['email']) {
$body = str_replace(['%%EMAIL%%', '%%CODE%%', '%%URL%%'], [$row[0], $row[1], $row[2]], $template);
if (sendEmail($config, $row[0], $options['subject'], $body)) {
echo "Re-sent to: {$row[0]}" . PHP_EOL;
}
$found = true;
break;
}
}
if (!$found) {
echo "No entry for {$options['email']} in the cache file." . PHP_EOL;
}
// --- Send a single email over the configured SMTP server (Amazon SES) ---
function sendEmail(array $config, string $to, string $subject, string $body): bool
{
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = $config['SES_HOST'];
$mail->Port = (int) $config['SES_PORT'];
$mail->SMTPAuth = true;
$mail->Username = $config['SMTP_USERNAME'];
$mail->Password = $config['SMTP_PASS'];
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // encrypted submission (port 587)
$mail->CharSet = 'UTF-8';
$mail->setFrom($config['SENDER'], $config['SENDER_NAME']);
$mail->addAddress($to);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = strip_tags($body); // plain-text version
$mail->send();
return true;
} catch (Exception $e) {
echo "NOT sent to {$to}: {$mail->ErrorInfo}" . PHP_EOL;
return false;
}
}