We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22295
    • 153 Posts
    Have there been recent changes to modMail/phpmailer?
    Notification script uses this function to send multiple emails, but it only send the first one then fails on all the rest.
    strangely, if i comment out the modx->mail->reset() it works just fine..



    public function sendEmail($email,$name,$subject,$properties = array()) {
    	if (empty($properties['tpl'])) $properties['tpl'] = 'notification-post';
    	if (empty($properties['tplType'])) $properties['tplType'] = 'modChunk';
    
    	$this->modx->getService('mail', 'mail.modPHPMailer');
    	
    	$msg = $this->getChunk($properties['tpl'],$properties,$properties['tplType']);
    	$emailHtml = (!empty($properties['emailHtml']))? $properties['emailHtml'] : true;
    	$this->modx->mail->setHTML($emailHtml);
    	if ($emailHtml) $msg = nl2br($msg);	// fix plaintext comments for HTML
    
    	$this->modx->mail->set(modMail::MAIL_BODY, $msg);
    	$this->modx->mail->set(modMail::MAIL_FROM, $this->modx->getOption('emailsender'));
    	$this->modx->mail->set(modMail::MAIL_FROM_NAME, $this->modx->getOption('site_name'));
    	$this->modx->mail->set(modMail::MAIL_SENDER, $this->modx->getOption('emailsender'));
    	$this->modx->mail->set(modMail::MAIL_SUBJECT, $subject);
    	$this->modx->mail->address('to', $email, $name);
    	$this->modx->mail->address('reply-to', $this->modx->getOption('emailsender'));
    
    	$sent = $this->modx->mail->send();
    	$this->modx->mail->reset();
    
    	return $sent;
    }   

      • 3749
      • 24,544 Posts
      AFAIK, reset() clears all the recipients. Not sure why it’s being called there.

      public function reset($attributes= array()) {
              parent :: reset($attributes);
              $this->mailer->ClearAllRecipients();
              $this->mailer->ClearAttachments();
              $this->mailer->IsHTML(false);
          }
        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
        • 22295
        • 153 Posts
        as far as i know, it must be called to clear the previous sent email properties.
        http://svn.modxcms.com/docs/display/revolution/modMail
          • 3749
          • 24,544 Posts
          To send to multiple addresses, then, you might have to do this:

          $modx->mail->address('to','[email protected]');
          $modx->mail->address('to','[email protected]');


          Maybe there should be code somewhere to do that automatically with a comma-separated recipient list.
            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
            • 22295
            • 153 Posts
            To send to multiple addresses, then, you might have to do this:
            Thanks for your reply. but you got me wrong, multiple i meant - not same email to many people. but multiple calls to sendEmail (with different data).
            The mail-reset cleans up the previous email properties (in case the next one is missing one, it should not reuse the previous ones), i simply wonder why this breaks the execution when sendmail is called multiple times on a single script? when removed, this works:
            foreach ($msgs as $msg) {
            ...stuff...
            $output = $this->sendEmail($emailTo,$emailToName,$emailSubject,$emailPropertiesArray);
            }
              • 10487 MODX Staff
              • 1,535 Posts
              Notification script uses this function to send multiple emails, but it only send the first one then fails on all the rest.
              modMail/modPHPMailer was recently changed to include character set and encoding as attributes - these are initially set in the constructor but when using reset() they will be cleared as like all other attributes. So, in loops, the first email will be sent but then subsequent others will fail because of the missing charset and encoding for the email. To avoid this, use the following code:

              /* attributes to persist on reset */
              $attributes = array();
              $attributes[modMail::MAIL_CHARSET] = $modx->getOption('mail_charset',null,'UTF-8');
              $attributes[modMail::MAIL_ENCODING] = $modx->getOption('mail_encoding',null,'8bit');
              
              $modx->mail->reset($attributes);
              
                Garry Nutting
                Senior Developer
                MODX, LLC

                Email: [email protected]
                Twitter: @garryn
                Web: modx.com
                • 22295
                • 153 Posts
                Thanks. just tested and you’re right, that was the problem, as the constructor only runs once.

                a. as I use smtp, I had to add also the whole smtp block (modmail.class.php line 114 - 127) to the end of my sendEmail function.
                b. this is not elegant and may break with future changes to modmail (like another default attribute added..). I suggest that default attributes be taken to another initialization function which will be called at the end of reset().


                Thanks.
                  • 28215
                  • 4,149 Posts
                  Quote from: oori at Apr 05, 2010, 07:22 AM

                  Thanks. just tested and you’re right, that was the problem, as the constructor only runs once.

                  a. as I use smtp, I had to add also the whole smtp block (modmail.class.php line 114 - 127) to the end of my sendEmail function.
                  b. this is not elegant and may break with future changes to modmail (like another default attribute added..). I suggest that default attributes be taken to another initialization function which will be called at the end of reset().
                  Thanks.

                  I thought this was already done pre-RC1, here:

                  http://svn.modxcms.com/crucible/changelog/modx/branches/2.0?cs=6571
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • 22295
                    • 153 Posts
                    I thought this was already done pre-RC1, here:
                    http://svn.modxcms.com/crucible/changelog/modx/branches/2.0?cs=6571

                    true!
                    my mistake, I’m on 6551, i’ll grab the latest now.

                    thanks