forked from PHPMailer/PHPMailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailing_list.phps
49 lines (39 loc) · 1.95 KB
/
mailing_list.phps
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
<?php
error_reporting(E_STRICT|E_ALL);
date_default_timezone_set('Etc/UTC');
require '../class.phpmailer.php';
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$mail->IsSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Host = 'mail.example.com';
$mail->Port = 25;
$mail->Username = '[email protected]';
$mail->Password = 'yourpassword';
$mail->SetFrom('[email protected]', 'List manager');
$mail->AddReplyTo('[email protected]', 'List manager');
$mail->Subject = "PHPMailer Simple database mailing list test";
//Connect to the database and select the recipients from your mailing list that have not yet been sent to
//You'll need to alter this to match your database
$mysql = mysql_connect('localhost', 'username', 'password');
mysql_select_db('mydb', $mysql);
$result = mysql_query("SELECT full_name, email, photo FROM mailinglist WHERE sent = false", $mysql);
while ($row = mysql_fetch_array($result)) {
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML($body);
$mail->AddAddress($row['email'], $row['full_name']);
$mail->AddStringAttachment($row['photo'], 'YourPhoto.jpg'); //Assumes the image data is stored in the DB
if(!$mail->Send()) {
echo "Mailer Error (" . str_replace("@", "@", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
break; //Abandon sending
} else {
echo "Message sent to :" . $row['full_name'] . ' (' . str_replace("@", "@", $row['email']) . ')<br />';
//Mark it as sent in the DB
mysql_query("UPDATE mailinglist SET sent = true WHERE email = '".mysql_real_escape_string($row['email'], $mysql)."'");
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}