Looking to build a hybrid news service that publishes to a website but also sends an entry to a mailing list. Has anyone done anything similar?


1 Answer 1


You would need to attach such functionality to EVENT_AFTER_SAVE event in the custom php module.

The module can be built using plugin factory website. For attaching functionality to the event, you don't need to select any additional components.


You can find useful information about modules here:


Add these statements to use section in main module class:

use yii\base\Event;
use craft\elements\Entry;
use craft\events\ModelEvent;
use craft\helpers\ElementHelper;

Now you need to attach functionality to the event - add this code to modules main class, in init() method:

    function(ModelEvent $event) {

        // react only to entries from this section:
        $section_id = 1;

        // entry data
        $entry = $event->sender;

        // if it is not a draft
        if (ElementHelper::isDraftOrRevision($entry)) {

        // if entry has specific section
        if($entry->sectionId != $section_id){


This code will run when you save entry in a specific section. Now, you need to give ourselves some control.

We will make our code run only when we check a specific field in our entry. Create lightswitch field and add it to entry field layout. Thanks to the code below, an email will be sent only when entry is saved and lightswitch is checked. Lightswitch will be automatically de-checked after saving. Just remember that lightswitch field needs to be disabled by default to keep you from accidentally sending email when you save entry first time.

$lightswitchHandle = 'someField';
if($entry->getFieldValue($lightswitchHandle) !== true){

$entry->setFieldValue($lightswitchHandle, false);

Now we need to compose our email:

// compose email from entry content
$email_template = 'path_to_email_template.twig';
$email_html = Craft::$app->view->renderTemplate(
        'entry' => $entry,

This code will inject the contents of entry as entry variable into Twig template path_to_email_template.twig placed in templates directory and render email HTML. I suggest giving your email some nice base template - here are good examples:


Now what you will do with such email, is up to you. You could potentially send this email manually using Craft mailer class:

$email_recepients = ['[email protected]', '[email protected]'];
$email_subject = 'interesting content inside!';


Just try to avoid sending emails using server built-in sendmail functionality, this can lead to your ip/domain being marked as spam-sending. It's good to use some middleman services like mailgun.


You can attach Craft to mailgun SMTP using the application configuration file:


If you want to test how your emails behave, you can use a mock SMTP server like mailtrap:


An alternative to using SMTP might be mailchimp service or something like that - in that case, instead of using Craft mailer, you would use some dedicated PHP library to send email to service API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.