Skip to content

Sending Attachments

Andrew Theken edited this page Dec 13, 2017 · 6 revisions

You may add file attachments to any outgoing email provided they comply with the current white-list of accepted email attachments. You may add as many attachments as you like as long as you don't go past the total size limit of 10MB per message.

Including an Attachment with a message

The following illustrates how to add an attachment to a message that will be treated as a file.

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

$client = new PostmarkClient('<server token>');
$currentTime = date("c");

// The first parameter is the unencoded content of the attachment. 
// The PostmarkAttachment class takes care 
// of encoding as base64 JSON content before sending it to the Postmark API. 
// The second parameter is the name you wish to assign to the attachment in your 
// email message. The last parameter is the MIME-type of the attachment,
// this defaults to `application/octet-stream` if you do not specify it.
$attachment = PostmarkAttachment::fromRawData("attachment content", "hello.txt", "text/plain");

$sendResult = $client->sendEmail('sender@example.com',
	'recipient@example.com',
	"Hello from the PHP Postmark Client Tests! ($currentTime)",
	'<b>Hi there!</b>',
	'This is a text body for a test email.',
	NULL, true, NULL, NULL, NULL, NULL, [$attachment]);

Including an "inline" Attachment

The following example works similarly to the example above, except it loads the file content directly from the filesystem.

In order to maximize compatibility with the most email clients, you should also specify the $contentId (the last parameter of the PostmarkAttachment::fromFile method. cid:logo.png -- notice that this matches the src= attribute in the HTML part of the content. Including the cid: prefix instructs the Postmark API to treat this attachment as an "inline" attachment, instead of a file attachment, as was shown above. The above example can also accept the $contentId parameter.

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

$client = new PostmarkClient('<server token>');
$currentTime = date("c");

// The first argument is the absolute path to an image attachment,
// This example assumes a file called 'logo.png' is in the same directory
// as the currently executing `__FILE__`.
$attachment = PostmarkAttachment::fromFile(dirname(__FILE__) . '/logo.png',
			'logo.png', 'image/png', 'cid:logo.png');

$sendResult = $client->sendEmail('sender@example.com',
	'recipient@example.com',
	'Hello from the PHP Postmark Client Examples!',
	'<b>Hi there! This is an inlined image attachment: <img src="cid:logo.png"/></b>',
	'This is a text body for a example email.',
	NULL, true, NULL, NULL, NULL, NULL, [$attachment]);