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

<pre class="lang-php prettyprint-override"><code>$mailer = SproutBaseEmail::$app-&gt;mailers-&gt;getMailerByName(DefaultMailer::class);
$notificationEmail = Craft::$app-&gt;elements-&gt;getElementById(123);
$mailer-&gt;sendNotificationEmail($notificationEmail);
</code></pre>

<p>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.</p>

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

<pre><code>Hello {firstName}

OR

Hello {{ object.firstName }}
</code></pre>

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

<pre class="lang-php prettyprint-override"><code>$mailer = SproutBaseEmail::$app-&gt;mailers-&gt;getMailerByName(DefaultMailer::class);
$notificationEmail = Craft::$app-&gt;elements-&gt;getElementById(123);
$notificationEmail-&gt;setEventObject([
  'firstName' =&gt; 'Mr. Smithyyy'
]);
$mailer-&gt;sendNotificationEmail($notificationEmail);
</code></pre>

CC BY-SA 4.0
2019-12-18T19:28:39.450Z
2019-12-18T19:28:39.450Z
Ben Parizek
<p>I'm trying to send a sprout email from my plugin like so:</p>

<pre class="lang-php prettyprint-override"><code>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-&gt;mailers-&gt;getMailerByName(DefaultMailer::class);

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

<p>It says that <code>sendNotificationEmail</code> expects an instance of <code>NotificationEmail</code> 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.</p>

<p>The only similar question I found was <a href="https://craftcms.stackexchange.com/questions/16210/how-to-build-a-transactional-email-platform-with-craft">this</a> but it's for Craft2.</p>

<plugin-sproutemail>
Send sprout email programmatically
# Send sprout email programmatically

(Asked by: username_0 on 2019-12-18)

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.

**Tags:** plugin-sproutemail

---

## 1 answers

### Answer by username_1 on 2019-12-18. Score: 3 | Accepted answer

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);


---

