2016-08-26 12:43:45 +00:00
|
|
|
<?php
|
|
|
|
|
2019-09-05 16:16:10 +00:00
|
|
|
require_once('libphp-phpmailer/autoload.php');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
class Mail extends Vn\Lib\Method {
|
|
|
|
function run($db) {
|
|
|
|
$db->query('START TRANSACTION');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$mailer = new Vn\Web\Mailer($db);
|
|
|
|
$res = $db->query(
|
2019-05-07 15:06:40 +00:00
|
|
|
'SELECT * FROM vn.mail WHERE sent = 0
|
|
|
|
ORDER BY creationDate
|
|
|
|
LIMIT 20
|
|
|
|
FOR UPDATE'
|
|
|
|
);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2016-08-31 11:53:46 +00:00
|
|
|
$count = 0;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
while ($row = $res->fetch_object()) {
|
2016-08-26 12:43:45 +00:00
|
|
|
$sent = 1;
|
|
|
|
$status = 'OK';
|
|
|
|
|
|
|
|
try {
|
2019-05-07 15:06:40 +00:00
|
|
|
$mail = $mailer->createObject($row->sender, $row->body, $row->subject);
|
|
|
|
$mail->AddReplyTo($row->replyTo, $row->replyTo);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2019-05-07 15:06:40 +00:00
|
|
|
if (!empty($row->attachment)) {
|
|
|
|
$attachment = '/mnt/cluster/pdfs/'. $row->attachment;
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (file_exists($attachment))
|
|
|
|
$mail->AddAttachment($attachment, '');
|
2016-08-26 12:43:45 +00:00
|
|
|
else
|
2018-05-23 10:14:20 +00:00
|
|
|
throw new Exception("Attachment file could not be found: $attachment");
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!$mail->Send())
|
2019-05-07 15:06:40 +00:00
|
|
|
throw new Exception($mail->ErrorInfo);
|
2016-08-31 11:53:46 +00:00
|
|
|
|
|
|
|
$count++;
|
2018-05-23 11:09:55 +00:00
|
|
|
} catch (Exception $e) {
|
2016-08-26 12:43:45 +00:00
|
|
|
$sent = 2;
|
2018-05-23 10:14:20 +00:00
|
|
|
$status = $e->getMessage();
|
2019-05-07 15:06:40 +00:00
|
|
|
|
|
|
|
if ($row->replyTo) {
|
2019-05-08 10:47:07 +00:00
|
|
|
Vn\Lib\Locale::set('es');
|
|
|
|
$errorMsg =
|
|
|
|
'<p>'. s('Notification from IT department about problem.') .'</p>'
|
|
|
|
.'<p>'. s('If you have questions, resend this email to cau@verdnatura.es.') .'</p>'
|
|
|
|
.'<p style="color: gray">'. $status .'</p>';
|
|
|
|
|
2019-05-07 15:06:40 +00:00
|
|
|
$errorMail = $mailer->createObject($row->replyTo,
|
2019-05-08 10:47:07 +00:00
|
|
|
$errorMsg,
|
2019-05-07 15:06:40 +00:00
|
|
|
s('An automated message could not be delivered')
|
|
|
|
);
|
|
|
|
$errorMail->AddStringAttachment(
|
|
|
|
$mail->getSentMIMEMessage(),
|
|
|
|
'Undelivered Message',
|
|
|
|
'8bit',
|
|
|
|
'message/rfc822'
|
|
|
|
);
|
|
|
|
$errorMail->Send();
|
|
|
|
}
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 15:06:40 +00:00
|
|
|
$db->query('UPDATE vn.mail SET sent = #, status = # WHERE id = #',
|
2017-11-29 12:55:51 +00:00
|
|
|
[$sent, $status, $row->id]);
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
|
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";
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
}
|