2016-08-26 12:43:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once ('libphp-phpmailer/PHPMailerAutoload.php');
|
|
|
|
|
|
|
|
class Mail extends Vn\Lib\Method
|
|
|
|
{
|
2016-09-23 22:47:34 +00:00
|
|
|
function run ($db)
|
2016-08-26 12:43:45 +00:00
|
|
|
{
|
2016-08-31 11:53:46 +00:00
|
|
|
$db->selectDb ('vn2008');
|
2016-08-26 12:43:45 +00:00
|
|
|
$db->query ('START TRANSACTION');
|
|
|
|
|
|
|
|
$conf = $db->getRow (
|
|
|
|
'SELECT host, port, secure, sender, sender_name, user, password
|
|
|
|
FROM hedera.mail_config'
|
|
|
|
);
|
|
|
|
$res = $db->query (
|
|
|
|
'SELECT * FROM mail WHERE sent = 0 ORDER BY DATE_ODBC DESC
|
|
|
|
LIMIT 20 FOR UPDATE');
|
|
|
|
|
2016-08-31 11:53:46 +00:00
|
|
|
$count = 0;
|
|
|
|
|
2016-08-26 12:43:45 +00:00
|
|
|
while ($row = $res->fetch_assoc ())
|
|
|
|
{
|
|
|
|
$sent = 1;
|
|
|
|
$status = 'OK';
|
|
|
|
|
|
|
|
try {
|
2016-09-19 06:40:18 +00:00
|
|
|
//if (!preg_match ('/^[\w\._%-]+@[\w\.-]+\.[A-Za-z]{2,4}$/', $row['to']))
|
|
|
|
// throw new Exception ('Destination mail has invalid sintax');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
$mail = new PHPMailer ();
|
|
|
|
$mail->isSMTP ();
|
|
|
|
$mail->Host = $conf['host'];
|
|
|
|
|
|
|
|
if (!empty ($conf['user']))
|
|
|
|
{
|
|
|
|
$mail->SMTPAuth = TRUE;
|
|
|
|
$mail->Username = $conf['user'];
|
|
|
|
$mail->Password = base64_decode ($conf['password']);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$mail->SMTPAuth = FALSE;
|
|
|
|
|
|
|
|
if ($conf['secure'])
|
|
|
|
{
|
|
|
|
$mail->SMTPSecure = 'ssl';
|
|
|
|
$mail->Port = 465;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mail->setFrom ($conf['sender'], $conf['sender_name']);
|
2016-10-13 15:07:48 +00:00
|
|
|
$mail->AddReplyTo ($row['reply_to'], $conf['sender_name']);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
if (strpos ($row['to'], ','))
|
|
|
|
$mailList = explode (',', $row['to']);
|
|
|
|
else
|
|
|
|
$mailList = explode (';', $row['to']);
|
|
|
|
|
|
|
|
foreach ($mailList as $mailTo)
|
|
|
|
$mail->AddAddress ($mailTo);
|
|
|
|
|
2017-03-02 10:01:29 +00:00
|
|
|
$mail->IsHTML (TRUE);
|
2016-08-26 12:43:45 +00:00
|
|
|
$mail->Subject = $row['subject'];
|
|
|
|
$mail->Body = ' '. $row['text'];
|
|
|
|
$mail->CharSet = 'UTF-8';
|
|
|
|
|
|
|
|
if (!empty ($row['path']))
|
|
|
|
{
|
2016-09-19 06:40:18 +00:00
|
|
|
$attachment = '/mnt/cluster/pdfs/'. $row['path'];
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
if (file_exists ($attachment))
|
|
|
|
$mail->AddAttachment ($attachment, '');
|
|
|
|
else
|
2016-09-01 11:42:11 +00:00
|
|
|
throw new Exception ("Attachment file could not be found: $attachment");
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$mail->Send ())
|
|
|
|
throw new Exception ('Send error: '.$mail->ErrorInfo);
|
2016-08-31 11:53:46 +00:00
|
|
|
|
|
|
|
$count++;
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
$sent = 2;
|
|
|
|
$status = $e->getMessage ();
|
|
|
|
}
|
|
|
|
|
|
|
|
$db->query ('UPDATE mail SET sent = #, error = # WHERE id = #',
|
|
|
|
[$sent, $status, $row['id']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$db->query ('COMMIT');
|
2016-08-31 11:53:46 +00:00
|
|
|
echo "Total $count mails sent\n";
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:45:10 +00:00
|
|
|
|