S2OJ/web/app/models/UOJMail.php

110 lines
14 KiB
PHP
Raw Normal View History

2016-07-18 16:39:37 +00:00
<?php
2022-10-09 23:35:48 +00:00
use PHPMailer\PHPMailer\PHPMailer;
2016-07-18 16:39:37 +00:00
class UOJMail {
public static function noreply() {
2023-02-13 12:29:32 +00:00
$mailer = new PHPMailer();
2016-07-18 16:39:37 +00:00
$mailer->isSMTP();
$mailer->Host = UOJConfig::$data['mail']['noreply']['host'];
$mailer->Port = UOJConfig::$data['mail']['noreply']['port'];
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = UOJConfig::$data['mail']['noreply']['secure'];
$mailer->Username = UOJConfig::$data['mail']['noreply']['username'];
$mailer->Password = UOJConfig::$data['mail']['noreply']['password'];
2023-02-13 12:29:32 +00:00
$mailer->setFrom(UOJConfig::$data['mail']['noreply']['username'], UOJConfig::$data['profile']['oj-name-short']);
2023-03-11 01:43:08 +00:00
$mailer->addCC(UOJConfig::$data['mail']['noreply']['username'], UOJConfig::$data['profile']['oj-name-short']);
2016-07-18 16:39:37 +00:00
$mailer->CharSet = "utf-8";
$mailer->Encoding = "base64";
return $mailer;
}
2023-02-13 12:29:32 +00:00
public static function cronSendEmail() {
$emails = DB::selectAll([
"select * from emails",
"where", DB::land([
["created_at", ">=", DB::raw("addtime(now(), '-24:00:00')")],
"send_time" => null,
]),
"order by priority desc",
]);
$oj_name = UOJConfig::$data['profile']['oj-name'];
$oj_name_short = UOJConfig::$data['profile']['oj-name-short'];
$oj_url = HTML::url('/');
2023-02-13 23:05:47 +00:00
$oj_email_address = UOJConfig::$data['mail']['noreply']['username'];
2023-02-13 12:29:32 +00:00
foreach ($emails as $email) {
$user = UOJUser::query($email['receiver']);
$name = $user['username'];
if ($user['realname']) {
$name .= ' (' . $user['realname'] . ')';
}
if ($user['email']) {
$mailer = UOJMail::noreply();
$mailer->addAddress($user['email'], $user['username']);
$mailer->Subject = $email['subject'];
$mailer->msgHTML(<<<EOD
<base target="_blank" />
<div style="padding: 48px; margin: 60px auto 60px auto; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.15), inset 0px 0px 1px rgba(0, 0, 0, 0.5); max-width: 700px">
<div style="display: block">
2023-03-11 01:43:08 +00:00
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAACXBIWXMAAAsTAAALEwEAmpwYAAAGUmlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNi4wLWMwMDIgNzkuMTY0NDYwLCAyMDIwLzA1LzEyLTE2OjA0OjE3ICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdEV2dD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlRXZlbnQjIiB4bWxuczpwaG90b3Nob3A9Imh0dHA6Ly9ucy5hZG9iZS5jb20vcGhvdG9zaG9wLzEuMC8iIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcvZGMvZWxlbWVudHMvMS4xLyIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgMjEuMiAoTWFjaW50b3NoKSIgeG1wOkNyZWF0ZURhdGU9IjIwMjAtMDgtMTlUMjE6NTk6MjkrMDg6MDAiIHhtcDpNZXRhZGF0YURhdGU9IjIwMjAtMD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ho6OH/MT1FpHr9UNSxaBM0fwmY1C9EcVvAFqTIovH2sixvDGsMhlf4DLVxrM8v1ATfO+g5usFnZ19bPxUYDNHVz74iPV/whXm7s4mFVo9phESYogCEqrrKNOR0BOrwWUjleXczW1dVgt2AaHOPLTV38YtSPwWBg96iXs53W6KQMWMxwaoB/WLeALS4bOK2sspplcnTZwO2HinyUsnz+uHER1gInoYZOfmozCxF1HXLsMOxhsc3MW/4QHznWyWNAcXEOv/UG0RSFC4+2sy+ic0NNCTtVTcRiJMaNLGLORoCicFdYpctu5mbgtXea0F96n6A3gK04lzf6Rrky38nB0jz2uf2iKQVVCIahspBdDivUdbA6qNJ00Sq+ZDWDNwilubBlOdeFVKqBrRUFUFnI4ZAG/pAQcsQL5fm8V1XE0d5RrtJ1zCYjh5p7+OaJHv49ouOpLOSJhg6+3++GQBg6BqV+szG7dJkzBqgaS/KdfNtl4772QT424oFQGMPOOu7rG2XpZWsxLCnnleVVfK+2nJaIDgYF1tbwanUx3993nIO9wxzef5LuigJ+culqKs+qYPs156A4LAy8WU/jWw38/s166tdU88Oza3nSaBD1taaEjvOWcseSCl66fC2K0cC+XfXc7Q9RazZR824zX9YiDOa72NvUxde8QRlhBiXrAyCrIigfWAgcA/xhjbE8B65QmEt1XXRrBenlY34qinPQ3zvBA/lODl21kUW9w9SqOs6qQg6f7OX/NHWzNtcB3cMUvtXAyxet5NLiXB4wGbG+eoj/dfug0AVt/Sxv6edzZ9fy+XkFPBTR0SsLqWvp47NuH5XrFnB/cw+rDAaxOUDa0jHIlrJ8uvvGWLGwFIIqVmAJ0AkMZ4tI2WBACXALEAE04HqgTYtQU+Dk3MExxrxBMap0XUSNwwq7G3jzYCsXOq0QDLNiUy13eYMQUuHUADcritgKLhs0dPCx0lzu+cgy/u2VA/y8rZ+FeY5xH
2023-02-13 23:05:47 +00:00
<div style="font-size: 24px; font-weight: bold; display: inline-block; vertical-align: baseline; margin-left: 6px; line-height: 1">{$oj_name_short}</div>
2023-02-13 12:29:32 +00:00
</div>
<hr />
2023-03-11 01:43:08 +00:00
<h1 style="margin: 20px auto;"><center>{$email['subject']}</center></h1>
2023-02-13 12:29:32 +00:00
<div style="font-size: 18px">{$name} 您好,</div>
<br />
<div>
{$email['content']}
</div>
<br />
<br />
2023-03-11 01:43:08 +00:00
<div style="text-align: right; margin-bottom: 16px;">
2023-02-13 12:29:32 +00:00
<a href="{$oj_url}">{$oj_name}</a>
2023-02-13 23:05:47 +00:00
<br />
{$email['created_at']}
2023-02-13 12:29:32 +00:00
</div>
<hr />
<div style="font-size: 12px; color: grey; text-align: center;">
本邮件由系统自动发送,请勿回复。
2023-02-13 23:05:47 +00:00
<br />
2023-03-11 01:43:08 +00:00
您之所以收到本邮件,是因为您是 {$oj_name} 的用户。
<br />
若本邮件没有出现在正常收件箱内,请将我们的发信邮箱地址 {$oj_email_address} 添加到您的邮箱白名单内,以免错过重要通知。
2023-02-13 12:29:32 +00:00
</div>
</div>
EOD);
$res = retry_loop(function () use (&$mailer) {
$res = $mailer->send();
if ($res) return true;
UOJLog::error($mailer->ErrorInfo);
return false;
});
if ($res) {
DB::update("update emails set send_time = now() where id = {$email['id']}");
echo '[UOJMail::cronSendEmail] ID: ' . $email['id'] . ' sent.' . "\n";
}
} else {
DB::update("update emails set send_time = now() where id = {$email['id']}");
echo '[UOJMail::cronSendEmail] ID: ' . $email['id'] . ' - empty email address.' . "\n";
2023-02-13 12:29:32 +00:00
}
}
echo '[UOJMail::cronSendEmail] Done.' . "\n";
}
2016-07-18 16:39:37 +00:00
}