function testClientCanSendBatchMessages() { $tk = parent::$testKeys; $currentTime = date("c"); $batch = array(); $attachment = PostmarkAttachment::fromRawData("attachment content", "hello.txt", "text/plain"); for ($i = 0; $i < 5; $i++) { $payload = array('From' => $tk->WRITE_TEST_SENDER_EMAIL_ADDRESS, 'To' => $tk->WRITE_TEST_EMAIL_RECIPIENT_ADDRESS, 'Subject' => "Hello from the PHP Postmark Client Tests! ({$currentTime})", 'HtmlBody' => '<b>Hi there! (batch test)</b>', 'TextBody' => 'This is a text body for a test email.', 'TrackOpens' => true, 'Headers' => array("X-Test-Header" => "Test Header Content", 'X-Test-Date-Sent' => date('c')), 'Attachments' => array($attachment)); $batch[] = $payload; } $client = new PostmarkClient($tk->WRITE_TEST_SERVER_TOKEN, $tk->TEST_TIMEOUT); $response = $client->sendEmailBatch($batch); $this->assertNotEmpty($response, 'The client could not send a batch of messages.'); }
public function sendPostmarked($to, $from, $subject, $htmlContent, $plainContent, $attachedFiles, $customheaders, $signature = null) { if (empty($signature)) { $signature = PostmarkSignature::get()->filter('Email', $from)->first(); } if (!$signature) { $signature = PostmarkSignature::get()->filter('IsDefault', 1)->first(); } $client = new PostmarkClient(SiteConfig::current_site_config()->PostmarkToken); $customerIDs = PostmarkHelper::client_list()->filter('Email', explode(',', $to))->column('ID'); $message = null; if (is_array($customerIDs) && !empty($customerIDs) && self::$record_emails) { $message = new PostmarkMessage(array('Subject' => $subject, 'Message' => $htmlContent, 'PlainMessage' => $plainContent, 'ToID' => implode(',', $customerIDs), 'FromID' => $signature ? $signature->ID : $signature, 'InReplyToID' => self::$reply_to_message)); $message->write(); } $cc = null; if (isset($customheaders['CC'])) { $cc = $customheaders['CC']; unset($customheaders['CC']); } if (isset($customheaders['cc'])) { $cc = $customheaders['cc']; unset($customheaders['cc']); } $bcc = null; if (isset($customheaders['Bcc'])) { $bcc = $customheaders['Bcc']; unset($customheaders['Bcc']); } if (isset($customheaders['BCC'])) { $bcc = $customheaders['bcc']; unset($customheaders['BCC']); } if (isset($customheaders['bcc'])) { $bcc = $customheaders['bcc']; unset($customheaders['bcc']); } $attachments = null; if ($attachedFiles && is_array($attachedFiles)) { $attachments = array(); foreach ($attachedFiles as $attachment) { $attachments[] = PostmarkAttachment::fromRawData($attachment['contents'], $attachment['filename'], $attachment['mimetype']); if (is_array($customerIDs) && !empty($customerIDs) && self::$record_emails) { $postmarkAttachment = new Attachment(array('Content' => base64_encode($attachment['contents']), 'FileName' => $attachment['filename'], 'ContentType' => $attachment['mimetype'], 'PostmarkMessageID' => $message->ID, 'Length' => strlen($attachment['contents']))); $postmarkAttachment->write(); } } } $arrangedHeaders = null; if ($customheaders && is_array($customheaders) && count($customheaders)) { $arrangedHeaders = array(); foreach ($customheaders as $key => $val) { $arrangedHeaders[$key] = $val; } } $sendResult = $client->sendEmail($signature ? $signature->Email : $from, $to, $subject, $htmlContent, $plainContent, null, true, $message ? $message->replyToEmailAddress() : $from, $cc, $bcc, $arrangedHeaders, $attachments); if (is_array($customerIDs) && !empty($customerIDs) && self::$record_emails && $sendResult->__get('message') == 'OK') { $message->MessageID = $sendResult->__get('messageid'); $message->write(); } return $sendResult->__get('message') == 'OK'; }
/** * Send email through Postmark's REST API * * @return bool (true = sent successfully) */ private function sendPostmarkEmail($to, $from, $subject, $htmlContent = NULL, $attachedFiles = NULL, $customHeaders = NULL, $plainContent = NULL) { $apiKey = $this->config()->get('api_key'); $senderSignatures = $this->config()->get('sender_signatures'); $cc = NULL; $bcc = NULL; $replyTo = NULL; if (!$apiKey) { user_error('A Postmark App API key is required to send email', E_USER_ERROR); } if (!($htmlContent || $plainContent)) { user_error("Can't send email with no content", E_USER_ERROR); } if (empty($senderSignatures)) { user_error('At least one Postmark App sender signature is required to send email', E_USER_ERROR); } // Parse out problematic custom headers if (is_array($customHeaders)) { if (array_key_exists('Cc', $customHeaders)) { $cc = $customHeaders['Cc']; unset($customHeaders['Cc']); } if (array_key_exists('Bcc', $customHeaders)) { $bcc = $customHeaders['Bcc']; unset($customHeaders['Bcc']); } if (array_key_exists('Reply-To', $customHeaders)) { $replyTo = $customHeaders['Reply-To']; unset($customHeaders['Reply-To']); } if (empty($customHeaders)) { $customHeaders = NULL; } } else { $customHeaders = NULL; } // Ensure from address is valid if (!in_array($from, $senderSignatures)) { // Fallback to first valid signature if (!$replyTo) { $replyTo = $from; } $from = $senderSignatures[0]; } // Set up attachments $attachments = array(); if ($attachedFiles && is_array($attachedFiles)) { foreach ($attachedFiles as $f) { $attachments[] = PostmarkAttachment::fromRawData($f['contents'], $f['filename'], $f['mimetype']); } } // Send the email try { $client = new PostmarkClient($apiKey); $sendResult = $client->sendEmail($from, $to, $subject, $htmlContent, $plainContent, $tag = NULL, $trackOpens = true, $replyTo, $cc, $bcc, $customHeaders, $attachments); return true; } catch (PostmarkException $ex) { // If client is able to communicate with the API in a timely fashion, // but the message data is invalid, or there's a server error, // a PostmarkException can be thrown. user_error("Postmark Exception: {$ex->message} (Error code: {$ex->postmarkApiErrorCode})", E_USER_WARNING); return false; } catch (Exception $generalException) { // A general exception is thown if the API // was unreachable or times out. user_error('Postmark API was unreachable or timed out', E_USER_WARNING); return false; } }