We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5430
    • 247 Posts
    I need to manually send an email to a specific user group. I really expected this to be far more simple than it's turned out to be, maybe one of you geniuses can help me out. I think I'm close, but I'm currently somehow sending duplicate emails on every send.

    I have an unpublished resource containing the following snippet:
    <?php
    $group = $modx->getObject('modUserGroup',array('name' => testing));
    $members = $group->getMany('UserGroupMembers');
    
    $message = $modx->getChunk('email-manuscripts');
    $modx->getService('mail', 'mail.modPHPMailer');
    $modx->mail->set(modMail::MAIL_BODY,$message);
    $modx->mail->set(modMail::MAIL_FROM,'[email protected]');
    $modx->mail->set(modMail::MAIL_FROM_NAME,'SSPM Manuscripts');
    $modx->mail->set(modMail::MAIL_SUBJECT,'Email test');
    foreach ($members as $member){
        $user = $modx->getObject('modUser',$member->get('member'));
        $modx->mail->address('to',$user->get('username'));
    }
    $modx->mail->address('reply-to','[email protected]');
    $modx->mail->setHTML(true);
    if (!$modx->mail->send()) {
        echo 'mail send failed';
    }else{
        echo 'mail sent';
    }
    $modx->mail->reset();

    When I view the resource, I see my success message, and email goes through, but there are two identical messages in the inbox of each account. Anybody see an obvious reason this would be happening? The final list will have something in the neighborhood of 40 emails, is this a bad way to go about it? I dont' need the overkill of a full newsletter system, just a simple bulk-email snippet for sending out a specific message to a specific user group once every couple of months.

    I'm running Revo 2.3.3 in MODX Cloud using smtp.gmail.com for outgoing.

    This question has been answered by claytonk. See the first response.

      • 3749
      • 24,544 Posts
      You might checkout Notify. I think it's designed for exactly your situation and once you set it up, it will be a lot more convenient for you. In addition, it has the option to use Mandrill to send the mail, which takes the load off your server.

      By default, Notify's Tpl chunks assume that you want to notify users of a new page or updated page, but it can be used for any kind of message to a user group or interest group (or the intersection of both).

      To answer your question, when I'm logged in and visit pages, I often see double hits in the log. I think it's Google visiting the page after you do. That would make the code execute twice, but I wouldn't think it would apply to an unpublished page. It's possible that you have a duplicate userGroupMember record.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 5430
        • 247 Posts
        I took a look at Notify, but it looked like the only way to execute the send was via a resource preview. With my method I can add a "send mail" button directly to the page the client is editing for distribution, so it's a lot simpler for then. There's no duplicate records, and I highly doubt it's a robot since it happens every test in exactly the same way. Very odd. I've run across a number of threads on phpMailer sending dups but nobody seems to have a solution. I thought it was that I was using the send command inside an if statement, but that was false hope.

        Annoying to say the least. Don't think it's MODX though, looking more and more like it's a phpMailer issue, possibly a phpMailer/Gmail issue.
        • discuss.answer
          • 5430
          • 247 Posts
          Just to close this out, I discovered Chrome was hitting the page twice for whatever reason. A session variable check to prevent a double request resolved the problem.
            • 24374
            • 322 Posts
            rainbowtiger Reply #5, 9 years ago
            You might check to make sure there are no hits producing unfound errors. I've seen this force a double (or even triple) load of a page. You can use the WebDeveloper tools in Firefox (Chrome may have an equivalent) and look at the Logging screen under Console to check every single page element as it loads and see if any image, script, stylesheet, etc. is not found.
            • Mark Hamstra Reply #6, 9 years ago
              $group = $modx->getObject('modUserGroup',array('name' => testing));

              should be
              $group = $modx->getObject('modUserGroup',array('name' => 'testing'));


              You also don't seem to be passing any actual email addresses? Just the username?

              foreach ($members as $member){
                  $user = $modx->getObject('modUser',$member->get('member'));
                  $modx->mail->address('to',$user->get('username'));
              }

              should probably be
              foreach ($members as $member){
                  $profile = $modx->getObject('modUserProfile', array('internalKey' => $member->get('member'));
                  $modx->mail->address('to',$profile->get('email'));
              }
              
                Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                • 3749
                • 24,544 Posts
                Quote from: claytonk at May 17, 2015, 11:39 PM
                I took a look at Notify, but it looked like the only way to execute the send was via a resource preview.

                No, Notify launches from the Create/Edit Resource panel. There's a button on the TV tab that says "Launch Notify. I use it all the time to send out a quick messages to members of a user group, and you can create several different Tpls to choose from for the message (or use a blank one).
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 5430
                  • 247 Posts
                  Quote from: BobRay at May 19, 2015, 10:11 PM
                  Quote from: claytonk at May 17, 2015, 11:39 PM
                  I took a look at Notify, but it looked like the only way to execute the send was via a resource preview.

                  No, Notify launches from the Create/Edit Resource panel. There's a button on the TV tab that says "Launch Notify. I use it all the time to send out a quick messages to members of a user group, and you can create several different Tpls to choose from for the message (or use a blank one).

                  Sorry Bob, I should have clarified, I didn't want a solution that was only accessible from the manager. I like the functionality of Notify, but for what I'm doing it seemed like jumping through too many hoops to edit the page in the manager and find the template variable, etc. As it stands they can simply look at the page on the front end and, if they're logged in, they get a button to send the email notice right there.

                  Notify is great for site managers/administrators, but this ain't for that.
                    • 5430
                    • 247 Posts
                    Quote from: markh at May 19, 2015, 09:21 AM
                    $group = $modx->getObject('modUserGroup',array('name' => testing));

                    should be
                    $group = $modx->getObject('modUserGroup',array('name' => 'testing'));

                    Yep, typo.

                    You also don't seem to be passing any actual email addresses? Just the username?
                    In this case the username is the email address.