-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMailer.php
54 lines (47 loc) · 1.83 KB
/
Mailer.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
<?php
class Mailer {
private $recipients;
private $message;
private $subject;
private $fromAddr;
private $addGreeting;
public function __construct($message, Array $recipients, $subject, $fromAddr, $addGreeting) {
$this->message = $message;
$this->recipients = $recipients;
$this->subject = $subject;
$this->fromAddr = $fromAddr;
$this->addGreeting = $addGreeting;
}
public function sendAll($pauseEvery = 5) {
$count = 1;
foreach($this->recipients as $toName => $toAddr) {
// Every 5 emails, wait for two seconds
if(!empty($pauseEvery) && $count%$pauseEvery == 0) {
echo "Waiting for a second...\n";
sleep(1);
}
// Now send the email
$this->sendEmail($toAddr, $toName);
// Increment counter
$count++;
}
}
private function sendEmail($toAddr, $toName = null) {
// Prepare name and subject
$toName = is_string($toName) ? $toName : 'Sir/Madam';
// Set headers
$headers = 'X-Mailer: php';
if(isset($this->fromAddr)) { $headers .= "\r\n" . 'From: ' . $this->fromAddr; }
// Compose body
$body = $this->message;
if($this->addGreeting) {
$body = 'Dear ' . $toName . ",\r\n\r\n" . $body;
}
// Send the email
if (mail($toAddr, $this->subject, $body, $headers)) {
echo('Message sent to ' . $toName . ': ' . $toAddr . "\n");
} else {
echo('Failed to send message to ' . $toName . ': ' . $toAddr . "\n");
}
}
}