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.

swiftmailer-postmark's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swiftmailer-postmark's Issues

text/plain body is not sent using setBody()

During testing, we discovered that a text/plain body is not sent whenever an additional part is added to the message later on.

Example (taken from Swiftmailer docs):

$message = Swift_Message::newInstance()
    ->setSubject('Your subject')
    ->setFrom(array('[email protected]'))
    ->setTo(array('[email protected]'))
    ->setBody('Here is the message itself', 'text/plain')
    ->addPart('<q>Here is the message itself</q>', 'text/html')
;

At the moment the transport just assumes that the main body of any multipart message is text/html. It seems this issue can not be resolved just yet, as the content type of the body is not exposed: swiftmailer/swiftmailer#736

As a workaround, we use setBody for HTML and addPart for text. The parts get reordered anyway.

Swift Mailer 6 compatibility

Will this package be updated for Swift Mailer 6? (Released in May.)

Looks like the only thing you’d need to add is a ping() method.

Error Handling

http_errors is set to false in the send method. How then can errors be caught e.g. network issues or postmark api issues?

Retrieve MessageID from response

Is there a way to do this currently? Other packages such as https://github.com/craigpaul/laravel-postmark add this as a header like so when the message is sent:

  $message->getHeaders()->addTextHeader(
            'X-PM-Message-Id',
            $this->getMessageId($response)
        );

I'd like to use this package as it is the Laravel recommendation but require this functionality. Would you be interested in a Pull Request?

Support for Tags and Metadata?

Hello there!

I'm using Laravel and Postmark using this adapter. I'm trying to send a message and add metadata/tags to it. Reading through the code & docs, I figured my best bet would be to add X-PM headers as per the SMTP documentation. I tried doing that:

public function build()
    {
		$this->withSwiftMessage(function (\Swift_Message $message) {
			$message->getHeaders()->addTextHeader('X-PM-Metadata-document-id', $this->document->id);
		});

		$client_name = $this->document->client->name;

		return $this->markdown('emails.documents.review')
					->attach($this->document->download_path, ['as' => $this->document->file_name])
					->subject('Document for review - ' . $client_name);
    }

The headers do get added to the message (I can see them when clicking the "raw source" tab in the UI) but Postmark doesn't seem to care for such headers when using the API.

Maybe it's possible to handle these special headers in the adapter? I see that there is some kind of precedent here.

Thank you!
Guilherme

Receiving type error for Transport::convertEmailsArray

I received this error when only setting a BCC message.

Uncaught TypeError: Argument 1 passed to Postmark\Transport::convertEmailsArray() must be of the type array, null given, called in Transport.php on line 138 and defined in Transport.php:82
Stack trace:
#0 Transport.php(138): Postmark\Transport->convertEmailsArray(NULL)
#1 Transport.php(118): Postmark\Transport->processRecipients(Array, Object(Swift_Message))
#2 Transport.php(70): Postmark\Transport->getMessagePayload(Object(Swift_Message))
#3 Transport->send(Ob

After inspecting it though it looks like it is set.

Swiftmailer 5.2

image

License

Could you please clarify the license of this pacakge please?

Swift_Mime_Message is removed since 6.0

When you install the transport in a newer Symfony project, it installs swiftmailer 6.0+ where the Swift_Mime_Message class is removed thus the following error occurs;

PHP Warning:  Uncaught ReflectionException: Class Swift_Mime_Message not found in vendor/wildbit/swiftmailer-postmark/src/Postmark/Transport.php:10

Support Guzzle 7

It was just released, but creating an issue to raise awareness :)

Use multiple postmark drivers with stream ID in Laravel application

I'm using a newsletter application (Mailcoach) where you can define two different mail drivers:

  • one for bulk emails
  • one for transx emails

Postmark also suggests sending through different streams.

This package lets you define the "Postmark Message Stream" by adding the stream id to the header. But when you use the postmark driver in Laravel, you only have the option to define the token seen here: https://github.com/laravel/framework/blob/bd67cc22af6fbc1f5745b0e3ce79e0a12b092048/src/Illuminate/Mail/MailManager.php#L325

So even if I create two postmark mail drivers, I could only use a different API token (= 2 different Postmark "servers"), but I cannot define the message stream.

If the above assumptions are correct, I could do a PR on Laravel to also get a headers array from the services.php config file. What do you think?

Wrong return type for send function

The definition of Swift_Transport->send function requires to return int. You return result of http client call.
When sending symfony do this

$count += $transport->send($message, $failedRecipients);

With your implementation it will cause

Notice: Object of class GuzzleHttp\Psr7\Response could not be converted to int

Support for Batch Email ?

I am using this package with Laravel 8 application, and would like to integrate Batch email sending. Are there plans to add Batch sending soon ?

Does not work with Laravel 8 (Guzzle 7)

composer require wildbit/swiftmailer-postmark

Using version ^3.2 for wildbit/swiftmailer-postmark
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Can only install one of: guzzlehttp/guzzle[6.5.x-dev, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[7.0.1, 6.5.x-dev].
- Can only install one of: guzzlehttp/guzzle[6.5.x-dev, 7.0.1].
- wildbit/swiftmailer-postmark 3.2.0 requires guzzlehttp/guzzle ^6.0.0 -> satisfiable by guzzlehttp/guzzle[6.5.x-dev].
- Installation request for wildbit/swiftmailer-postmark ^3.2 -> satisfiable by wildbit/swiftmailer-postmark[3.2.0].
- Installation request for guzzlehttp/guzzle (locked at 7.0.1, required as ^7.0.1) -> satisfiable by guzzlehttp/guzzle[7.0.1].

Installation failed, reverting ./composer.json to its original content.

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.