Skip to content

Sending Email

Vlad-Cosmin Sandu edited this page Mar 24, 2020 · 14 revisions

The Postmark API allows sending and tracking emails with as little as a To, From, Subject, and Body. The API is powerful, and supports the inclusion of Custom Headers, HTML bodies, attachments, and automatic open & read tracking. Let's cover some common uses:

Sending a Simple Email:

function sendTestMessage(){
	try {
		$client = new PostmarkClient("<server token>");
		$sendResult = $client->sendEmail("<sender signature>", 
			"ben@example.com", 
			"Hello from Postmark!",
			"This is just a friendly hello from your friends at Postmark.");
	} catch {
		// Calls to the $client can throw an exception 
		// if the request to the API times out.
	}
}

Note: By default, the message will be sent via the default transactional Message Stream ("outbound").

Sending Complex Emails:

use Postmark\Models\PostmarkAttachment;
use Postmark\PostmarkClient;

function sendBatchOfEmails(){
	$attachment = PostmarkAttachment::fromFile
	                  (dirname(__FILE__). '/test.jpg', 'attachment-file-name.jpg', image/jpg');
	$message = [
	    'To' => "recipient@example.com",
	    'From' => "sender@example.com",
	    'TrackOpens' => true,
	    'Subject' => "A complex email",
	    'TextBody' => "Plain Text Body",
	    'HtmlBody' => "<html><body><img src=\"cid:attachment-file-name.jpg\"/></body></html>",
	    'Tag' => "New Year's Email Campaign",
	    'Headers' => [ "X-CUSTOM-HEADER" => "Header content"],
	    'Attachments' => [$attachment],
	    'MessageStream' => "outbound" // here you can set your custom Message Stream
	];

	$client = new PostmarkClient("<server token>");

	$sendResult = $client->sendEmailBatch([$message]);
}

Take special note that you can enable and disable message tracking (only messages with an HTML body can be tracked). You may also set custom headers, as well include attachments (Here, it's referenced by the img tag of the HTML email body).

Send a Batch of Email:

You may send an Array of message objects in one call to the API. This is more efficient than sending emails individually, but consider bandwidth and message payload limits to determine how many messages to send at once:

//Create messages:
$message1 = ['To' => "someone@someplace.com",
             'Cc' => "someoneelse@someplace.com",
             'Subject' => "Message 1",
             'TextBody' =>"Some plain text",
             'From' => "you@yourdomain.com"];

$message2 = ['To' => "someone@someplace.com",
             'Cc' => "someoneelse@someplace.com",
             'Subject' => "Message 1",
             'HtmlBody' =>"<b>HELLO!</b>",
             'From' => "you@yourdomain.com"];

$newClient = new PostmarkClient("server_token");

//Pass the messages as an array to the `sendEmailBatch` function.
$responses = $newClient->sendEmailBatch([$message1, $message2]);

foreach($responses as $key=>$response){
	echo $response;
}

For more information about what fields can be set on messages, refer to the Postmark developer documentation.