<p>There are two things to consider here:</p>

<ol>
<li>How to approach sending to a list</li>
<li>How to approach populating a notification with dynamic information</li>
</ol>

<h3>Sending email to a list of users</h3>

<p>Sprout Email notifications use the default Craft email service. Each notification is sent to a single individual. To send to a list, what you'll need to do behind the scenes is loop through all the people in that list and send them each an email one by one.</p>

<h3>Sending notifications with dynamic information</h3>

<p>While Sprout Email uses the default email sending behavior in Craft, to send notifications programmatically you'll want to use the <code>sendNotifcation</code> method:</p>

<pre><code>craft()-&gt;sproutEmail_defaultMailer-&gt;sendNotification($campaign, $object);
</code></pre>

<p>The <code>$campaign</code> variable should be an instance of a <code>SproutEmail_CampaignModel</code> and can be created by retrieving the Notification you want to send by it's ID like so:</p>

<pre><code>craft()-&gt;sproutEmail_notifications-&gt;getNotificationById($id);
</code></pre>

<p>The <code>$object</code> is the data that will be processed in your template and any fields that accept dynamic variables such as the Subject, From Name, From Email, Reply To, and Recipients fields.</p>

<p>In your situation, it sounds like you are sending User-specific notifications and your template might be expecting a <code>UserModel</code> for any dynamic variables.</p>

<hr>

<p>A very rough starting point for how to approach the sending could be as follows:</p>

<pre><code>// Get all your users first

foreach ($users as $user) 
{
    // Get your Campaign (SproutEmail_CampaignModel)
    $campaign = craft()-&gt;sproutEmail_notifications-&gt;getNotificationById($campaignId);

    // Get your User (UserModel)
    // This step is only needed if $user is not already a UserModel
    $user = craft()-&gt;users-&gt;getUserById($userId);

    // Send your Notification
    craft()-&gt;sproutEmail_defaultMailer-&gt;sendNotification($campaign, $user);
}
</code></pre>

CC BY-SA 3.0
2016-03-19T00:14:10.157Z
2016-03-19T00:14:10.157Z
Ben Parizek
<p>I would like to send an Email to a dynamic list of recipients with <strong>SproutEmail</strong>.</p>

<p>I've created a Plugin that adds custom element actions in the users indexview that raise a custom SproutEmailEvent to send a Notification. The event works fine already, but I want to send the notification to the dynamically selected list of users.</p>

<p>Let's say I select 4 Users in my user indexview and click on my custom action "Send Reminder email", then all of the four selected users should receive the notification email.</p>

<p>Also of course, I would need to address the selected users in the email template, like "Dear {firstName} {lastName}"</p>

<p>I understand how I could do that with a single user, but how about a dynamic list of selected users?</p>

<p>Thanks for your help.</p>

<p>Robert</p>

<plugin-sproutemail>
Send Emails to dynamic list of recipients
# Send Emails to dynamic list of recipients

(Asked by: username_0 on 2016-03-07)

I would like to send an Email to a dynamic list of recipients with **SproutEmail**.

I've created a Plugin that adds custom element actions in the users indexview that raise a custom SproutEmailEvent to send a Notification. The event works fine already, but I want to send the notification to the dynamically selected list of users.

Let's say I select 4 Users in my user indexview and click on my custom action "Send Reminder email", then all of the four selected users should receive the notification email.

Also of course, I would need to address the selected users in the email template, like "Dear {firstName} {lastName}"

I understand how I could do that with a single user, but how about a dynamic list of selected users?

Thanks for your help.

Robert

**Tags:** plugin-sproutemail


---

## 1 answers

### Answer by username_1 on 2016-03-19. Score: 1

There are two things to consider here:

How to approach sending to a list
How to approach populating a notification with dynamic information

Sending email to a list of users
Sprout Email notifications use the default Craft email service. Each notification is sent to a single individual. To send to a list, what you'll need to do behind the scenes is loop through all the people in that list and send them each an email one by one.
Sending notifications with dynamic information
While Sprout Email uses the default email sending behavior in Craft, to send notifications programmatically you'll want to use the sendNotifcation method:
craft()->sproutEmail_defaultMailer->sendNotification($campaign, $object);

The $campaign variable should be an instance of a SproutEmail_CampaignModel and can be created by retrieving the Notification you want to send by it's ID like so:
craft()->sproutEmail_notifications->getNotificationById($id);

The $object is the data that will be processed in your template and any fields that accept dynamic variables such as the Subject, From Name, From Email, Reply To, and Recipients fields.
In your situation, it sounds like you are sending User-specific notifications and your template might be expecting a UserModel for any dynamic variables.

A very rough starting point for how to approach the sending could be as follows:
// Get all your users first

foreach ($users as $user) 
{
    // Get your Campaign (SproutEmail_CampaignModel)
    $campaign = craft()->sproutEmail_notifications->getNotificationById($campaignId);

    // Get your User (UserModel)
    // This step is only needed if $user is not already a UserModel
    $user = craft()->users->getUserById($userId);

    // Send your Notification
    craft()->sproutEmail_defaultMailer->sendNotification($campaign, $user);
}


---

