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;
} public function reset($attributes= array()) {
parent :: reset($attributes);
$this->mailer->ClearAllRecipients();
$this->mailer->ClearAttachments();
$this->mailer->IsHTML(false);
}$modx->mail->address('to','[email protected]');
$modx->mail->address('to','[email protected]');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).
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);
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