/** * Send letter via selfTemplate * @param mixed $to * @param string $template * @param mixed $vars * @param mixed $params * @example * rad_mailtemplate::sendMasTemplate( * array('John Smith'=>'*****@*****.**', 'Anna'=>'*****@*****.**', '*****@*****.**'), * 'Template body with %%varname%% to %%__USER_NAME__%% to %%__USER_EMAIL__', * array('%%varname%%'=>'autoreplaced keys'), * array( * 'fromName'=>'ROBOT', * 'fromEmail'=>'*****@*****.**', * 'Subject'=>'mail subject', * 'smtp'=>array( * 'host':'smtp.example.com', * 'port':25, * 'user':'******', * 'password':'******', * 'security':'encode method none/ssh/tls' * ), * 'format'=>'text/plain',//default is text/html * 'header'=>'X-Priority: 1 (Higuest)'//not required * ) * ) * @return mixed result array('e-mail'=>boolean) */ public static function sendMasTemplate($to, $template, $vars, $params) { if (empty($to) or !is_array($to)) { throw new rad_exception('ERROR[mailtemplate]: $to param can be only array and can\'t be empty!'); } if (empty($params['fromName']) or empty($params['fromEmail'])) { throw new rad_exception('ERROR[mailtemplate]: params "fromName" and "fromEmail" can\'t be empty!'); } if (empty($params['Subject'])) { throw new rad_exception('ERROR[mailtemplate]: param "Subject" can\'t be empty!'); } $result = array(); foreach ($to as $userName => $userEmail) { $mailer = self::getMailerMessage($params['Subject']); if (!empty($params['header'])) { $headers = rad_mailtemplate::parseHeader($params['header']); if (!empty($headers)) { foreach ($headers as $headerName => $headerValue) { switch (strtolower($headerName)) { case 'x-priority': $mailer->setPriority((int) $headerValue); break; default: $mailer->getHeaders()->addTextHeader($headerName, $headerValue); break; } } } } $mailer->setBody(str_replace(array_keys($vars), $vars, $template), !empty($params['format']) ? $params['format'] : 'text/html'); $mailer->setFrom(array($params['fromEmail'] => $params['fromName'])); $mailer->setReturnPath($params['fromEmail']); if (!empty($params['smtp'])) { if (empty($params['smtp']['host']) or empty($params['smtp']['port']) or empty($params['smtp']['user']) or !isset($params['smtp']['password'])) { throw new rad_exception('Error in mailtemplate! Transport block: Not enouph actual params!'); } $transport = Swift_SmtpTransport::newInstance($params['smtp']['host'], $params['smtp']['port'])->setUsername($params['smtp']['user'])->setPassword($params['smtp']['password']); if (!empty($params['smtp']['security'])) { $transport->setEncryption($params['smtp']['security']); } } else { $transport = Swift_MailTransport::newInstance(); } $mailer->setCharset('utf-8'); if (is_int($userName)) { $mailer->setTo($userEmail); } else { $mailer->setTo('"' . $userName . '" <' . $userEmail . '>'); } $result[$userEmail] = rad_mailtemplate::getMailer($transport)->send($mailer); } return $result; }
/** * Send the mail * @param string|array $to - mail reciver, can be also as array('*****@*****.**' => 'John Doe') * @param enum(html|text) $format - format of letter (html or text) * @return boolean */ public function send($to, $format = 'text') { //include_once LIBPATH rad_mailtemplate::setCurrentItem($this); $o = rad_rsmarty::getSmartyObject(); if ($this->getVars()) { foreach ($this->getVars() as $key => $value) { $o->assign($key, $value); } } if (!is_file(MAILTEMPLATESPATH . $this->getTemplateName())) { throw new rad_exception('File "' . MAILTEMPLATESPATH . $this->getTemplateName() . '" not found!'); } $o->fetch(MAILTEMPLATESPATH . $this->getTemplateName()); $o->clearAllAssign(); if (empty($this->_blocks[$format])) { throw new rad_exception('Format "' . $format . '" is not declared in file: "' . MAILTEMPLATESPATH . $this->getTemplateName() . '"'); } if (!empty($this->_mailer)) { $this->_mailer->setSubject($this->_blocks[$format]['subject']); if (!empty($this->_blocks[$format]['Cc'])) { $this->_mailer->setCc($this->_blocks[$format]['Cc']); } if (!empty($this->_blocks[$format]['Bcc'])) { $this->_mailer->setBcc($this->_blocks[$format]['Bcc']); } if (!empty($this->_blocks[$format]['headers'])) { $headers = rad_mailtemplate::parseHeader($this->_blocks[$format]['headers']); if (!empty($headers)) { foreach ($headers as $headerName => $headerValue) { switch (strtolower($headerName)) { case 'x-priority': $this->_mailer->setPriority((int) $headerValue); break; default: $this->_mailer->getHeaders()->addTextHeader($headerName, $headerValue); break; } } } } if (!empty($this->_blocks[$format]['body'])) { $this->_mailer->setBody($this->_blocks[$format]['body'], $format == 'text' ? 'text/plain' : 'text/html'); } if (!empty($this->_blocks[$format]['from'])) { $from = explode("\n", str_replace("\r", '', $this->_blocks[$format]['from'])); if (count($from)) { foreach ($from as $fromString) { $fromItem = explode('<', $fromString); if (count($fromItem) > 1) { $fromName = trim($fromItem[0]); $fromEmail = trim(str_replace('>', '', $fromItem[1])); } else { $fromName = trim($fromItem[0]); $fromEmail = trim($fromItem[0]); } $this->_mailer->setFrom(array($fromEmail => $fromName)); $this->_mailer->setReturnPath($fromEmail); } } } if (!empty($this->_blocks[$format]['transport'])) { $transport = explode("\n", str_replace("\r", '', $this->_blocks[$format]['transport'])); if (!empty($transport)) { $transportParams = array(); foreach ($transport as $transportKey => $transportString) { $transportString = trim($transportString); if (!empty($transportString)) { $transportItem = explode(':', $transportString); if (count($transportItem) > 1) { $transportItemKey = trim($transportItem[0]); unset($transportItem[0]); $transportItemValue = trim(implode(':', $transportItem)); $transportParams[$transportItemKey] = $transportItemValue; } } } } if (empty($transportParams['type'])) { throw new rad_exception('Error in mailtemplate "' . $this->getTemplateName() . '" at transport block: type of the transport required!'); } switch (strtolower($transportParams['type'])) { case 'smtp': if (empty($transportParams['host']) or empty($transportParams['port']) or empty($transportParams['user']) or !isset($transportParams['password'])) { throw new rad_exception('Error in mailtemplate "' . $this->getTemplateName() . '" at transport block: Not enouph actual params!'); } $this->_transportInstance = Swift_SmtpTransport::newInstance($transportParams['host'], $transportParams['port'])->setUsername($transportParams['user'])->setPassword($transportParams['password']); if (!empty($transportParams['security'])) { $this->_transportInstance->setEncryption($transportParams['security']); } break; case 'mail': $this->_transportInstance = Swift_MailTransport::newInstance(); break; default: throw new rad_exception('Error in mailtemplate "' . $this->getTemplateName() . '" Unknown transport type "' . $transportParams['type'] . '"!'); break; } //switch } $this->_mailer->setTo($to); $this->_mailer->setCharset('utf-8'); if (!$this->_transportInstance) { $this->_transportInstance = Swift_MailTransport::newInstance(); } return rad_mailtemplate::getMailer($this->_transportInstance)->send($this->_mailer); } else { $headers = 'MIME-Version: 1.0' . PHP_EOL; $headers .= 'Content-Transfer-Encoding: base64' . PHP_EOL; $headers .= 'From: ' . $this->_blocks[$format]['from'] . PHP_EOL; switch ($format) { case 'text': $headers = 'Content-Type: text/plain; charset=utf-8' . PHP_EOL; break; case 'html': $headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL; break; default: throw new rad_exception('Unknown format: "' . $format . '"'); break; } if (!empty($this->_blocks[$format]['Cc'])) { $headers .= 'Cc: ' . $this->_blocks[$format]['Cc'] . PHP_EOL; } if (!empty($this->_blocks[$format]['Bcc'])) { $headers .= 'Bcc: ' . $this->_blocks[$format]['Bcc'] . PHP_EOL; } if (!empty($this->_blocks[$format]['headers'])) { $headers .= $this->_blocks[$format]['headers']; } if (is_array($to)) { $toString = ''; foreach ($to as $toEmail => $toName) { $toString .= $toName . ' <' . $toEmail . '>,'; } $to = substr($toString, 0, strlen($toString) - 1); } return mail($to, $this->_blocks[$format]['subject'], chunk_split(base64_encode($this->_blocks[$format]['body'])), $headers); } }