2

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

1

There are two things to consider here:

  1. How to approach sending to a list
  2. 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);
}

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.