Send messages via Mailgun and synchronise with Mailgun Events API.
This module provides functionality to send emails via the Mailgun API and store events related to messages using Mailgun's webhooks feature
and...
composer require nswdpc/silverstripe-mailgun-sync
Configuration of your Mailgun domain and account is beyond the scope of this document but is straightforward.
The best starting point is Verifying a Domain.
Add the following to your project's YML config:
---
Name: local-mailgunsync-config
After:
- '#mailgunsync'
---
# API config
NSWDPC\Messaging\Mailgun\Connector\Base:
# your Mailgun mailing domain
api_domain: 'configured.mailgun.domain'
# your API key or Domain Sending Key
api_key: 'xxxx'
# the endpoint region, if you use EU set this value to 'API_ENDPOINT_EU'
# for the default region, leave empty
api_endpoint_region: ''
# this setting triggers o:testmode='yes' in messages
api_testmode: true|false
# You will probably want this as true, when false some clients will show 'Sent on behalf of' text
always_set_sender: true
# set this to override the From header, this is useful if your application sends out mail from anyone (see DMARC below)
always_from: '[email protected]'
# Whether to send via a job - see below
send_via_job: 'yes|no|when-attachments'
# When set, messages with no 'To' header are delivered here.
default_recipient: ''
# grab this from your Mailgun account control panel
webhook_signing_key: ''
# whether you want to store webhook requests
webhooks_enabled: true|false
# the current or new filter variable (see webhooks documentation in ./docs)
webhook_filter_variable: ''
# the previous one, to allow variable rotation
webhook_previous_filter_variable: ''
---
Name: local-mailer
After:
- '#emailconfig'
---
# Send messages via the MailgunMailer
SilverStripe\Core\Injector\Injector:
SilverStripe\Control\Email\Email:
class: 'NSWDPC\Messaging\Mailgun\MailgunEmail'
SilverStripe\Control\Email\Mailer:
class: 'NSWDPC\Messaging\Mailgun\MailgunMailer'
Remember to flush configuration after a YML change.
For a good example of this, look at the MailgunSyncTest class. Messages are sent using the default Silverstripe Email API:
use SilverStripe\Control\Email\Email;
$email = Email::create();
$email->setFrom($from);
$email->setTo($to);
$email->setSubject($subject);
To add custom parameters used by Mailgun you call setCustomParameters():
// variables
$variables = [
'test' => 'true',
'foo' => 'bar',
];
//options
$options = [
'testmode' => 'yes',
'tag' => ['tag1','tag2','tag4'],
'tracking' => 'yes',
'require-tls' => 'yes'
];
// headers
$headers = [
'X-Test-Header' => 'testing'
];
$recipient_variables = [
'[email protected]' => ["unique_id" => "testing_123"]
];
$args = [
'options' => $options,
'variables' => $variables,
'headers' => $headers,
'recipient-variables' => $recipient_variables
];
$email->setCustomParameters($args)
Where $args
is an array of your custom parameters. Calling setCustomParameters() multiple times will overwrite previous parameters.
Send the message:
$response = $email->send();
The response will either be a Mailgun message-id OR a Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor
instance if you are sending via the queued job.
You can send directly via the API connector, which handles client setup and the like based on configuration.
For a good example of this, look at the MailgunMailer class
use NSWDPC\Messaging\Mailgun\Connector\Message;
//set parameters
$parameters = [
'to' => ...,
'from' => ...,
'o:tag' => ['tag1','tag2']
// etc
];
$connector = Message::create();
$response = $connector->send($parameters);
The response will either be a Mailgun message-id OR a Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor
instance if you are sending via the queued job.
If you like, you can send messages and interact with the Mailgun API via the Mailgun PHP SDK:
use Mailgun\Mailgun;
$client = Mailgun::create($api_key);
// set things up then send
$response = $client->messages()->send($domain, $parameters);
The response will be a Mailgun\Model\Message\SendResponse
instance if successful.
See the Mailgun PHP SDK documentation for examples.
This is a queued job that can be used to send emails depending on the send_via_job
config value -
Messages are handed off to this queued job, which is configured to send after one minute. Once delivered, the message parameters are cleared to reduce space used by large messages.
This job is marked as 'broken' immediately upon an API or other general error.
Use this job to clear out older MailgunEvent webhook records.
Use this job to kick broken SendJob instances, which happen from time-to-time due to API or connectivity issues.
This job will:
On the next queue run, these jobs will attempt to send again.
Messages can be resent from the Mailgun control panel
When sending email it's wise to consider how you maintain the quality of your mailing domain (and IP(s)).
If your mailing domain is "mg.example.com" and you send "From: [email protected]" DMARC rules will most likely kick in at the recipient mail server and your message will be quarantined or rejected (unless example.net designates example.com as a permitted sender). Instead, use a From header of "[email protected]" or "[email protected]" in your messages.
Your Reply-To header can be any valid address.
See dmarc.org for more information on the importance of DMARC, SPF and DKIM
See ./tests
When testing this module, you probably want to avoid emails going out to the internet.
Ensure you use a Mailgun sandbox domain with approved recipients to avoid this.
Version 1 removed unused features to reduce the complexity of this module.
The core functionality is now:
Synchronisation of events is now handled by the webhooks controller
BSD-3-Clause
See LICENSE
Module rating system helping users find modules that are well supported. For more on how the rating system works visit Module standards
Score not correct? Let us know there is a problem