/** * 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(); }
/** * @param string $queue_name * @param \Zend\Mail\Message $mail */ public function add($queue_name, \Zend\Mail\Message $message) { if (!count($message->getFrom())) { $message->addFrom($this->config['default_from']); } $this->table->add($queue_name, $message); }
public function sendMail(MailMessage $message) { $from = $message->getFrom(); if (count($from) == 0) { $message->addFrom($this->createSenderAddress()); } $this->transport->send($message); }
/** * @param Message $message */ public function send(Message $message) { if (!count($message->getFrom())) { $message->setFrom('*****@*****.**', 'US Canine Scent Sports'); } $transport = new Transport\Smtp(); $transport->setOptions($this->transportOptions)->send($message); }
public function testCanSetFromListFromAddressList() { $list = new AddressList(); $list->add('*****@*****.**'); $this->message->addFrom('*****@*****.**'); $this->message->setFrom($list); $from = $this->message->getFrom(); $this->assertEquals(1, count($from)); $this->assertFalse($from->has('*****@*****.**')); $this->assertTrue($from->has('*****@*****.**')); }
/** * {@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 additional_parameters argument * * Basically, overrides the MAIL FROM envelope with either the Sender or * From address. * * @param \Zend\Mail\Message $message * @return string */ protected function prepareParameters(Mail\Message $message) { if ($this->isWindowsOs()) { return null; } $parameters = (string) $this->parameters; $sender = $message->getSender(); if ($sender instanceof AddressInterface) { $parameters .= ' -f ' . $sender->getEmail(); return $parameters; } $from = $message->getFrom(); if (count($from)) { $from->rewind(); $sender = $from->current(); $parameters .= ' -f ' . $sender->getEmail(); return $parameters; } return $parameters; }
/** * @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; }
/** * Retrieve email address for envelope FROM * * @param Message $message * @throws Exception\RuntimeException * @return string */ protected function prepareFromAddress(Message $message) { $sender = $message->getSender(); if ($sender instanceof Address\AddressInterface) { return $sender->getEmail(); } $from = $message->getFrom(); if (!count($from)) { // Per RFC 2822 3.6 throw new Exception\RuntimeException(sprintf('%s transport expects either a Sender or at least one From address in the Message; none provided', __CLASS__)); } $from->rewind(); $sender = $from->current(); return $sender->getEmail(); }
/** * @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; }
/** * {@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; }