Add option to send mail to account with Mailer

"Send Mail" link next to Email input in account editor
This commit is contained in:
slawkens 2021-01-04 21:34:52 +01:00
parent 62b485abf9
commit 611d6f505d
3 changed files with 63 additions and 48 deletions

View File

@ -353,8 +353,8 @@ else if (isset($_REQUEST['search'])) {
</div> </div>
<div class="form-group row"> <div class="form-group row">
<div class="col-12 col-sm-12 col-lg-6"> <div class="col-12 col-sm-12 col-lg-6">
<label for="email">Email:</label> <label for="email">Email:</label><?php echo (config('mail_enabled') ? ' (<a href="' . ADMIN_URL . '?p=mailer&mail_to=' . $account->getEMail() . '">Send Mail</a>)' : ''); ?>
<input type="text" class="form-control" id="email" name="email" autocomplete="off" maxlength="20" value="<?php echo $account->getEMail(); ?>"/> <input type="text" class="form-control" id="email" name="email" autocomplete="off" value="<?php echo $account->getEMail(); ?>"/>
</div> </div>
<?php if ($hasCoinsColumn): ?> <?php if ($hasCoinsColumn): ?>
<div class="col-12 col-sm-12 col-lg-6"> <div class="col-12 col-sm-12 col-lg-6">

View File

@ -15,55 +15,69 @@ if (!hasFlag(FLAG_CONTENT_MAILER) && !superAdmin()) {
return; return;
} }
if (!$config['mail_enabled']) { if (!config('mail_enabled')) {
echo 'Mail support disabled.'; echo 'Mail support disabled.';
return; return;
} }
$mail_content = isset($_POST['mail_content']) ? stripslashes($_POST['mail_content']) : NULL; $mail_to = isset($_REQUEST['mail_to']) ? stripslashes(trim($_REQUEST['mail_to'])) : null;
$mail_subject = isset($_POST['mail_subject']) ? stripslashes($_POST['mail_subject']) : NULL; $mail_subject = isset($_POST['mail_subject']) ? stripslashes($_POST['mail_subject']) : null;
$preview = isset($_REQUEST['preview']); $mail_content = isset($_POST['mail_content']) ? stripslashes($_POST['mail_content']) : null;
$preview_done = false; if (isset($_POST['submit'])) {
if ($preview) { if (empty($mail_subject)) {
if (!empty($mail_content) && !empty($mail_subject)) { warning('Please enter subject of the message.');
$preview_done = _mail($account_logged->getCustomField('email'), $mail_subject, $mail_content); }
if (!$preview_done) if (empty($mail_content)) {
error('Error while sending preview mail. More info can be found in system/logs/mailer-error.log'); warning('Please enter content of the message.');
} }
} }
if (!empty($mail_to)) {
if(!Validator::email($mail_to)) {
$twig->display('admin.mailer.html.twig', array( warning('E-Mail is invalid.');
'mail_subject' => $mail_subject, }
'mail_content' => $mail_content,
'preview_done' => $preview_done
));
if (empty($mail_content) || empty($mail_subject) || $preview)
return;
$success = 0;
$failed = 0;
$add = '';
if ($config['account_mail_verify']) {
note('Note: Sending only to users with verified E-Mail.');
$add = ' AND ' . $db->fieldName('email_verified') . ' = 1';
}
$query = $db->query('SELECT ' . $db->fieldName('email') . ' FROM ' . $db->tableName('accounts') . ' WHERE ' . $db->fieldName('email') . ' != ""' . $add);
foreach ($query as $email) {
if (_mail($email['email'], $mail_subject, $mail_content))
$success++;
else { else {
$failed++; if (!empty($mail_content) && !empty($mail_subject)) {
echo '<br />'; if (_mail($mail_to, $mail_subject, $mail_content)) {
error('An error occorred while sending email to <b>' . $email['email'] . '</b>. For Admin: More info can be found in system/logs/mailer-error.log'); success("Successfully mailed <strong>$mail_to</strong>");
}
else {
error("Error while sending mail to <strong>$mail_to</strong>. More info can be found in system/logs/mailer-error.log");
}
}
} }
} }
success('Mailing finished.'); if (!empty($mail_content) && !empty($mail_subject) && empty($mail_to)) {
success("$success emails delivered."); $success = 0;
warning("$failed emails failed."); $failed = 0;
$add = '';
if (config('account_mail_verify')) {
note('Note: Sending only to users with verified E-Mail.');
$add = ' AND `email_verified` = 1';
}
$query = $db->query('SELECT `email` FROM `accounts` WHERE `email` != ""' . $add);
foreach ($query as $email) {
if (_mail($email['email'], $mail_subject, $mail_content)) {
$success++;
}
else {
$failed++;
echo '<br />';
error('An error occorred while sending email to <b>' . $email['email'] . '</b>. For Admin: More info can be found in system/logs/mailer-error.log');
}
}
success('Mailing finished.');
success("$success emails delivered.");
warning("$failed emails failed.");
}
$twig->display('admin.mailer.html.twig', [
'mail_to' => $mail_to,
'mail_subject' => $mail_subject,
'mail_content' => $mail_content
]);

View File

@ -18,21 +18,22 @@
</div> </div>
<form method="post"> <form method="post">
<div class="card-body"> <div class="card-body">
<div class="form-group row">
<label for="mail_to">To: (enter email, or leave empty to all)</label>
<input class="form-control" type="text" id="mail_to" name="mail_to" value="{{ mail_to }}"/>
</div>
<div class="form-group row"> <div class="form-group row">
<label for="mail_subject">Subject:</label> <label for="mail_subject">Subject:</label>
<input class="form-control" type="text" id="mail_subject" name="mail_subject" <input class="form-control" type="text" id="mail_subject" name="mail_subject" value="{{ mail_subject }}" maxlength="30"/>
value="{{ mail_subject }}" size="30" maxlength="30"/>
</div> </div>
<div class="form-group row"> <div class="form-group row">
<label for="mail_content" class="control-label">Content:</label> <label for="mail_content" class="control-label">Content:</label>
<textarea id="mail_content" name="mail_content" style="width: 100%" class="tinymce">{{ mail_content }}</textarea> <textarea id="mail_content" name="mail_content" style="width: 100%" class="tinymce">{{ mail_content }}</textarea>
</div> </div>
<div class="form-group row">
<input type="checkbox" name="preview" id="preview" value="1"/><label for="category">Just send test email to me (preview)</label>{% if preview_done %} - <b>Done.</b>{% endif %}
</div>
</div> </div>
<div class="card-footer"> <div class="card-footer">
<button type="submit" class="btn btn-info"><i class="fas fa-share"></i> Send</button> <button type="submit" name="submit" class="btn btn-info"><i class="fas fa-share"></i> Send</button>
<a href="{{ constant('ADMIN_URL') }}?p=mailer" class="btn btn-danger float-right">Cancel</a>
</div> </div>
</form> </form>
</div> </div>