Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ryssbowh committed Jan 10, 2022
1 parent 8450632 commit 8fb5e9a
Show file tree
Hide file tree
Showing 34 changed files with 1,475 additions and 752 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# ryssbowh/craft-emails Changelog

## 1.1.0 - 09/01/2022

### Fixed
- Plugin would show up as disabled after install

### Changed
- Mailchimp caching can be set to -1 to skip caching
- Email log stored in filesystem instead of database
- Replace mailer with custom one
- Removed config driven attributes

### Added
- Email translations
- Email templates
- Email preview
- Translatable attachements

## 1.0.1 - 09/01/2022

### Fixed
Expand Down
62 changes: 34 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,34 @@ Define email shots to send to various users.

![List](/images/list.png)

Once this plugin is installed, you'll be able to change, for each email :
- It's subject
- It's body
Change the subject, body and attachements of Craft system emails and define new ones.
Emails config are saved in project config and will populate from an environment to another, the config includes :
- The email identifier (key)
- The email heading
- The template used for rendering the email, defaults to 'emails/template'
- Whether the email is plain text or html
- Redactor config for the body
- Some instructions for the body
- The email it's coming from, will default to system email config
- The name it's coming from, will default to system email config
- The reply to address, will default to system email config
- List of email addresses to CC
- List of email addresses to BCC
- Add assets as attachements
- Have a different redactor config
- Wether the logs of sent emails shoud be saved

![Content](/images/content.png)

### Logging
![Config](/images/config.png)

You can choose to save a log of each email sent in database for future reference. The logs are compressed in database so not to take too much space.
Enabling the logs will allow you to resend previously sent emails.
Emails content (subject, body and attachements) is translatable, and will not be saved in project config.
There will be one translation available per site language, define new sites with different languages to access translations. Your emails translation will be used automatically when sending emails from different websites.
Emails can be previewed.

![Logs](/images/logs.png)
![Content](/images/content.png)

### Project Config
### Logs

You can choose which email parameter(s) are considered as config. The chosen ones will be included in the project config and will be modified from an environment to another, possible parameters are:
- Heading
- From
- Name from
- Reply to email
- Cc
- Bcc
- Subject
- Body
- Attachements
Enabling logs will allow resending the emails

![Config](/images/config.png)
![Logs](/images/logs.png)

### Sending emails

Expand All @@ -59,6 +53,18 @@ Craft::$app->getMailer()

The email will automatically be modified according to its config before it's sent.

You may modify variables passed to any email using this event :

```
Event::on(
Mailer::class,
Mailer::EVENT_BEFORE_PREP,
function (MailEvent $e) {
$e->message->variables['name'] = 'value';
}
);
```

## Email shots

Define a new email shots using the dashboard :
Expand All @@ -79,7 +85,7 @@ Email shots can use the queue to send emails, using the queue present advantages

### Logs

See what email shot has been sent to which email with the logs :
See what email shot has been sent to which emails and by whom with the logs :

![Shot logs](/images/shot-logs.png)

Expand All @@ -101,7 +107,7 @@ Email sources must implement `EmailSourceInterface`. Exceptions will be thrown w

### Variables

If the email you send with a shot expects bespoke variables, you will need to define them manually before the shot is sent :
You can define variables manually before the shot is sent, they will be passed to the email :

```
Event::on(
Expand All @@ -125,14 +131,14 @@ Send an email shot :

## Permissions

7 new permissions :
8 new permissions :

- Access Emails (under General)
- Add and delete email templates
- Modify emails content
- Modify emails config
- See emails logs (applies to shot logs as well)
- Delete emails logs (applies to shot logs as well)
- See logs (emails & shots)
- Delete logs (emails & shots)
- Manage email shots
- Send emails

Expand Down
Binary file modified images/content.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 72 additions & 19 deletions src/Emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,38 @@
use Craft;
use Ryssbowh\CraftEmails\Events\RegisterEmailSourcesEvent;
use Ryssbowh\CraftEmails\Models\Settings;
use Ryssbowh\CraftEmails\Services\AttachementsService;
use Ryssbowh\CraftEmails\Services\EmailShotsService;
use Ryssbowh\CraftEmails\Services\EmailSourceService;
use Ryssbowh\CraftEmails\Services\EmailerService;
use Ryssbowh\CraftEmails\Services\EmailsService;
use Ryssbowh\CraftEmails\Services\EmailsVariable;
use Ryssbowh\CraftEmails\Services\MailchimpService;
use Ryssbowh\CraftEmails\emailSources\AllUsersEmailSource;
use Ryssbowh\CraftEmails\emailSources\MailchimpEmailSource;
use Ryssbowh\CraftEmails\emailSources\UserGroupEmailSource;
use craft\base\Plugin;
use craft\db\Table;
use craft\events\DefineBehaviorsEvent;
use craft\events\RebuildConfigEvent;
use craft\events\RegisterCacheOptionsEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterEmailMessagesEvent;
use craft\events\RegisterTemplateRootsEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\events\RegisterUserPermissionsEvent;
use craft\helpers\App;
use craft\mail\Mailer;
use craft\models\SystemMessage;
use craft\records\Site;
use craft\services\ProjectConfig;
use craft\services\SystemMessages;
use craft\services\UserPermissions;
use craft\services\Utilities;
use craft\utilities\ClearCaches;
use craft\utilities\SystemMessages as SystemMessagesUtility;
use craft\web\UrlManager;
use craft\web\View;
use craft\web\twig\variables\CraftVariable;
use yii\base\Event;
use yii\mail\BaseMailer;
Expand All @@ -41,7 +51,7 @@ class Emails extends Plugin
/**
* @inheritdoc
*/
public $schemaVersion = '1.0.0';
public $schemaVersion = '1.1.0';

/**
* @inheritdoc
Expand All @@ -66,16 +76,19 @@ public function init()
'emailSources' => EmailSourceService::class,
'emailShots' => EmailShotsService::class,
'mailchimp' => MailchimpService::class,
'attachements' => AttachementsService::class,
]);

$this->registerMailer();
$this->registerProjectConfig();
$this->registerSystemMessages();
$this->disableSystemMessages();
$this->registerEmailEvents();
$this->registerTwigVariables();
$this->registerPermissions();
$this->registerEmailSources();
$this->registerClearCacheEvent();
$this->registerSiteTemplates();
$this->registerSiteChange();

if (Craft::$app->request->getIsConsoleRequest()) {
$this->controllerNamespace = 'Ryssbowh\\CraftEmails\\console';
Expand All @@ -92,7 +105,7 @@ public function init()
public function registerTwigVariables()
{
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $e) {
$e->sender->set('emails', Emails::$plugin->emails);
$e->sender->set('emails', EmailsVariable::class);
});
}

Expand Down Expand Up @@ -121,6 +134,53 @@ public function getCpNavItem ()
return null;
}

/**
* Listen to site after save event, in case their language change
*/
protected function registerSiteChange()
{
Event::on(
Site::class,
Site::EVENT_BEFORE_UPDATE,
function (Event $e) {
if (!$e->sender->primary) {
return;
}
$oldLanguage = $e->sender->getOldAttribute('language');
$newLanguage = $e->sender->language;
if ($oldLanguage !== $newLanguage) {
Emails::$plugin->emails->updatePrimaryMessageLanguage($oldLanguage, $newLanguage);
}
}
);
}

/**
* Replace Craft mailer
*/
protected function registerMailer()
{
$config = App::mailerConfig();
$config['class'] = EmailerService::class;
\Craft::$app->setComponents([
'mailer' => \Craft::createObject($config)
]);
}

/**
* Registers front templates
*/
protected function registerSiteTemplates()
{
Event::on(
View::class,
View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS,
function (RegisterTemplateRootsEvent $event) {
$event->roots[''][] = __DIR__ . '/templates/site';
}
);
}

/**
* Registers Clear cache options
*/
Expand Down Expand Up @@ -182,19 +242,6 @@ protected function settingsHtml(): string
);
}

/**
* Events before and after an email is sent
*/
protected function registerEmailEvents()
{
Event::on(Mailer::class, Mailer::EVENT_BEFORE_PREP, function (Event $event) {
Emails::$plugin->emails->modifyMessage($event->message);
});
Event::on(BaseMailer::class, BaseMailer::EVENT_AFTER_SEND, function ($event) {
Emails::$plugin->emails->afterSent($event->message);
});
}

/**
* Disable Craft system messages
*/
Expand Down Expand Up @@ -233,10 +280,10 @@ function (RegisterUserPermissionsEvent $event) {
'label' => \Craft::t('emails', 'Modify emails config')
],
'seeEmailLogs' => [
'label' => \Craft::t('emails', 'See emails logs')
'label' => \Craft::t('emails', 'See logs')
],
'deleteEmailLogs' => [
'label' => \Craft::t('emails', 'Delete emails logs')
'label' => \Craft::t('emails', 'Delete logs')
],
'sendEmails' => [
'label' => \Craft::t('emails', 'Send emails')
Expand All @@ -255,7 +302,7 @@ function (RegisterUserPermissionsEvent $event) {
protected function registerSystemMessages()
{
Event::on(SystemMessages::class, SystemMessages::EVENT_REGISTER_MESSAGES, function(RegisterEmailMessagesEvent $event) {
$event->messages = Emails::$plugin->emails->replaceSystemMessages($event->messages);
$event->messages = Emails::$plugin->emails->getAllSystemMessages($event->messages);
});
}

Expand Down Expand Up @@ -283,12 +330,14 @@ protected function registerCpRoutes()
$event->rules = array_merge($event->rules, [
'emails' => 'emails/cp-emails',
'emails/list' => 'emails/cp-emails',
'emails/preview/<id:\d+>/<langId>' => 'emails/cp-emails/preview',
'emails/shots' => 'emails/cp-shots',
'emails/shots/add' => 'emails/cp-shots/add-shot',
'emails/shots/edit/<id:\d>' => 'emails/cp-shots/edit-shot',
'emails/shots/logs/<id:\d>' => 'emails/cp-shots/logs',
'emails/quick-shot' => 'emails/cp-shots/quick-shot',
'emails/edit/<id:\d+>' => 'emails/cp-emails/edit-content',
'emails/edit/<id:\d+>/<langId>' => 'emails/cp-emails/edit-content',
'emails/logs/<emailId:\d+>' => 'emails/cp-emails/logs'
]);
if (\Craft::$app->config->getGeneral()->allowAdminChanges) {
Expand All @@ -306,6 +355,7 @@ protected function registerCpRoutes()
protected function afterInstall()
{
Emails::$plugin->emails->install();
\Craft::$app->plugins->enablePlugin('emails');
}

/**
Expand All @@ -314,5 +364,8 @@ protected function afterInstall()
protected function afterUninstall()
{
Craft::$app->getProjectConfig()->remove('emails');
\Craft::$app->getDb()->createCommand()
->delete(Table::SYSTEMMESSAGES)
->execute();
}
}
Loading

0 comments on commit 8fb5e9a

Please sign in to comment.