GithubHelp home page GithubHelp logo

activecampaign / swiftmailer-postmark Goto Github PK

View Code? Open in Web Editor NEW
53.0 15.0 28.0 83 KB

The Official Swiftmailer Transport for Postmark.

Home Page: https://postmarkapp.com

PHP 100.00%
postmark postmark-integrations email mail

swiftmailer-postmark's Introduction

swiftmailer-postmark

Build Status

An official Swiftmailer Transport for Postmark.

Send mail through Postmark from your favorite PHP frameworks!

Usage

1. Include this package in your project:

composer require wildbit/swiftmailer-postmark

2. Use the transport to send a message:

<?php
//import the transport from the standard composer directory:
require_once('./vendor/autoload.php');

$transport = new \Postmark\Transport('<SERVER_TOKEN>');
$mailer = new Swift_Mailer($transport);

//Instantiate the message you want to send.
$message = (new Swift_Message('Hello from Postmark!'))
  ->setFrom(['[email protected]' => 'John Doe'])
  ->setTo(['[email protected]'])
  ->setBody('<b>A really important message from our sponsors.</b>', 'text/html')
  ->addPart('Another important message from our sponsors.','text/plain');

//Add some attachment data:
$attachmentData = 'Some attachment data.';
$attachment = new Swift_Attachment($attachmentData, 'my-file.txt', 'application/octet-stream');

$message->attach($attachment);

//Send the message!
$mailer->send($message);
?>

3. Throw exceptions on Postmark api errors:

$transport = new \Postmark\Transport('<SERVER_TOKEN>');
$transport->registerPlugin(new \Postmark\ThrowExceptionOnFailurePlugin());

$message = new Swift_Message('Hello from Postmark!');
$mailer->send($message); // Exception is throw when response !== 200

4. Use default headers:

You can set default headers at Transport-level, to be set on every message, unless overwritten.

$defaultHeaders = ['X-PM-Tag' => 'my-tag'];

$transport = new \Postmark\Transport('<SERVER_TOKEN>', $defaultHeaders);

$message = new Swift_Message('Hello from Postmark!');

// Overwriting default headers
$message->getHeaders()->addTextHeader('X-PM-Tag', 'custom-tag');

5. Setting the Message Stream:

By default, the "outbound" transactional stream will be used when sending messages.

// Change the default stream for every message via Default Headers
$transport = new \Postmark\Transport('<SERVER_TOKEN>', ['X-PM-Message-Stream' => 'your-custom-stream']);

$message = new Swift_Message('Hello from Postmark!');

// Overwrite the default stream for a specific message by setting the header
$message->getHeaders()->addTextHeader('X-PM-Message-Stream', 'another-stream');

6. Getting the Postmark Message ID after sending

After sending the mail to Postmark, it is possible to get the Postmark ID.

$transport = new \Postmark\Transport('<SERVER_TOKEN>', $defaultHeaders);
$mailer = new Swift_Mailer($transport);

$message = new Swift_Message('Hello from Postmark!');

$mailer->send($message);

$postmarkID = $message->getHeaders()->get('X-PM-Message-Id')->getValue();

Releasing a new version

Swiftmailer Transport uses packagist. To publish a new version, simply create a new release on GitHub tagged with the version number.

Notes:

  • The Transport uses the Postmark API internally to send mail, via the /email endpoint. Other sending features such as Batch sending or sending via Templates are currently not supported by this library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.