Skip to content

Sending Attachments

roby94 edited this page Mar 23, 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.

You can create a PostmarkAttachment using one of two static methods on the PostmarkAttachment class, here, we use fromRawData(...):

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]);

And here, we use the fromFile(...) method to read the content of the attachment to the file system.

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");

$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]);

Also note that in the example above, we're inlining the image in the HTMLBody of the message, attachments added to messages automatically have Content-ID set to the value of the attachment name parameter (in this case, 'logo.png'). This simplifies the process of showing images inline in HTML messages.

Clone this wiki locally