function send($recipients, $type = 'mail') { global $tikilib, $prefs; $logslib = TikiLib::lib('logs'); $this->mail->getHeaders()->removeHeader('to'); foreach ((array) $recipients as $to) { $this->mail->addTo($to); } if ($prefs['zend_mail_handler'] == 'smtp' && $prefs['zend_mail_queue'] == 'y') { $query = "INSERT INTO `tiki_mail_queue` (message) VALUES (?)"; $bindvars = array(serialize($this->mail)); $tikilib->query($query, $bindvars, -1, 0); $title = 'mail'; } else { try { tiki_send_email($this->mail); $title = 'mail'; } catch (Zend\Mail\Exception\ExceptionInterface $e) { $title = 'mail error'; } if ($title == 'mail error' || $prefs['log_mail'] == 'y') { foreach ($recipients as $u) { $logslib->add_log($title, $u . '/' . $this->mail->getSubject()); } } } return $title == 'mail'; }
/** * Parse the subject of the email. */ public function parseSubject() { //Transfer first the subject form the email (if any) $this->message->setSubject($this->email->getSubject()); /* * When the subject is empty AND we have a template, simply take the subject of the template */ if (!empty($this->message->getSubject()) && !is_null($this->template)) { $this->message->setSubject($this->template->getSubject()); } /* * Go over the templateVars to replace content in the subject */ foreach ($this->templateVars as $key => $replace) { /* * Skip the service manager */ if ($replace instanceof ServiceManager) { continue; } /* * replace the content of the title with the available keys in the template vars */ if (!is_array($replace)) { $this->message->setSubject(str_replace(sprintf("[%s]", $key), $replace, $this->message->getSubject())); } } }
/** * Send a mail message * * @param \Zend\Mail\Message $message * @return array */ public function send(Mail\Message $message) { $consumer = new Consumer(); $consumer->setUrl($this->config['url']); $params = array('to' => urlencode($this->config['to']), 'subject' => urlencode($message->getSubject()), 'from' => urlencode($message->getFrom()->current()->getEmail()), 'fromname' => urlencode($message->getFrom()->current()->getName()), 'text' => urlencode($message->getBodyText()), 'api_user' => urlencode($this->config['user']), 'api_key' => urlencode($this->config['key'])); $bcc = $message->getBcc()->current(); if ($bcc) { $params['bcc'] = $bcc->getEmail(); } $consumer->setParams($params); $consumer->setResponseType('json'); $consumer->setCallType('get'); return $consumer->doApiCall(); }
/** * {@inheritDoc} * @link http://help.postageapp.com/kb/api/send_message * @throws Exception\RuntimeException if the mail is sent to more than 50 recipients (Amazon SES limit) * @return array The id and UID of the sent message (if sent correctly) */ public function send(Message $message) { $from = $message->getFrom(); if (count($from) !== 1) { throw new Exception\RuntimeException('Amazon SES requires exactly one from sender'); } $parameters = array('Source' => $from->rewind()->toString(), 'Message' => array('Subject' => array('Data' => $message->getSubject()))); $textContent = $this->extractText($message); if (!empty($textContent)) { $parameters['Message']['Body']['Text']['Data'] = $textContent; } $htmlContent = $this->extractHtml($message); if (!empty($htmlContent)) { $parameters['Message']['Body']['Html']['Data'] = $htmlContent; } $countRecipients = count($message->getTo()); $to = array(); foreach ($message->getTo() as $address) { $to[] = $address->toString(); } $parameters['Destination']['ToAddresses'] = $to; $countRecipients += count($message->getCc()); $cc = array(); foreach ($message->getCc() as $address) { $cc[] = $address->toString(); } $parameters['Destination']['CcAddresses'] = $cc; $countRecipients += count($message->getBcc()); $bcc = array(); foreach ($message->getBcc() as $address) { $bcc[] = $address->toString(); } $parameters['Destination']['BccAddresses'] = $bcc; if ($countRecipients > self::RECIPIENT_LIMIT) { throw new Exception\RuntimeException(sprintf('You have exceeded limitation for Amazon SES count recipients (%s maximum, %s given)', self::RECIPIENT_LIMIT, $countRecipients)); } $replyTo = array(); foreach ($message->getReplyTo() as $address) { $replyTo[] = $address->toString(); } $parameters['ReplyToAddresses'] = $replyTo; try { return $this->client->sendEmail($this->filterParameters($parameters))->toArray(); } catch (SesException $exception) { $this->parseException($exception); } }
/** * {@inheritDoc} * @link http://help.postageapp.com/kb/api/send_message * @return array The id and UID of the sent message (if sent correctly) */ public function send(Message $message) { $from = $message->getFrom(); if (count($from) !== 1) { throw new Exception\RuntimeException('Postage API requires exactly one from sender'); } if (count($message->getCc())) { throw new Exception\RuntimeException('Postage does not support CC addresses'); } if (count($message->getBcc())) { throw new Exception\RuntimeException('Postage does not support BCC addresses'); } $parameters = array('uid' => sha1(json_encode(uniqid())), 'arguments' => array('headers' => array('subject' => $message->getSubject(), 'from' => $from->rewind()->toString()), 'content' => array('text/plain' => $this->extractText($message), 'text/html' => $this->extractHtml($message)))); $to = array(); foreach ($message->getTo() as $address) { $to[] = $address->toString(); } $parameters['arguments']['recipients'] = implode(',', $to); $replyTo = $message->getReplyTo(); if (count($replyTo) > 1) { throw new Exception\RuntimeException('Postage has only support for one Reply-To address'); } elseif (count($replyTo)) { $parameters['headers']['reply-to'] = $replyTo->rewind()->toString(); } if ($message instanceof PostageMessage) { if ($message->getTemplate() !== '') { $parameters['arguments']['template'] = $message->getTemplate(); $parameters['arguments']['variables'] = $message->getVariables(); } } $attachments = $this->extractAttachments($message); foreach ($attachments as $attachment) { $parameters['arguments']['attachments'][$attachment->filename] = array('content_type' => $attachment->type, 'content' => base64_encode($attachment->getRawContent())); } $response = $this->prepareHttpClient('/send_message.json', $parameters)->send(); $data = $this->parseResponse($response); return array('uid' => $parameters['uid'], 'id' => $data['message']['id']); }
/** * {@inheritDoc} * @link http://elasticemail.com/api-documentation/send * @return string The transaction id of the email */ public function send(Message $message) { $from = $message->getFrom(); if (count($from) !== 1) { throw new Exception\RuntimeException('Elastic Email API requires exactly one from sender'); } $parameters = array('from' => $from->rewind()->getEmail(), 'from_name' => $from->rewind()->getName(), 'subject' => $message->getSubject(), 'body_text' => $this->extractText($message), 'body_html' => $this->extractHtml($message)); $to = array(); foreach ($message->getTo() as $address) { $to[] = $address->toString(); } foreach ($message->getCc() as $address) { $to[] = $address->toString(); } // Elastic Email treats Bcc exactly as To addresses (they are treated separately) foreach ($message->getBcc() as $address) { $to[] = $address->toString(); } $parameters['to'] = implode(';', $to); $replyTo = $message->getReplyTo(); if (count($replyTo) > 1) { throw new Exception\RuntimeException('Elastic Email has only support for one Reply-To address'); } elseif (count($replyTo)) { $parameters['reply_to'] = $replyTo->rewind()->getEmail(); $parameters['reply_to_name'] = $replyTo->rewind()->getName(); } if ($message instanceof ElasticEmailMessage) { $parameters['channel'] = $message->getChannel(); $parameters['template'] = $message->getTemplate(); } // Attachments are handled using a very strange way in Elastic Email. They must first be uploaded // to their API and THEN appended here. Therefore, you should limit how many attachments you have $attachmentIds = array(); $attachments = $this->extractAttachments($message); foreach ($attachments as $attachment) { $attachmentIds[] = $this->uploadAttachment($attachment); } if (count($attachmentIds)) { $parameters['attachments'] = implode(';', $attachmentIds); } $response = $this->prepareHttpClient('/mailer/send', $parameters)->send(); return $this->parseResponse($response); }
/** * {@inheritDoc} * @link http://developer.postmarkapp.com/developer-build.html * @throws Exception\RuntimeException if the mail is sent to more than 20 recipients (Postmark limit) * @return array The id and UID of the sent message (if sent correctly) */ public function send(Message $message) { $from = $message->getFrom(); if (count($from) !== 1) { throw new Exception\RuntimeException('Postmark API requires exactly one from sender'); } $parameters = array('From' => $from->rewind()->toString(), 'Subject' => $message->getSubject(), 'TextBody' => $this->extractText($message), 'HtmlBody' => $this->extractHtml($message)); $countRecipients = count($message->getTo()); $to = array(); foreach ($message->getTo() as $address) { $to[] = $address->toString(); } $parameters['To'] = implode(',', $to); $countRecipients += count($message->getCc()); $cc = array(); foreach ($message->getCc() as $address) { $cc[] = $address->toString(); } $parameters['Cc'] = implode(',', $cc); $countRecipients += count($message->getBcc()); $bcc = array(); foreach ($message->getBcc() as $address) { $bcc[] = $address->toString(); } $parameters['Bcc'] = implode(',', $bcc); if ($countRecipients > self::RECIPIENT_LIMIT) { throw new Exception\RuntimeException(sprintf('You have exceeded limitation for Postmark count recipients (%s maximum, %s given)', self::RECIPIENT_LIMIT, $countRecipients)); } $replyTo = $message->getReplyTo(); if (count($replyTo) > 1) { throw new Exception\RuntimeException('Postmark has only support for one Reply-To address'); } elseif (count($replyTo)) { $parameters['ReplyTo'] = $replyTo->rewind()->toString(); } if ($message instanceof PostmarkMessage) { if ($message->getTag()) { $parameters['Tag'] = $message->getTag(); } } $attachments = $this->extractAttachments($message); foreach ($attachments as $attachment) { $parameters['Attachments'][] = array('Name' => $attachment->filename, 'ContentType' => $attachment->type, 'Content' => base64_encode($attachment->getRawContent())); } $response = $this->prepareHttpClient('/email')->setMethod(HttpRequest::METHOD_POST)->setRawBody(json_encode($this->filterParameters($parameters)))->send(); return $this->parseResponse($response); }
/** * Prepare the subject line string * * @param Message $message * @return string */ protected function prepareSubject(Message $message) { return $message->getSubject(); }
public function testSubjectIsMutable() { $this->message->setSubject('test subject'); $subject = $this->message->getSubject(); $this->assertEquals('test subject', $subject); }
/** * @param Message $message * @param DateTime|null $sendAt * @throws Exception\RuntimeException * @return array */ private function parseMessage(Message $message, DateTime $sendAt = null) { $hasTemplate = $message instanceof MandrillMessage && null !== $message->getTemplate(); $from = $message->getFrom(); if ($hasTemplate && count($from) > 1 || !$hasTemplate && count($from) !== 1) { throw new Exception\RuntimeException('Mandrill API requires exactly one from sender (or none if send with a template)'); } $from = $from->rewind(); $parameters['message'] = array('subject' => $message->getSubject(), 'text' => $this->extractText($message), 'html' => $this->extractHtml($message), 'from_email' => $from ? $from->getEmail() : '', 'from_name' => $from ? $from->getName() : ''); foreach ($message->getTo() as $to) { $parameters['message']['to'][] = array('email' => $to->getEmail(), 'name' => $to->getName(), 'type' => 'to'); } foreach ($message->getCc() as $cc) { $parameters['message']['to'][] = array('email' => $cc->getEmail(), 'name' => $cc->getName(), 'type' => 'cc'); } foreach ($message->getBcc() as $bcc) { $parameters['message']['to'][] = array('email' => $bcc->getEmail(), 'name' => $bcc->getName(), 'type' => 'bcc'); } $replyTo = $message->getReplyTo(); if (count($replyTo) > 1) { throw new Exception\RuntimeException('Mandrill has only support for one Reply-To address'); } elseif (count($replyTo)) { $parameters['message']['headers']['Reply-To'] = $replyTo->rewind()->toString(); } if ($message instanceof MandrillMessage) { if ($hasTemplate) { $parameters['template_name'] = $message->getTemplate(); foreach ($message->getTemplateContent() as $key => $value) { $parameters['template_content'][] = array('name' => $key, 'content' => $value); } // Currently, Mandrill API requires a content for template_content, even if it is an // empty array if (!isset($parameters['template_content'])) { $parameters['template_content'] = array(); } } foreach ($message->getGlobalVariables() as $key => $value) { $parameters['message']['global_merge_vars'][] = array('name' => $key, 'content' => $value); } foreach ($message->getVariables() as $recipient => $variables) { $recipientVariables = array(); foreach ($variables as $key => $value) { $recipientVariables[] = array('name' => $key, 'content' => $value); } $parameters['message']['merge_vars'][] = array('rcpt' => $recipient, 'vars' => $recipientVariables); } $parameters['message']['metadata'] = $message->getGlobalMetadata(); foreach ($message->getMetadata() as $recipient => $variables) { $parameters['message']['recipient_metadata'][] = array('rcpt' => $recipient, 'values' => $variables); } foreach ($message->getOptions() as $key => $value) { $parameters['message'][$key] = $value; } foreach ($message->getTags() as $tag) { $parameters['message']['tags'][] = $tag; } foreach ($message->getImages() as $image) { $parameters['message']['images'][] = array('type' => $image->type, 'name' => $image->filename, 'content' => base64_encode($image->getRawContent())); } } $attachments = $this->extractAttachments($message); foreach ($attachments as $attachment) { $parameters['message']['attachments'][] = array('type' => $attachment->type, 'name' => $attachment->filename, 'content' => base64_encode($attachment->getRawContent())); } if (null !== $sendAt) { // Mandrill needs to have date in UTC, using this format $sendAt->setTimezone(new DateTimeZone('UTC')); $parameters['send_at'] = $sendAt->format('Y-m-d H:i:s'); } return $parameters; }
public function testMessageReturnsNonEncodedSubject() { $this->message->setSubject('This is a subject'); $this->message->setEncoding('UTF-8'); $this->assertEquals('This is a subject', $this->message->getSubject()); }
/** * @param Message $message * @return bool */ public function send(Message $message) { $this->email->addTo($message->getTo()->current()->getEmail(), $message->getTo()->current()->getName())->setFrom($message->getFrom()->current()->getEmail(), $message->getFrom()->current()->getName())->setSubject($message->getSubject())->setHtml($message->getBodyText()); $this->sendGrid->send($this->email); return true; }
/** * Don't See In Subject * * Look for the absence of a string in an email subject * * @return void * @author Koen Punt **/ protected function dontSeeInEmailSubject(Message $email, $unexpected) { $this->assertNotContains($unexpected, $email->getSubject(), "Email Subject Does Not Contain"); }
/** * {@inheritDoc} * @link http://sendgrid.com/docs/API_Reference/Web_API/mail.html * @return mixed */ public function send(Message $message) { $from = $message->getFrom(); if (count($from) !== 1) { throw new Exception\RuntimeException('SendGrid API requires exactly one from sender'); } if (count($message->getCc())) { throw new Exception\RuntimeException('SendGrid does not support CC addresses'); } $parameters = array('from' => $from->rewind()->getEmail(), 'fromname' => $from->rewind()->getName(), 'subject' => $message->getSubject(), 'text' => $this->extractText($message), 'html' => $this->extractHtml($message)); foreach ($message->getTo() as $address) { $parameters['to'][] = $address->getEmail(); } foreach ($message->getBcc() as $address) { $parameters['bcc'][] = $address->getEmail(); } $replyTo = $message->getReplyTo(); if (count($replyTo) > 1) { throw new Exception\RuntimeException('SendGrid has only support for one Reply-To address'); } elseif (count($replyTo)) { $parameters['replyto'] = $replyTo->rewind()->getEmail(); } $client = $this->prepareHttpClient('/mail.send.json'); // Set Parameters as POST, since prepareHttpClient() put only GET parameters $client->setParameterPost($parameters); // Eventually add files. This cannot be done before prepareHttpClient call because prepareHttpClient // reset all parameters (response, request...), therefore we would loose the file upload $post = $client->getRequest()->getPost(); $attachments = $this->extractAttachments($message); foreach ($attachments as $attachment) { $post->set('files[' . $attachment->filename . ']', $attachment->getRawContent()); } $response = $client->setMethod(HttpRequest::METHOD_POST)->setEncType(HttpClient::ENC_FORMDATA)->send(); return $this->parseResponse($response); }
/** * {@inheritDoc} * @link http://documentation.mailgun.com/api-sending.html * @return string id of message (if sent correctly) */ public function send(Message $message) { $from = $message->getFrom(); if (count($from) !== 1) { throw new Exception\RuntimeException('Postage API requires exactly one from sender'); } $parameters = array('from' => $from->rewind()->toString(), 'subject' => $message->getSubject(), 'text' => $this->extractText($message), 'html' => $this->extractHtml($message)); $to = array(); foreach ($message->getTo() as $address) { $to[] = $address->toString(); } $parameters['to'] = implode(',', $to); $cc = array(); foreach ($message->getCc() as $address) { $cc[] = $address->toString(); } $parameters['cc'] = implode(',', $cc); $bcc = array(); foreach ($message->getBcc() as $address) { $bcc[] = $address->toString(); } $parameters['bcc'] = implode(',', $bcc); $attachments = $this->extractAttachments($message); foreach ($attachments as $attachment) { $parameters['attachment'][] = $attachment->filename; } if ($message instanceof MailgunMessage) { $options = $message->getValidOptions(); foreach ($message->getOptions() as $key => $value) { $parameters[$options[$key]] = $value; } $tags = $message->getTags(); if (count($tags) > 0) { $parameters['o:tag'] = $tags; } $variables = $message->getRecipientVariables(); if (count($variables)) { // It is only possible to add variables for recipients that exist in the To: field foreach ($variables as $recipient => $variable) { if (!$message->getTo()->has($recipient)) { throw new Exception\RuntimeException(sprintf('The email "%s" must be added as a receiver before you can add recipient variables', $recipient)); } } $parameters['recipient-variables'] = json_encode($variables); } } $client = $this->prepareHttpClient('/messages', $parameters); // Eventually add files. This cannot be done before prepareHttpClient call because prepareHttpClient // reset all parameters (response, request...), therefore we would loose the file upload $attachments = $this->extractAttachments($message); foreach ($attachments as $attachment) { $client->setFileUpload($attachment->filename, 'attachment', $attachment->getRawContent(), $attachment->type); } $client->setEncType(HttpClient::ENC_FORMDATA); $response = $client->send(); $result = $this->parseResponse($response); return $result['id']; }
/** * Create `Mandrill message` from `ZF message`. * * @param ZendMessage $zfMessage * * @return Message */ public static function convertZFMail(ZendMessage $zfMessage) { $mandrillMessage = new self(); // GET HTML MIME PART $messageHtmlPart = $messageTextPart = null; $mimeParts = $zfMessage->getBody()->getParts(); foreach ($mimeParts as $mimePart) { switch ($mimePart->getType()) { case ZendMime::TYPE_HTML: $messageHtmlPart = $mimePart->getContent(); break; case ZendMime::TYPE_TEXT: $messageTextPart = $mimePart->getContent(); break; } } // text part if ($messageTextPart !== null) { $mandrillMessage->text = $messageTextPart; } // html part if ($messageHtmlPart !== null) { $mandrillMessage->html = $messageHtmlPart; } // subject if ($zfMessage->getSubject() !== null) { $mandrillMessage->subject = $zfMessage->getSubject(); } // from if ($zfMessage->getFrom()->count()) { $mandrillMessage->from_email = $zfMessage->getFrom()->current()->getEmail(); $mandrillMessage->from_name = $zfMessage->getFrom()->current()->getName(); } // reply-to if ($zfMessage->getReplyTo()->count()) { $mandrillMessage->headers = ['Reply-To' => $zfMessage->getReplyTo()->current()->getEmail()]; } // to if ($zfMessage->getTo()->count()) { foreach ($zfMessage->getTo() as $to) { $mandrillMessage->addRecipient(new Recipient($to->getEmail(), null, Recipient::RECIPIENT_TYPE_TO)); } } // cc if ($zfMessage->getCc()->count()) { foreach ($zfMessage->getCc() as $cc) { $mandrillMessage->addRecipient(new Recipient($cc->getEmail(), null, Recipient::RECIPIENT_TYPE_CC)); } } // bcc if ($zfMessage->getBcc()->count()) { foreach ($zfMessage->getBcc() as $bcc) { $mandrillMessage->addRecipient(new Recipient($bcc->getEmail(), null, Recipient::RECIPIENT_TYPE_BCC)); } } return $mandrillMessage; }