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'; }
<?php // Preparation require_once './vendor/autoload.php'; use Postmark\Models\PostmarkAttachment; use Postmark\PostmarkClient; //function sendBatchOfEmails(){ $attachment = PostmarkAttachment::fromFile(dirname(__FILE__) . '/test.jpg', 'attachment-file-name.jpg', image / jpg); //$TussenArray = $_POST; //$message = $_POST; $message = array('To' => "*****@*****.**", 'From' => "*****@*****.**", 'Cc' => "*****@*****.**", 'Subject' => "Test", 'TextBody' => "Dit is een testmail, ik hoop dat het nu eindelijk werkt.", 'Attachments' => $attachment); $client = new PostmarkClient("f92ee11a-3de9-48ff-801e-1b6efc9afcdf"); $sendResult = $client->sendEmailBatch($message); // Did it send successfully? if ($sendResult != null) { echo 'The email was sent!'; } else { echo 'The email could not be sent!'; } //}
/** * 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; } }
header("Location: index.php"); } } } } else { } include 'attachment.php'; require_once './vendor/autoload.php'; use Postmark\PostmarkClient; use Postmark\Models\PostmarkException; use Postmark\Models\PostmarkAttachment; try { // Example request $client = new PostmarkClient("f5662a2d-8885-4cd1-b0b2-65b084216b3a"); //$_FILES[file][name][type][size] $attachment = PostmarkAttachment::fromFile($target_dir . basename($_FILES["file"]["name"]), basename($_FILES["file"]["name"]), "text/plain"); $sendResult = $client->sendEmail("*****@*****.**", $_POST['tosend'], "You Have a Request (No Reply)", "* You have a job application request from:" . "<br><br><br>" . "Name: " . $_POST['aname'] . "<br><br>" . "Age: " . $_POST['age'] . "<br><br>" . "Have Work Experience: " . $_POST['experience'] . "<br><br>" . "Experience About Previous Jobs: " . $_POST['area'] . "<br><br>" . "Contact: " . $_POST['contact'] . "<br><br>" . "Email: " . $_POST['email'] . "<br><br>" . "(Attachments is provided within the email)" . "<br><br>", "Attachement body", NULL, true, NULL, NULL, NULL, NULL, [$attachment]); } 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. echo $ex->httpStatusCode; echo $ex->message; echo $ex->postmarkApiErrorCode; } catch (Exception $generalException) { // A general exception is thown if the API // was unreachable or times out. } if ($sendResult) { include 'addapply.php'; header("Location: sent.php");