Sending email to a subset of members

<%- if @topic_view.topic.tags.present? %>
<%= t 'js.tagging.tags' %>: <%- @topic_view.topic.tags.each do |t| %> <%= t %> <%- end %>
<% end %>

Silverstripe Version:
SS4.2.1

Question:
I’d like to add a button to the CMS (Security or Members admin) which enables the user to send a custom email to all members in the current found/filtered set.

For example, CMS user should be able to fill out a custom message field, then click the button to send welcome emails to each member with the member’s ID, link to login page, their username and the custom message.

Is there a module for this or some tips on how to implement this?

The Bulk Manager component of this module should let you accomplish most of this I think, with the caveat that you might have to manually select rows first, so not a 100% solution.

If anyone else has tips for how to accomplish this I’m interested too!

1 Like

@JonoM Have played around with Bulk Manager. The following code allows me to send a custom (currently hard-coded) email message to selected members (add to desired members modeladmin):

namespace App\Web;
use SilverStripe\Admin\ModelAdmin;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use Colymba\BulkManager\BulkManager;
use App\Web\BulkEmailHandler;
use App\Web\Guide;

class GuidesAdmin extends ModelAdmin {

  private static $menu_title = 'Guides';
  private static $url_segment = 'guides';

  private static $managed_models = array (
    Guide::class
	  );

  function getEditForm($id = null, $fields = null){

		$form = parent::getEditForm($id, $fields);

		$guidegrid = $form->Fields()->fieldByName("App-Web-Guide");

		// allows bulk emailing to selected members
		if($guidegrid){
			$config = $guidegrid->getConfig()->addComponent(new BulkManager());
			$config->getComponentByType('Colymba\\BulkManager\\BulkManager')
				->addBulkAction(BulkEmailHandler::class)
				->removeBulkAction('Colymba\BulkManager\BulkAction\DeleteHandler')
				->removeBulkAction('Colymba\BulkManager\BulkAction\UnlinkHandler')
				->removeBulkAction('Colymba\BulkManager\BulkAction\EditHandler');
		}
		
		return $form;

}
}

Then the custom bulk action - BulkEmailHandler.php

namespace App\Web;

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Email\Email;
use SilverStripe\SiteConfig\SiteConfig;
use Exception;

use Colymba\BulkManager\BulkAction\Handler;
use Colymba\BulkTools\HTTPBulkToolsResponse;


class BulkEmailHandler extends Handler
{

    private static $url_segment = 'email';

    private static $allowed_actions = array('email');

    private static $url_handlers = array(
        '' => 'email',
    );

    protected $label = 'Send Login Details Email';

    protected $destructive = false;

    public function email(HTTPRequest $request)
    {
        $records = $this->getRecords();
        $response = new HTTPBulkToolsResponse(false, $this->gridField);
        try {
            foreach ($records as $record)
            {
                // send email to each selected member
                $sender = SiteConfig::current_site_config()->Email;
                $email = Email::create()
                    ->setTo($record->Email)
                    ->setReplyTo($sender)
                    ->setSubject("Member login details")
                    ->setBody("
                        <p>Hi {$record->FirstName},</p>
                        <p>To login, please visit: [website link]<br>Email: {$record->Email}</p>
                    ");
                $done = $email->send();

                if ($done){
                    $response->addSuccessRecord($record);
                } else {
                    $response->addFailedRecord($record, $done);
                }
            }
            $doneCount = count($response->getSuccessRecords());
            $failCount = count($response->getFailedRecords());
            /*$message = sprintf(
                'Published %1$d of %2$d records.',
                $doneCount,
                $doneCount + $failCount
            );*/
            $message = 'Emails sent to: '.implode(', ',$records->column('Email'));
            $response->setMessage($message);
        } catch (Exception $ex) {
            $response->setStatusCode(500);
            $response->setMessage($ex->getMessage());
        }
        
        return $response;
    }

}

It’s still away off yet, but maybe you have some ideas as to how to add a user-customisable email message?

The EditHandler class in that module provides an editing form prior to the save step, so if you study that file you should be able to put something similar together :slight_smile:

1 Like