f500 / swiftmailer-sparkpost
A SwiftMailer transport implementation for the SparkPost API.
Installs: 62 993
Dependents: 1
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 8
Open Issues: 2
Requires
- ext-json: *
- php-http/guzzle7-adapter: ^1.0
- sparkpost/sparkpost: ^2.0
- swiftmailer/swiftmailer: ^6.0
Requires (Dev)
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^2.7
README
A SwiftMailer transport implementation for the SparkPost API.
It uses the official SparkPost PHP library.
It adds support for some SparkPost specific features to SwiftMailer messages.
Installation
composer require f500/swiftmailer-sparkpost
Usage
$transport = SwiftSparkPost\Transport::newInstance('API-KEY'); $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance() ->setFrom('me@domain.com', 'Me') ->setTo(['john@doe.com' => 'John Doe', 'jane@doe.com']) ->setSubject('...') ->setBody('...'); $sent = $mailer->send($message);
Specialized messages
$message = SwiftSparkPost\Message::newInstance() ->setFrom('me@domain.com', 'Me') ->setTo(['john@doe.com' => 'John Doe', 'jane@doe.com']) ->setSubject('...') ->setBody('...') ->setCampaignId('...') ->setPerRecipientTags('john@doe.com', ['...']) ->setMetadata(['...' => '...']) ->setPerRecipientMetadata('john@doe.com', ['...' => '...']) ->setSubstitutionData(['...' => '...']) ->setPerRecipientSubstitutionData('john@doe.com', ['...' => '...']) ->setOptions(['...']);
Configuration
$config = SwiftSparkPost\Configuration::newInstance(); $transport = SwiftSparkPost\Transport::newInstance('API-KEY', $config); $mailer = Swift_Mailer::newInstance($transport);
Override recipients
Override all To
, Cc
and Bcc
addresses, but leave name and per-recipient properties intact.
john@doe.com
becomes override@domain.com
.
$config = SwiftSparkPost\Configuration::newInstance() ->setRecipientOverride('override@domain.com');
Gmail style
john@doe.com
becomes override+john-doe-com@domain.com
.
$config = SwiftSparkPost\Configuration::newInstance() ->setRecipientOverride('override@domain.com') ->setOverrideGmailStyle(true);
Options for all messages
$config = SwiftSparkPost\Configuration::newInstance() ->setOptions([ SwiftSparkPost\Option::TRANSACTIONAL => false, SwiftSparkPost\Option::OPEN_TRACKING => false, SwiftSparkPost\Option::CLICK_TRACKING => false, SwiftSparkPost\Option::SANDBOX => true, SwiftSparkPost\Option::SKIP_SUPPRESSION => true, SwiftSparkPost\Option::INLINE_CSS => true, SwiftSparkPost\Option::IP_POOL => 'some-ip-pool', ]);
These options are also available for messages, where they take precedence over the configured options.
$message = SwiftSparkPost\Message::newInstance() ->setOptions(['...']);
IP pool probability
Add a probability factor to enable the IP pool only for a percentage of messages sent. 0 will never use the IP pool, 1 will always use it.
Can be used to facilitate an IP warming process.
$config = SwiftSparkPost\Configuration::newInstance() ->setOptions([SwiftSparkPost\Option::IP_POOL => 'some-ip-pool']) ->setIpPoolProbability(0.5);