We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49604
    • 3 Posts
    Hi all,

    I've just migrated a friend's website from DotNetNuke to ModX and, after adding the 50-odd users am now trying to send a mass email to them letting them know how to reset their auto-generated passwords. The snippet works great and sends out the emails (I'm sending all emails temporarily to my own email address in order to validate they are being created correctly) but it appears that modPHPMailer is creating duplicate emails, as I get two of every email in my Inbox.

    Has anyone ever seen this behavior? I'm using MODX Revolution 2.3.2-pl (traditional).

    My snippet content is below if you are interested. I've purposely obfuscated all sensitive information using COMPANY and NAME as substitutes for the real thing, so the content may look a bit weird in places...

    <?php
    // create PDO query
    $c = $modx->newQuery('modUser');
    $c->leftJoin('modUserProfile','Profile');
     
    // select columns of interest...
    $c->select(array(
        'modUser.*',
        'Profile.fullname',
        'Profile.email'
    ));
     
    $users = $modx->getCollection("modUser",$c);
    
    $emailed = '';
    
    foreach($users as $user){
        $userArray = $user->toArray(); 
        $name = $userArray['fullname'];
        $email = $userArray['email'];
        $username = $userArray['username'];
        $message = "Hello " . $name . ",<br/><br/>The COMPANY website has undergone a recent update which required that we create a new user profile for you.  To ensure your member profile is secure and that you continue having access to member-only content on our site, <span style=\"text-decoration: underline;\">we are asking that you reset your user password</span>.  To do this, please follow the instructions below:<br/><br/>
    
    1) Visit our \"Forgot Password\" page via the following URL:  http://COMPANY.com/forgot-password.html <br/>
    2) Enter either your COMPANY username: <strong>" . $username . "</strong>, or the email address we have on file for you: <strong>" . $email . "</strong> in the webform and click Submit. <br/>
    3) You will receive an email shortly afterwards with a link to reset your password and the temporary password assigned to you (<em><strong>make sure to check your Spam/Junk folder if you do not receive it within 10 minutes or so of submitting this form</strong></em>).  Copy the temporary password, click on the link in the email and then enter your COMPANY username: <strong>$username</strong> and the temporary password into the Login Form you are presented with. <br/>
    4) Once you are successfully logged in, you will be on the Member Page area.  Click the link on that page that says \"change your password\" (it will be one of the links in yellow text) and follow the steps to set your new, permanent password.<br/><br/>
    
    If you have any questions during this process, please get in touch with us at [email protected] or via phone at 555.555.5555.<br/><br/>
    
    We look forward to continuing to serve your reporting needs via our new website and hope you like our new look!<br/><br/>
    
    Sincerely,<br/><br/>
    
    NAME President, <strong>COMPANY</strong><br/>
    COMPANY.com<br/><br/>
    
    <img src=\"http://COMPANY.com/assets/template/images/logo_sm.png\"> ";
    
    $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,'COMPANY');
        $modx->mail->set(modMail::MAIL_SUBJECT,'Your COMPANY Login');
        $modx->mail->address('to','[email protected]');
        $modx->mail->address('reply-to','[email protected]');
        $modx->mail->setHTML(true);
        if (!$modx->mail->send()) {
            $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
        }
        if ($modx->mail->send()) 
            $emailed = $emailed . $email . "<br/>";
    
        $modx->mail->reset();
    }
    
    return $emailed;


    Thanks for any help you can provide,

    Sandman7
      • 3749
      • 24,544 Posts
      See if using this for the join helps:


      $c->leftJoin('modUserProfile','Profile','Profile.internalKey = modUser.id');
        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
        • 49604
        • 3 Posts
        Quote from: BobRay at Mar 19, 2015, 04:16 PM
        See if using this for the join helps:


        $c->leftJoin('modUserProfile','Profile','Profile.internalKey = modUser.id');

        Hi Bob,

        Many thanks for jumping in to help me here. I tried your modified join and, unfortunately, got the same result on my end - duplicate emails. I'm now pretty convinced this may not have anything to do with the snippet or modPHPmailer at all as I added a counter variable to the snippet to track the number of emails generated and it always reflects the correct (non-duplicated) number. So it would seem that the issue lies outside of the ModX application.

        I'm now looking deeper into my friend's webhost settings as my current theory is that the process of porting over his email MX records from the Windows account he had to the new Linux account may be causing dupe emails to be generated as there may be two email accounts configured instead of just one.

        Then again, I'm just grasping at straws here, so who knows... smiley

        Sandman7
          • 3749
          • 24,544 Posts
          I suspected that myself. I don't see any way that code could send two emails per user. Still, it's good form to specify the fields to join on.
            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
            Was just having a similar problem. For reasons beyond my small mind, sending mail as if ($modx->mail->send()) causes it to run twice. If you remove the if statement and use $modx->mail->send() on it's own, it only sends one email.
              • 5430
              • 247 Posts
              ...or not. Looks like it didn't solve the problem after all, still getting the duplicate. Grrrrr.
                • 5430
                • 247 Posts
                Turns out, in my case, the browser was double-tapping the page. A session variable check fixed it.