Skip to content

Commit

Permalink
Add option to send as HTML
Browse files Browse the repository at this point in the history
Fix default subject translation key in job
  • Loading branch information
clarkwinkelmann committed Apr 13, 2023
1 parent 839f991 commit 90a4147
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 23 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"type": "flarum-extension",
"license": "MIT",
"require": {
"php": "^8.0",
"flarum/core": "^1.7"
},
"replace": {
Expand Down
2 changes: 1 addition & 1 deletion js/dist/forum.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/forum.js.map

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions js/src/forum/components/EmailUserModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Vnode} from 'mithril';
import app from 'flarum/forum/app';
import Modal, {IInternalModalAttrs} from 'flarum/common/components/Modal';
import Button from 'flarum/common/components/Button';
import Switch from 'flarum/common/components/Switch';
import LoadingIndicator from 'flarum/common/components/LoadingIndicator';
import Group from 'flarum/common/models/Group';
import User from 'flarum/common/models/User';
Expand All @@ -25,6 +26,7 @@ export default class EmailUserModal extends Modal<EmailUserModalAttrs> {
recipients: Recipient[] = []
subject: string = ''
messageText: string = ''
asHtml: boolean = false
searchIndex: number = 0
navigator: KeyboardNavigatable = new KeyboardNavigatable()
filter: string = ''
Expand Down Expand Up @@ -204,6 +206,15 @@ export default class EmailUserModal extends Modal<EmailUserModalAttrs> {
disabled: this.sending,
}),
]),
m('.Form-group', [
Switch.component({
state: this.asHtml,
onchange: (value: boolean) => {
this.asHtml = value;
},
disabled: this.sending,
}, app.translator.trans('clarkwinkelmann-mailing.forum.modal_mail.as_html_label')),
]),
m('.Form-group', [
Button.component({
type: 'submit',
Expand Down Expand Up @@ -317,6 +328,7 @@ export default class EmailUserModal extends Modal<EmailUserModalAttrs> {
}),
subject: this.subject,
text: this.messageText,
asHtml: this.asHtml,
},
},
}).then(
Expand Down
1 change: 1 addition & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ clarkwinkelmann-mailing:
subject_label: Subject
default_subject: => clarkwinkelmann-mailing.ref.default_subject
message_label: Message
as_html_label: Send as HTML
submit_button: Send
modal_sent:
title_text: Mail Sent!
Expand Down
23 changes: 12 additions & 11 deletions src/Controllers/SendAdminEmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@

class SendAdminEmailController implements RequestHandlerInterface
{
protected $users;
protected $queue;

public function __construct(UserRepository $users, Queue $queue)
public function __construct(
protected UserRepository $users,
protected Queue $queue)
{
$this->users = $users;
$this->queue = $queue;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
$actor = RequestUtil::getActor($request);

$data = Arr::get($request->getParsedBody(), 'data', []);
$recipients = collect(Arr::get($data, 'recipients', []));
$data = (array)Arr::get($request->getParsedBody(), 'data');
$recipients = collect((array)Arr::get($data, 'recipients'));

$userIds = $recipients->filter(function ($model) {
return Arr::get($model, 'type') === 'users';
Expand Down Expand Up @@ -69,18 +66,22 @@ public function handle(ServerRequestInterface $request): ResponseInterface
});
}

$subject = (string)Arr::get($data, 'subject');
$text = (string)Arr::get($data, 'text');
$html = (bool)Arr::get($data, 'asHtml');

$recipientCount = 0;

$userQuery->chunk(50, function ($users) use ($data, &$recipientCount) {
$userQuery->chunk(50, function ($users) use ($subject, $text, $html, &$recipientCount) {
foreach ($users as $user) {
$this->queue->push(new SendMail($user->email, $data['subject'], $data['text']));
$this->queue->push(new SendMail($user->email, $subject, $text, $html));

$recipientCount++;
}
});

foreach ($emails as $email) {
$this->queue->push(new SendMail($email, $data['subject'], $data['text']));
$this->queue->push(new SendMail($email, $subject, $text, $html));

$recipientCount++;
}
Expand Down
19 changes: 9 additions & 10 deletions src/Jobs/SendMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ class SendMail implements ShouldQueue
{
use Queueable;

protected $email;
protected $subject;
protected $text;

public function __construct(string $email, string $subject, string $text)
public function __construct(
protected string $email,
protected string $subject,
protected string $text,
protected bool $html = false
)
{
$this->email = $email;
$this->subject = $subject;
$this->text = $text;
}

public function handle(SettingsRepositoryInterface $settings, Mailer $mailer, Translator $translator)
{
$mailer->send(['raw' => $this->text], [], function (Message $message) use ($settings, $translator) {
$mailer->send([], [], function (Message $message) use ($settings, $translator) {
$message->setBody($this->text, $this->html ? 'text/html' : 'text/plain');
$message->to($this->email);
$message->subject('[' . $settings->get('forum_title') . '] ' . ($this->subject !== '' ? $this->subject : $translator->get('kilowhat-mailing.email.default_subject')));
$message->subject('[' . $settings->get('forum_title') . '] ' . ($this->subject !== '' ? $this->subject : $translator->get('clarkwinkelmann-mailing.email.default_subject')));
});
}
}

0 comments on commit 90a4147

Please sign in to comment.