Using Postmark with Swift Mailer on SS4.2

I attempted to set up Postmark on SS 4.2 today. I was able to send email by installing the official Swift Transport for Postmark:

$ composer require wildbit/swiftmailer-postmark ^2

And then using the following configuration:

---
Name: my-email
After:
  - '#emailconfig'
---
SilverStripe\Core\Injector\Injector:
  Swift_Transport:
    class: Postmark\Transport
    constructor:
      0: '<my-postmark-api-key>'

Big caveat though, the official v2 transport class is missing plugin and event support. This means that environment settings like SS_SEND_ALL_EMAILS_TO are ignored.

v3 looks better, but requires SwiftMailer v6, and SilverStripe is currently locked to v5. So I forked the repo and cherry picked some commits to make a franken-version compatible with SilverStripe 4.2 that supports the beforeSend event. If you want to use it too, edit your composer.json as such:

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/jonom/swiftmailer-postmark"
    }
],
"require": {
    "wildbit/swiftmailer-postmark": "dev-swift5"
}

Getting this working on SS4 was trickier than I expected so I’m sharing my process in case it helps other Postmark fans :grinning_face_with_smiling_eyes:

Update: If you need guzzle 7 you can use the swift5-guzzle7 branch. It’s a tiny change and I could probably merge that in to the swift-5 branch but I like to play it safe.

2 Likes

Awesome, thanks for this Jono :smiley:

1 Like

This no longer works as there isn’t a dev-swift5 tag anymore

Should do. The ‘dev-swift5’ branch is on my fork, not the original repo. It’s here. You need the repositories entry in your composer.json file to pick it up.

This fork is still working well to enable Postmark on Silverstripe 4, including 4.7. However, it appears that no sending errors are caught and it fails silently, usually reporting an email has been sent, when in fact it hasn’t.

I dug into this and seems that the ability to throw exceptions on Postmark API errors was added to the original repo here: Be able to throw exceptions · wildbit/swiftmailer-postmark@91afdf3 · GitHub

I’m not sure if integrating this would be as simple as applying this commit to your fork, but if not, then perhaps we could look at setting http_errors to true, here: swiftmailer-postmark/Transport.php at master · jonom/swiftmailer-postmark · GitHub which should at least stop it failing silently.

@purplespider ah that’s a bummer. v2 of wildbit/swiftmailer-postmark seemed to be missing some major pieces… hate to think what v1 looked like :grimacing:.

Hard to say if that commit can be dropped in as it’s from the next major version of wildbit/swiftmailer-postmark and the next major version of SwiftMailer.

If you have a chance, I would just drop it in and see what happens. If it works feel free to open a PR on my repo to add more stitches to this franken-version.

1 Like

It works! I opened a PR.

The code that was replaced matched the code in the original wildbits repo.

To enable Postmark API exceptions you can now just add the following to your site’s config.yml:

SilverStripe\Control\Email\SwiftMailer:
  swift_plugins:
    - Postmark\ThrowExceptionOnFailurePlugin

(Without this it will behave as before, silently failing on API errors).

Thanks @purplespider. I’ve merged and added your commit to the swift5-guzzle7 branch as well.

1 Like

So am I right that this fork is no longer required with Silverstripe 4.9, as Swiftmailer has now been upgraded to version 6?

A quick test of changing the version from "dev-swift5" to "^3" seems to work fine, including SS_SEND_ALL_EMAILS_TO.

Anything I should be aware of though?

@purplespider I haven’t tested but I think so!