Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Classes \Postman\Library\Transport

joebeeson edited this page Apr 8, 2011 · 4 revisions

Transports are the "work horse" of the library in that they handle the actual communication of an Email object to an endpoint. To ensure consistency across all transport classes an interface implementation (\Postman\Interfaces\Transport) is required by the library for all transport objects.

Along with the actual communication a transport will handle the "translation" from the Email object into a manner which can be understood by the endpoint it is communicating with. This covers the email content, recipients, subject and attachments. During this process the transport should not modify the actual Email as it may be reused by the user.

Example

An example of this translation process can be seen in the Postmark transport which builds a JSON payload which conforms to the Postmark API:

protected function _buildJsonPayload(\Postman\Library\Email $email) {
	return json_encode(array_merge(
		$this->__buildJsonPayloadRecipients($email),
		$this->__buildJsonPayloadAttachments($email),
		array(
			'From' 		=> $email->getFrom()->getAddress(),
			'Subject'	=> $email->getSubject(),
			'HtmlBody'	=> $email->getHtmlBody(),
			'TextBody'	=> $email->getTextBody(),
			'ReplyTo'	=> $email->getReplyTo()
		)
	));
}

This method is used by the send method to help build the expected payload that will be sent to the Postmark servers.

Notes

The send method should always return a boolean to indicate the success of the operation.

If an endpoint has limitations on the specifics it can handle (e.g., no attachment support) the transport should silently impose the restriction and log the issue.

The Email object provides many useful methods for inspecting the various information attached to it. Be sure to utilize these methods instead of attempting to access the variables directly as they are declared protected.

If a setting is absolutely required for a transport to operate (e.g., an API key) the transport should throw a RuntimeException upon initialization to signal that it is incorrectly configured.

All transport objects must implement the \Postman\Interfaces\Transport interface. If you intend on creating your own custom transport be sure to glance at the interface to figure what methods you'll need to provide. Alternatively you can simply extend from \Postman\Library\Transport which will bring in a variety of methods to get your class off the ground.

Related