hedera-web/rest/misc/mail.php

51 lines
1.2 KiB
PHP
Raw Normal View History

<?php
2018-05-23 10:14:20 +00:00
require_once('libphp-phpmailer/PHPMailerAutoload.php');
2018-05-23 10:14:20 +00:00
class Mail extends Vn\Lib\Method {
function run($db) {
$db->selectDb('vn2008');
$db->query('START TRANSACTION');
2018-05-23 10:14:20 +00:00
$mailer = new Vn\Web\Mailer($db);
$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;
2018-05-23 10:14:20 +00:00
while ($row = $res->fetch_object()) {
$sent = 1;
$status = 'OK';
try {
2018-05-23 10:14:20 +00:00
$mail = $mailer->createObject($row->to, $row->text, $row->subject);
$mail->AddReplyTo($row->reply_to, $row->reply_to);
2018-05-23 10:14:20 +00:00
if (!empty($row->path)) {
$attachment = '/mnt/cluster/pdfs/'. $row->path;
2018-05-23 10:14:20 +00:00
if (file_exists($attachment))
$mail->AddAttachment($attachment, '');
else
2018-05-23 10:14:20 +00:00
throw new Exception("Attachment file could not be found: $attachment");
}
2018-05-23 10:14:20 +00:00
if (!$mail->Send())
throw new Exception('Send error: '.$mail->ErrorInfo);
2016-08-31 11:53:46 +00:00
$count++;
2018-05-23 11:09:55 +00:00
} catch (Exception $e) {
$sent = 2;
2018-05-23 10:14:20 +00:00
$status = $e->getMessage();
}
2018-05-23 10:14:20 +00:00
$db->query('UPDATE mail SET sent = #, error = # WHERE id = #',
2017-11-29 12:55:51 +00:00
[$sent, $status, $row->id]);
}
2018-05-23 10:14:20 +00:00
$db->query('COMMIT');
2016-08-31 11:53:46 +00:00
echo "Total $count mails sent\n";
}
}