Plugin sending emails to all users in an entries Users field I have a channel set up with front end submitted entries. I want a way for users to be able to subscribe themselves to receive an update whenever the entry (or it's related entries) are updated. I was thinking one way to so this so that it can be managed on the front end would be to add a Users field to the entry and let logged in users add themselves to it.

I have a simple notification service that I am using in a custom plugin that is sending emails to entry authors, but I'm not sure how, or what would be the best way to send emails to multiple users as per the above requirement.

(p.s I'm new and limited in my plugin knowledge!)

Any thoughts or suggestions. 

Thanks!
If you have questions or other issues feel free to ask.
Please don't be confused, my Plugins handle/name is "Test" because I only use it for these purposes.. So whenever you see the name "Test" you know you have to rename it

This is your plugins init function

/**
 * Called after the plugin class is instantiated; do any one-time initialization here such as hooks and events:
 * @return mixed
 */
public function init()
{
    parent::init();
    craft()-&gt;on('entries.onBeforeSaveEntry', function(Event $event)
    {
        // trigger only when entries are changed not when created
        if(!$event-&gt;params['isNewEntry']){
            /** @var EntryModel $entry */
            $entry = $event-&gt;params['entry'];
            // create your logic to check if it is the correct entry...
            // you should check for your section here otherwise you'll send a mail for all updates^^
            // I just used the handle pages in my test system
            if($entry-&gt;getSection()-&gt;handle === "pages"){
                // next: fetch all user related to the entry...
                // since I don't know your entire fields, you have to implement the logic
                // to fetch users from entries that are related to the entry yourself
                // if you need help just ask
                $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
                $criteria-&gt;relatedTo($entry);

                /** @var UserModel $user */
                foreach( $criteria as $user){
                    // create a new entry in the database
                    $email = new Test_EmailRecord();
                    $email-&gt;entryId = $entry-&gt;id;
                    $email-&gt;email = $user-&gt;email;
                    $email-&gt;save();
                }
            }
        }

    });
}


And you have to create the following record

namespace Craft;


class Test_EmailRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'cron_mails';
    }

    protected function defineAttributes()
    {
        return array(
            // you could store the user Id here too, but this way
            // you can send the mail to non users too
            'email' =&gt; AttributeType::String,
            'entryId' =&gt; array(AttributeType::Number),
        );
    }

    // you could - if you want - create relations here..
    // https://craftcms.com/docs/plugins/records#defining-relations
    // but since it's only an example and not needed I skipped that part
}


And you need this in your controller

 /**
 * Cronjob to send emails... you can activate it every X hours
 * or once per day or whatever you want
 * when this function is trigger a specific number of mails will be send
 * their entries in the database will be deleted until there are no rows in the
 * table
 */
public function actionSendMails()
{
    // fetch mails with limit.. insert your limit here.. I would suggest to fetch 20-50
    // entries per trigger depending on how often it should run
    $emails = Test_EmailRecord::model()-&gt;findAllBySql('select * from craft_cron_mails LIMIT 1');

    if (is_array($emails) &amp;&amp; count($emails)) {
        foreach ($emails as $email) {
            /** @var UserModel $user */
            $user = craft()-&gt;users-&gt;getUserByEmail($email-&gt;email);

            // do your logic if no user is found..
            // of course you could store the user Id rather than the email in the database table
            // but this way you can send the mail to non users too if you like
            if (!$user) {
                // you can just remove that part
                // I just wanted to show you could create an array with defaults here
                // if the user is not registered
                $user = array(/* fill some placeholders for your twig template */);
            }

            // prepare templates, you can address each user personally with a hello Mr {{ user.name }}
            // the entry {{ entry.title }} was edited and such... 
            $message = craft()-&gt;templates-&gt;render('path/to/template.twig', array(
                'user' =&gt; $user,
                'entry' =&gt; craft()-&gt;elements-&gt;getElementById($email-&gt;entryId)
            ));
            if ($this-&gt;sendMail($message, "The post was edited", $user-&gt;email)) {
                //remove the column if the mail was send
                $email-&gt;delete();
            }

        }
    }
}

/**
 * This function sends the mail, it usually belongs to a service
 * but to make it easier I'll just leave it here
 *
 * @param $message
 * @param $subject
 * @param null $mail
 * @return bool
 */
public function sendMail($message, $subject, $mail = null)
{
    $errors = false;
    $adminEmail = craft()-&gt;systemSettings-&gt;getSetting('email', 'emailAddress');

    $email = new EmailModel();

    $email-&gt;fromEmail = $adminEmail;
    $email-&gt;replyTo = $adminEmail;
    $email-&gt;sender = $adminEmail;
    $email-&gt;fromName = craft()-&gt;getSiteName();
    $email-&gt;toEmail = $mail;
    $email-&gt;subject = $subject;
    $email-&gt;body = $message;

    if (!craft()-&gt;email-&gt;sendEmail($email)) {
        $errors = true;
    }

    return $errors;
}