query('START TRANSACTION'); $mailer = new Vn\Web\Mailer($db); $res = $db->query( 'SELECT * FROM vn.mail WHERE sent = 0 ORDER BY creationDate LIMIT 20 FOR UPDATE' ); $count = 0; while ($row = $res->fetch_object()) { $sent = 1; $status = 'OK'; try { $mail = $mailer->createObject($row->sender, $row->body, $row->subject); $mail->AddReplyTo($row->replyTo, $row->replyTo); if (!empty($row->attachment)) { $attachment = '/mnt/cluster/pdfs/'. $row->attachment; if (file_exists($attachment)) $mail->AddAttachment($attachment, ''); else throw new Exception("Attachment file could not be found: $attachment"); } if (!$mail->Send()) throw new Exception($mail->ErrorInfo); $count++; } catch (Exception $e) { $sent = 2; $status = $e->getMessage(); if ($row->replyTo) { Vn\Lib\Locale::set('es'); $errorMsg = '

'. s('Notification from IT department about problem.') .'

' .'

'. s('If you have questions, resend this email to cau@verdnatura.es.') .'

' .'

'. $status .'

'; $errorMail = $mailer->createObject($row->replyTo, $errorMsg, s('An automated message could not be delivered') ); $errorMail->AddStringAttachment( $mail->getSentMIMEMessage(), 'Undelivered Message', '8bit', 'message/rfc822' ); $errorMail->Send(); } } $db->query('UPDATE vn.mail SET sent = #, status = # WHERE id = #', [$sent, $status, $row->id]); } $db->query('COMMIT'); echo "Total $count mails sent\n"; } }