3

I'm trying to send a sprout email from my plugin like so:

use barrelstrength\sproutbaseemail\mailers\DefaultMailer;
use barrelstrength\sproutbaseemail\SproutBaseEmail;
use barrelstrength\sproutbaseemail\elements\NotificationEmail;

use Craft;
use craft\base\Plugin;

use yii\base\Event;

class MyPlugin extends Plugin
{
    public function init()
    {
        $mailer = SproutBaseEmail::$app->mailers->getMailerByName(DefaultMailer::class);

        // How do I let it know what email to send?
        $mailer->sendNotificationEmail();
    }
}

It says that sendNotificationEmail expects an instance of NotificationEmail but I can't seem to find any documentation on how to reference the notification email created on the dashboard so it can be sent.

The only similar question I found was this but it's for Craft2.

3

At the most simple level, if you have a Notification Email configured with the event 'None', you can do the following:

$mailer = SproutBaseEmail::$app->mailers->getMailerByName(DefaultMailer::class);
$notificationEmail = Craft::$app->elements->getElementById(123);
$mailer->sendNotificationEmail($notificationEmail);

If you have an Event selected other than 'None' or want to use any Personalization in your email, you may need to also configure the notification to have the Event Object.

Let's say, your Notification Email body needs to use the person's name:

Hello {firstName}

OR

Hello {{ object.firstName }}

In that case, you can use the setEventObject method to set any variables you need available in your Email Template:

$mailer = SproutBaseEmail::$app->mailers->getMailerByName(DefaultMailer::class);
$notificationEmail = Craft::$app->elements->getElementById(123);
$notificationEmail->setEventObject([
  'firstName' => 'Mr. Smithyyy'
]);
$mailer->sendNotificationEmail($notificationEmail);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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