Skip to content

Sending Email

Eli Wood edited this page Oct 15, 2024 · 15 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>", 
			"[email protected]", 
			"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' => "[email protected]",
	    'From' => "[email protected]",
	    '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).

Sending Email with a foreach loop:

Tip

Arrays can be useful when fetching emails from a database then passing the email array to the Postmark client.

$to = ["[email protected]", "[email protected]"];

try {
    $client = new PostmarkClient("<server token>");
    $sendResult = $client->sendEmail("<sender signature>", 
        $to, 
        "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.
}

Important

Postmark limits a maximum of 50 recipients in total. This total includes all to, cc, and bcc emails.

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' => "[email protected]",
             'Cc' => "[email protected]",
             'Subject' => "Message 1",
             'TextBody' =>"Some plain text",
             'From' => "[email protected]"];

$message2 = ['To' => "[email protected]",
             'Cc' => "[email protected]",
             'Subject' => "Message 1",
             'HtmlBody' =>"<b>HELLO!</b>",
             'From' => "[email protected]"];

$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.