Beispiel #1
0
 public function send($fromAddress, $fromName, $replyToAddress, $replyToName, $toAddress, $toName, $subject, array $mimeParts)
 {
     $mail = new Message();
     if ($fromAddress && $fromName) {
         $mail->setFrom($fromAddress, $fromName);
     } else {
         if ($fromAddress) {
             $mail->setFrom($fromAddress);
         }
     }
     if ($replyToAddress && $replyToName) {
         $mail->setReplyTo($replyToAddress, $replyToName);
     } else {
         if ($replyToAddress) {
             $mail->setReplyTo($replyToAddress);
         }
     }
     if ($toAddress && $toName) {
         $mail->setTo($toAddress, $toName);
     } else {
         if ($toAddress) {
             $mail->setTo($toAddress);
         }
     }
     $mail->setSubject($subject);
     $mail->setEncoding('utf-8');
     $mime = new MimeMessage();
     $mime->setParts($mimeParts);
     $mail->setBody($mime);
     $this->mailTransport->send($mail);
     $this->getEventManager()->trigger('send', $mail);
 }
 /**
  * @inheritDoc
  */
 public static function fromWrappedMessage(MailWrappedMessage $wrappedMessage = null, $transport = null)
 {
     if (!$wrappedMessage instanceof MailWrappedMessage) {
         throw new MailWrapperSetupException('Not MailWrappedMessage');
     }
     $message = new Message();
     foreach ($wrappedMessage->getToRecipients() as $address) {
         $message->addTo($address);
     }
     foreach ($wrappedMessage->getCcRecipients() as $address) {
         $message->addCc($address);
     }
     foreach ($wrappedMessage->getBccRecipients() as $address) {
         $message->addBcc($address);
     }
     $message->setReplyTo($wrappedMessage->getReplyTo());
     $message->setFrom($wrappedMessage->getFrom());
     $message->setSubject($wrappedMessage->getSubject());
     if ($wrappedMessage->getContentText()) {
         $message->setBody($wrappedMessage->getContentText());
     }
     if ($wrappedMessage->getContentHtml()) {
         $html = new MimePart($wrappedMessage->getContentHtml());
         $html->type = "text/html";
         $message->setBody($body);
     }
     return $message;
 }
Beispiel #3
0
 public function setReplyTo($emailOrAddressList = null, $name = null)
 {
     if ($emailOrAddressList == null && $name == null) {
         $emailOrAddressList = $this->mailMessageConfigObj->getReplyTo();
         $name = $this->mailMessageConfigObj->getReplyName();
     }
     parent::setReplyTo($emailOrAddressList, $name);
     return $this;
 }
Beispiel #4
0
 public function testCanSetReplyToListFromAddressList()
 {
     $list = new AddressList();
     $list->add('*****@*****.**');
     $this->message->addReplyTo('*****@*****.**');
     $this->message->setReplyTo($list);
     $replyTo = $this->message->getReplyTo();
     $this->assertEquals(1, count($replyTo));
     $this->assertFalse($replyTo->has('*****@*****.**'));
     $this->assertTrue($replyTo->has('*****@*****.**'));
 }
 /**
  * @param ZendMailInterface $mail
  *
  * @return Mail\Message
  */
 protected function createMessage(ZendMailInterface $mail)
 {
     $message = new Mail\Message();
     $message->getHeaders()->addHeaders($mail->getHeaders());
     $message->setTo($mail->getTo());
     $message->setCc($mail->getCc());
     $message->setBcc($mail->getBcc());
     $message->setFrom($mail->getFrom());
     $message->setReplyTo($mail->getReplyTo());
     $message->setSubject($mail->getSubject());
     $message->setBody($this->messageCreator->createMessage($mail));
     return $message;
 }
 protected function sendMessage($destinations, $from, $subject, Mime\Message $body, $headers = false)
 {
     $message = new Mail\Message();
     $message->setFrom($this->alwaysFrom ? $this->alwaysFrom : $from);
     $message->setSubject($subject);
     $message->setBody($body);
     $message->setReplyTo($from);
     if (isset($destinations)) {
         $destinations = is_array($destinations) ? $destinations : explode(',', $destinations);
     } else {
         $destinations = array();
     }
     //Set our headers. If we find CC or BCC emails add them to the Destinations array
     if (!isset($headers['To'])) {
         $headers['To'] = implode('; ', $destinations);
     }
     if (isset($headers['Cc'])) {
         $destinations = array_merge($destinations, explode(',', $headers['Cc']));
     }
     if (isset($headers['Bcc'])) {
         $destinations = array_merge($destinations, explode(',', $headers['Bcc']));
     }
     // if a custom 'reply-to' address has been set via headers
     if (isset($headers['Reply-To'])) {
         $message->setReplyTo($headers['Reply-To']);
         unset($headers['Reply-To']);
     }
     if ($headers) {
         $message->getHeaders()->addHeaders($headers);
     }
     //if no Destinations address is set SES will reject the email.
     if (!array_filter($destinations)) {
         throw new LogicException('No Destinations (To, Cc, Bcc) for email set.');
     }
     try {
         $response = $this->client->sendRawEmail(array('Destinations' => $destinations, 'RawMessage' => array('Data' => $this->getMessageText($message))));
     } catch (\Aws\Ses\Exception\SesException $ex) {
         return false;
     }
     /* @var $response Aws\Result */
     if (isset($response['MessageId']) && strlen($response['MessageId'])) {
         return true;
     }
 }
 /**
  * 
  * @param Message $message
  * @return ZendMessage
  */
 public static function convert(Message $message)
 {
     $mailMessage = new ZendMessage();
     $mailMessage->setSubject($message->getSubject());
     $mailMessage->setFrom($message->getFrom());
     $mailMessage->setTo($message->getTo());
     $mailMessage->setCc($message->getCc());
     $mailMessage->setBcc($message->getBcc());
     $mailMessage->setReplyTo($message->getReplyTo());
     $mailMessage->getHeaders()->addHeaders($message->getHeaders());
     if ($mailMessage->getSender()) {
         $mailMessage->setSender($message->getSender());
     }
     if ($message->isMultipart()) {
         $mimePart = new MimeMessage();
         if ($message->getBodyHtml()) {
             $part = new Part($message->getBodyHtml());
             $part->charset = $message->getCharset();
             $part->encoding = $message->getEncoding();
             $part->type = Mime::TYPE_HTML;
             $mimePart->addPart($part);
         }
         if ($message->getBodyText()) {
             $part = new Part($message->getBodyText());
             $part->charset = $message->getCharset();
             $part->encoding = $message->getEncoding();
             $part->type = Mime::TYPE_TEXT;
             $mimePart->addPart($part);
         }
         foreach ($message->getAttachments() as $attachment) {
             $mimePart->addPart($attachment->asMimePart());
         }
         foreach ($message->getParts() as $part) {
             $mimePart->addPart($part);
         }
         $mailMessage->setBody($mimePart);
     } else {
         $mailMessage->getHeaders()->addHeaderLine('Content-Type', $message->getContentType());
         $mailMessage->setEncoding($message->getEncoding());
         $mailMessage->setBody($message->getFilledBody());
     }
     return $mailMessage;
 }
Beispiel #8
0
 /**
  * @param  string  $tpl
  * @param  array   $data
  * @return Message
  */
 public function getMessage($tpl, array $data)
 {
     $mail = new Message();
     $mail->setEncoding('UTF-8');
     if (isset($data['encoding'])) {
         $mail->setEncoding($data['encoding']);
     }
     if (isset($data['from_address'])) {
         if (isset($data['from_name'])) {
             $mail->setFrom($data['from_address'], $data['from_name']);
         } else {
             $mail->setFrom($data['from_address']);
         }
     }
     if (isset($data['to'])) {
         if (isset($data['to_name'])) {
             $mail->setTo($data['to'], $data['to_name']);
         } else {
             $mail->setTo($data['to']);
         }
     }
     if (isset($data['cc'])) {
         $mail->setCc($data['cc']);
     }
     if (isset($data['bcc'])) {
         $mail->setBcc($data['bcc']);
     }
     if (isset($data['subject'])) {
         $mail->setSubject($data['subject']);
     }
     if (isset($data['sender'])) {
         $mail->setSender($data['sender']);
     }
     if (isset($data['replyTo'])) {
         $mail->setReplyTo($data['replyTo']);
     }
     $content = $this->renderMail($tpl, $data);
     $mail->setBody($content);
     $mail->getHeaders()->addHeaderLine('Content-Type', 'text/html; charset=UTF-8')->addHeaderLine('Content-Transfer-Encoding', '8bit');
     return $mail;
 }
    public static function send($config = [])
    {
        if (!empty($config['smtp'])) {
            $transport = new SmtpTransport(new SmtpOptions(['name' => $config['smtp']['name'], 'host' => $config['smtp']['host'], 'port' => $config['smtp']['port'], 'connection_class' => 'login', 'connection_config' => ['username' => $config['smtp']['username'], 'password' => $config['smtp']['password'], 'ssl' => $config['smtp']['ssl']]]));
        } else {
            $param = '';
            if (!empty($config['returnPath'])) {
                $param = sprintf('-r%s', $config['returnPath']);
            }
            $transport = new SendmailTransport($param);
        }
        $message = new Message();
        $message->setEncoding($config['charset']);
        foreach ($config['from'] as $email => $name) {
            $message->setFrom($email, $name);
            break;
        }
        foreach ($config['to'] as $email => $name) {
            $message->addTo($email, $name);
        }
        if (empty($config['replyTo'])) {
            $config['replyTo'] = $config['from'];
        }
        foreach ($config['replyTo'] as $email => $name) {
            $message->setReplyTo($email, $name);
            break;
        }
        $message->setSubject($config['subject']);
        $htmlBody = '';
        if (!empty($config['html'])) {
            $htmlBody = $config['html'];
        } elseif (!empty($config['template'])) {
            $htmlBody = file_get_contents($config['template']);
            foreach ($config['fields'] as $field => $label) {
                $htmlBody = str_replace(sprintf('{$%s}', $field), $config['post'][$field], $htmlBody);
            }
        } else {
            $htmlBody .= str_repeat('= ', $config['lineWidth'] / 2) . PHP_EOL;
            $maxWidth = 0;
            foreach ($config['fields'] as $field => $label) {
                $currentWidth = mb_strlen($label);
                if ($currentWidth > $maxWidth) {
                    $maxWidth = $currentWidth;
                }
            }
            foreach ($config['fields'] as $field => $label) {
                $htmlBody .= sprintf('<strong>%s:</strong> %s', str_pad($label, $maxWidth, '.', STR_PAD_RIGHT), $config['post'][$field]) . PHP_EOL;
            }
            $htmlBody .= str_repeat('= ', $config['lineWidth'] / 2);
            $htmlBody = '
			<html>
				<body>
					<table>
						<tr>
							<td>
								<div style="font-family: \'courier new\', courier, monospace; font-size: 14px;">' . nl2br($htmlBody) . '</div>
							</td>
						</tr>
					</table>
				</body>
			</html>';
        }
        $html = new MimePart($htmlBody);
        $html->type = 'text/html';
        $body = new MimeMessage();
        $body->setParts([$html]);
        $message->setBody($body);
        try {
            $transport->send($message);
            return true;
        } catch (RuntimeException $e) {
            return false;
        }
    }
 /**
  * @return Message
  */
 protected function createMessage()
 {
     $options = $this->mailOptions->getMessageOptions();
     // Prepare Mail Message
     $message = new Message();
     $from = $options->getFrom();
     if (!empty($from)) {
         $message->setFrom($from, $options->getFromName());
     }
     $replyTo = $options->getReplyTo();
     if (!empty($replyTo)) {
         $message->setReplyTo($replyTo, $options->getReplyToName());
     }
     $to = $options->getTo();
     if (!empty($to)) {
         $message->setTo($to);
     }
     $cc = $options->getCc();
     if (!empty($cc)) {
         $message->setCc($cc);
     }
     $bcc = $options->getBcc();
     if (!empty($bcc)) {
         $message->setBcc($bcc);
     }
     return $message;
 }
 /**
  * Create a email based on it's theme an params
  *
  * @param string $address
  * @param string $name
  * @throws Exception
  * @return \Zend\Mail\Message
  */
 protected function _constructEmail($address, $name)
 {
     $content = $this->_template->render();
     if ('' == $this->_replyTo) {
         $this->_replyTo = $this->_template->getTestament()->getReplyTo();
     }
     if ('' == $this->_fromName) {
         $this->_fromName = $this->_template->getTestament()->getFromName();
     }
     if ('' == $this->_fromAddress) {
         $this->_fromAddress = $this->_template->getTestament()->getFromAddress();
     }
     if ('' == $this->_subject) {
         $this->_subject = $this->_template->getTestament()->getFromAddress();
     }
     $contentParts = array();
     $partText = new Part($content->getText());
     $partText->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
     $partText->type = Mime::TYPE_TEXT;
     $contentParts[] = $partText;
     $partHtml = new Part($content->getHtml());
     $partHtml->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
     $partHtml->type = Mime::TYPE_HTML;
     $partHtml->charset = 'UTF-8';
     $contentParts[] = $partHtml;
     $alternatives = new \Zend\Mime\Message();
     $alternatives->setParts($contentParts);
     $alternativesPart = new Part($alternatives->generateMessage());
     $alternativesPart->type = "multipart/alternative; boundary=\"" . $alternatives->getMime()->boundary() . "\"";
     $body = new \Zend\Mime\Message();
     $body->addPart($alternativesPart);
     foreach ($this->_attachments as $attachmentSrc) {
         $attachment = new Part(fopen($attachmentSrc['filelocation'], 'r'));
         $attachment->filename = $attachmentSrc['filename'];
         $attachment->encoding = Mime::ENCODING_BASE64;
         $attachment->type = Mime::DISPOSITION_ATTACHMENT;
         $attachment->disposition = true;
         $body->addPart($attachment);
     }
     $subject = $this->_subject;
     foreach ($this->_variables as $name => $variable) {
         $subject = str_replace('{{:' . $name . ':}}', $variable, $subject);
     }
     $message = new Message();
     $message->setSubject($subject);
     $message->setFrom($this->_fromAddress, $this->_fromName);
     if ($this->_replyTo) {
         $message->setReplyTo($this->_replyTo);
     }
     $message->setBody($body);
     $message->setTo($address, $name);
     $message->setEncoding("UTF-8");
     return $message;
 }
Beispiel #12
0
 /**
  * @param string $templateName
  * @param string $email
  * @param array $params
  * @return boolean
  */
 public function sendMail($templateName, $email, array $params = array())
 {
     $config = $this->getServiceLocator()->get('Config');
     $settings = $this->getServiceLocator()->get('Settings');
     /** @var \Doctrine\ORM\EntityManager $entityManager */
     $entityManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
     if (!isset($config['mailer']['templates_path'])) {
         throw new \Exception('Mail templates path is not set');
     }
     // get from addresss
     if (!isset($params['from'])) {
         $params['from'] = $settings->get('mail:from');
     }
     $from = $params['from'];
     if ($from == '') {
         throw new \Exception('Can\'t send mail - from address not given');
     }
     // get language
     if (!isset($params['language']) || $params['language'] == '') {
         throw new \Exception('Can\'t send mail - language not given');
     }
     $language = $params['language'];
     // get mail template
     //        $templatesTable = $this->getServiceLocator()->get('Msingi\Cms\Db\Table\MailTemplates');
     $templates_repository = $entityManager->getRepository('Msingi\\Cms\\Entity\\MailTemplate');
     $template = $templates_repository->fetchOrCreate($templateName);
     /** @var \Msingi|Cms\Repository\MailTemplatesI18n $templates_i18n_repository */
     $templates_i18n_repository = $entityManager->getRepository('Msingi\\Cms\\Entity\\MailTemplateI18n');
     /** @var \Msingi\Cms\Entity\MailTemplateI18n $i18n */
     $i18n = $templates_i18n_repository->fetchOrCreate($template, $language);
     // replace tokens
     $subject = $this->processTokens($i18n->getSubject(), $params);
     $message = $this->processTokens($i18n->getTemplate(), $params);
     // initialize renderer
     $renderer = new PhpRenderer();
     $resolver = new Resolver\AggregateResolver();
     $renderer->setResolver($resolver);
     $stack = new Resolver\TemplatePathStack(array('script_paths' => array($config['mailer']['templates_path'])));
     $resolver->attach($stack);
     // render message template
     $messageHtml = $renderer->render('default', array('content' => $message));
     // get text content of the message
     $html2text = new HTML2Text();
     $html2text->html2text($messageHtml);
     // create text message part
     $messageTextPart = new Mime\Part($html2text->get_text());
     $messageTextPart->type = 'text/plain';
     // create html message part
     $messageHtmlPart = new Mime\Part($messageHtml);
     $messageHtmlPart->type = 'text/html';
     // create message body
     $messageBody = new Mime\Message();
     $messageBody->setParts(array($messageTextPart, $messageHtmlPart));
     // @todo Implement attachements
     //
     $mail = new Mail\Message();
     $mail->setFrom($from);
     if (isset($params['reply-to']) && $params['reply-to'] != '') {
         $mail->setReplyTo($params['reply-to']);
     }
     $mail->addTo($email);
     $mail->setEncoding('UTF-8');
     $mail->setSubject($subject);
     $mail->setBody($messageBody);
     $mail->getHeaders()->get('Content-Type')->setType('multipart/alternative');
     //
     $mailer_config = $config['mailer'];
     // log message
     if ($settings->get('mail:log')) {
         if (!is_dir($config['mailer']['log_dir'])) {
             mkdir($config['mailer']['log_dir']);
         }
         $transport = new \Zend\Mail\Transport\File();
         $options = new \Zend\Mail\Transport\FileOptions(array('path' => $config['mailer']['log_dir'], 'callback' => function (\Zend\Mail\Transport\File $transport) use($templateName) {
             return date('Ymd.His') . '-' . $templateName . '-' . mt_rand() . '.txt';
         }));
         $transport->setOptions($options);
         $transport->send($mail);
     }
     // send message
     if ($settings->get('mail:send')) {
         if (isset($mailer_config['transport']) && $mailer_config['transport'] == 'smtp') {
             $transport = new SmtpTransport();
             $options = new Mail\Transport\SmtpOptions($mailer_config['smtp_options']);
             $transport->setOptions($options);
         } else {
             $transport = new SendmailTransport();
         }
         $transport->send($mail);
     }
     return true;
 }
Beispiel #13
0
 public function send(Email $email, $params = array(), &$message = null, $attachmetList = [])
 {
     if (!$message) {
         $message = new Message();
     }
     $config = $this->config;
     $params = $this->params + $params;
     if ($email->get('from')) {
         $fromName = null;
         if (!empty($params['fromName'])) {
             $fromName = $params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom(trim($email->get('from')), $fromName);
     } else {
         if (!empty($params['fromAddress'])) {
             $fromAddress = $params['fromAddress'];
         } else {
             if (!$config->get('outboundEmailFromAddress')) {
                 throw new Error('outboundEmailFromAddress is not specified in config.');
             }
             $fromAddress = $config->get('outboundEmailFromAddress');
         }
         if (!empty($params['fromName'])) {
             $fromName = $params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom($fromAddress, $fromName);
     }
     if (!$email->get('from')) {
         $email->set('from', $fromAddress);
     }
     if (!empty($params['replyToAddress'])) {
         $replyToName = null;
         if (!empty($params['replyToName'])) {
             $replyToName = $params['replyToName'];
         }
         $message->setReplyTo($params['replyToAddress'], $replyToName);
     }
     $value = $email->get('to');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addTo(trim($address));
             }
         }
     }
     $value = $email->get('cc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addCC(trim($address));
             }
         }
     }
     $value = $email->get('bcc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addBCC(trim($address));
             }
         }
     }
     $value = $email->get('replyTo');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addReplyTo(trim($address));
             }
         }
     }
     $attachmentPartList = array();
     $attachmentCollection = $email->get('attachments');
     $attachmentInlineCollection = $email->getInlineAttachments();
     foreach ($attachmetList as $attachment) {
         $attachmentCollection[] = $attachment;
     }
     if (!empty($attachmentCollection)) {
         foreach ($attachmentCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_ATTACHMENT;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->filename = $a->get('name');
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $attachmentPartList[] = $attachment;
         }
     }
     if (!empty($attachmentInlineCollection)) {
         foreach ($attachmentInlineCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_INLINE;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->id = $a->id;
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $attachmentPartList[] = $attachment;
         }
     }
     $message->setSubject($email->get('name'));
     $body = new MimeMessage();
     $textPart = new MimePart($email->getBodyPlainForSending());
     $textPart->type = 'text/plain';
     $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
     $textPart->charset = 'utf-8';
     if ($email->get('isHtml')) {
         $htmlPart = new MimePart($email->getBodyForSending());
         $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
         $htmlPart->type = 'text/html';
         $htmlPart->charset = 'utf-8';
     }
     if (!empty($attachmentPartList)) {
         $messageType = 'multipart/related';
         if ($email->get('isHtml')) {
             $content = new MimeMessage();
             $content->addPart($textPart);
             $content->addPart($htmlPart);
             $messageType = 'multipart/mixed';
             $contentPart = new MimePart($content->generateMessage());
             $contentPart->type = "multipart/alternative;\n boundary=\"" . $content->getMime()->boundary() . '"';
             $body->addPart($contentPart);
         } else {
             $body->addPart($textPart);
         }
         foreach ($attachmentPartList as $attachmentPart) {
             $body->addPart($attachmentPart);
         }
     } else {
         if ($email->get('isHtml')) {
             $body->setParts(array($textPart, $htmlPart));
             $messageType = 'multipart/alternative';
         } else {
             $body = $email->getBodyPlainForSending();
             $messageType = 'text/plain';
         }
     }
     $message->setBody($body);
     if ($messageType == 'text/plain') {
         if ($message->getHeaders()->has('content-type')) {
             $message->getHeaders()->removeHeader('content-type');
         }
         $message->getHeaders()->addHeaderLine('Content-Type', 'text/plain; charset=UTF-8');
     } else {
         if (!$message->getHeaders()->has('content-type')) {
             $contentTypeHeader = new \Zend\Mail\Header\ContentType();
             $message->getHeaders()->addHeader($contentTypeHeader);
         }
         $message->getHeaders()->get('content-type')->setType($messageType);
     }
     $message->setEncoding('UTF-8');
     try {
         $rand = mt_rand(1000, 9999);
         if ($email->get('parentType') && $email->get('parentId')) {
             $messageId = '' . $email->get('parentType') . '/' . $email->get('parentId') . '/' . time() . '/' . $rand . '@espo';
         } else {
             $messageId = '' . md5($email->get('name')) . '/' . time() . '/' . $rand . '@espo';
         }
         if ($email->get('isSystem')) {
             $messageId .= '-system';
         }
         $messageIdHeader = new \Zend\Mail\Header\MessageId();
         $messageIdHeader->setId($messageId);
         $message->getHeaders()->addHeader($messageIdHeader);
         $this->transport->send($message);
         $email->set('messageId', '<' . $messageId . '>');
         $email->set('status', 'Sent');
         $email->set('dateSent', date("Y-m-d H:i:s"));
     } catch (\Exception $e) {
         throw new Error($e->getMessage(), 500);
     }
     $this->useGlobal();
 }
 /**
  * Metodo para envio de email pelo sistema. As configuracoes de conexao com o servidor de email
  * devem estar no arquivo 'config/autoload/'.$type.'.local.php'.
  * Ps.: Este arquivo, por conter senhas, é ignorado pelo git.
  * 
  * @param string|array $emailTo
  * @param string $subject
  * @param string $htmlBody
  * @param string $type
  * @param string|array $replyTo
  * @throws Exception
  */
 protected function email($emailTo, $subject, $htmlBody, $type = 'mail', $replyTo = null)
 {
     $result = true;
     /**
      * O arquivo *.local.php nao eh incluido no git, por isso as informacoes contidas
      * nele sao seguras, diferente do arquivo global.php.
      */
     $auth = 'config/autoload/' . $type . '.local.php';
     $authConf = file_exists($auth) ? require $auth : false;
     $global = (require 'config/autoload/global.php');
     $options = array_merge_recursive(isset($global[$type]) ? $global[$type] : array(), $authConf);
     try {
         if ($options) {
             // Codifica o tipo de mensagem HTML
             $mimePart = new Mime\Part($htmlBody);
             $mimePart->type = Mime\Mime::TYPE_HTML;
             $mimeMsg = new Mime\Message();
             $mimeMsg->setParts(array($mimePart));
             // Cria mensagem efetivamente
             $message = new Message();
             $message->setBody($mimeMsg);
             $message->setEncoding('UTF-8');
             // Informacoes do e-mail em si
             $message->addFrom($options['connection_config']['username'], 'Braghim Sistemas');
             if ($replyTo) {
                 $message->setReplyTo($replyTo);
             }
             $message->addTo($emailTo);
             $message->setSubject($subject);
             // Transportador de mensagem
             $transport = new SmtpTransport();
             $transport->setOptions(new SmtpOptions($options));
             $transport->send($message);
         } else {
             throw new \Exception("Configurações de e-mail não foram definidas ou arquivo '{$type}.local.php' não existe");
         }
     } catch (\Exception $e) {
         Firephp::getInstance()->err($e->__toString());
         $result = false;
     }
     return $result;
 }
Beispiel #15
0
 function setReplyTo($email, $name = null)
 {
     $this->mail->setReplyTo($email, $name);
 }
Beispiel #16
0
 public function send(Email $email, $params = array())
 {
     $message = new Message();
     $config = $this->config;
     $params = $this->params + $params;
     if ($email->get('from')) {
         $fromName = null;
         if (!empty($params['fromName'])) {
             $fromName = $params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom(trim($email->get('from')), $fromName);
     } else {
         if (!empty($params['fromAddress'])) {
             $fromAddress = $params['fromAddress'];
         } else {
             if (!$config->get('outboundEmailFromAddress')) {
                 throw new Error('outboundEmailFromAddress is not specified in config.');
             }
             $fromAddress = $config->get('outboundEmailFromAddress');
         }
         if (!empty($params['fromName'])) {
             $fromName = $params['fromName'];
         } else {
             $fromName = $config->get('outboundEmailFromName');
         }
         $message->addFrom($fromAddress, $fromName);
     }
     if (!empty($params['replyToAddress'])) {
         $replyToName = null;
         if (!empty($params['replyToName'])) {
             $replyToName = $params['replyToName'];
         }
         $message->setReplyTo($params['replyToAddress'], $replyToName);
     }
     $value = $email->get('to');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addTo(trim($address));
             }
         }
     }
     $value = $email->get('cc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addCC(trim($address));
             }
         }
     }
     $value = $email->get('bcc');
     if ($value) {
         $arr = explode(';', $value);
         if (is_array($arr)) {
             foreach ($arr as $address) {
                 $message->addBCC(trim($address));
             }
         }
     }
     $message->setSubject($email->get('name'));
     $body = new MimeMessage();
     $parts = array();
     $bodyPart = new MimePart($email->getBodyPlainForSending());
     $bodyPart->type = 'text/plain';
     $bodyPart->charset = 'utf-8';
     $parts[] = $bodyPart;
     if ($email->get('isHtml')) {
         $bodyPart = new MimePart($email->getBodyForSending());
         $bodyPart->type = 'text/html';
         $bodyPart->charset = 'utf-8';
         $parts[] = $bodyPart;
     }
     $aCollection = $email->get('attachments');
     if (!empty($aCollection)) {
         foreach ($aCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_ATTACHMENT;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->filename = $a->get('name');
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $parts[] = $attachment;
         }
     }
     $aCollection = $email->getInlineAttachments();
     if (!empty($aCollection)) {
         foreach ($aCollection as $a) {
             $fileName = 'data/upload/' . $a->id;
             $attachment = new MimePart(file_get_contents($fileName));
             $attachment->disposition = Mime::DISPOSITION_INLINE;
             $attachment->encoding = Mime::ENCODING_BASE64;
             $attachment->id = $a->id;
             if ($a->get('type')) {
                 $attachment->type = $a->get('type');
             }
             $parts[] = $attachment;
         }
     }
     $body->setParts($parts);
     $message->setBody($body);
     if ($email->get('isHtml')) {
         $message->getHeaders()->get('content-type')->setType('multipart/alternative');
     }
     try {
         $this->transport->send($message);
         $headers = $message->getHeaders();
         if ($headers->has('messageId')) {
             $email->set('messageId', $headers->get('messageId')->getId());
         }
         $email->set('status', 'Sent');
         $email->set('dateSent', date("Y-m-d H:i:s"));
     } catch (\Exception $e) {
         throw new Error($e->getMessage(), 500);
     }
     $this->useGlobal();
 }
 /**
  * Receives input from the user and sends an email to the recipient set in
  * the config.ini
  *
  * @return void
  */
 public function emailAction()
 {
     $view = $this->createViewModel();
     $view->useRecaptcha = $this->recaptcha()->active('feedback');
     $category = $this->params()->fromPost('category');
     $name = $this->params()->fromPost('name');
     $users_email = $this->params()->fromPost('email');
     $comments = $this->params()->fromPost('comments');
     $url = $this->params()->fromPost('url');
     $captcha = $this->params()->fromPost('captcha');
     // Process form submission:
     if ($this->formWasSubmitted('submit', $view->useRecaptcha)) {
         if (empty($captcha) || $captcha != $this->translate('feedback_captcha_answer')) {
             $view->setTemplate('feedback/home');
             $view->category = $category;
             $view->name = $name;
             $view->email = $users_email;
             $view->comments = $comments;
             $view->url = $url;
             $this->flashMessenger()->addErrorMessage('feedback_captcha_error');
             return $view;
         }
         if (empty($comments)) {
             throw new \Exception('Missing data.');
         }
         $validator = new \Zend\Validator\EmailAddress();
         if (!empty($users_email) && !$validator->isValid($users_email)) {
             throw new \Exception('Email address is invalid');
         }
         // These settings are set in the feedback section of your config.ini
         $config = $this->getServiceLocator()->get('VuFind\\Config')->get('config');
         $feedback = isset($config->Feedback) ? $config->Feedback : null;
         $recipient_email = !empty($feedback->recipient_email) ? $feedback->recipient_email : $config->Site->email;
         $recipient_name = isset($feedback->recipient_name) ? $feedback->recipient_name : 'Your Library';
         $email_subject = isset($feedback->email_subject) ? $feedback->email_subject : 'VuFind Feedback';
         $email_subject .= ' (' . $this->translate($category) . ')';
         $sender_email = isset($feedback->sender_email) ? $feedback->sender_email : '*****@*****.**';
         $sender_name = isset($feedback->sender_name) ? $feedback->sender_name : 'VuFind Feedback';
         if ($recipient_email == null) {
             throw new \Exception('Feedback Module Error: Recipient Email Unset (see config.ini)');
         }
         $email_message = $this->translate('feedback_category') . ': ' . $this->translate($category) . "\n";
         $email_message .= $this->translate('feedback_name') . ': ' . ($name ? $name : '-') . "\n";
         $email_message .= $this->translate('feedback_email') . ': ' . ($users_email ? $users_email : '-') . "\n";
         $email_message .= $this->translate('feedback_url') . ': ' . ($url ? $url : '-') . "\n";
         $email_message .= "\n" . $this->translate('feedback_message') . ":\n";
         $email_message .= "----------\n\n{$comments}\n\n----------\n";
         // This sets up the email to be sent
         $mail = new Mail\Message();
         $mail->setEncoding('UTF-8');
         $mail->setBody($email_message);
         $mail->setFrom($sender_email, $sender_name);
         $mail->setReplyTo($users_email, $name);
         $mail->addTo($recipient_email, $recipient_name);
         $mail->setSubject($email_subject);
         $headers = $mail->getHeaders();
         $headers->removeHeader('Content-Type');
         $headers->addHeaderLine('Content-Type', 'text/plain; charset=UTF-8');
         try {
             $this->getServiceLocator()->get('VuFind\\Mailer')->getTransport()->send($mail);
             $view->setTemplate('feedback/response');
         } catch (\Exception $e) {
             $this->flashMessenger()->addErrorMessage('feedback_error');
         }
     }
     return $view;
 }