Extends WireMail to use the Mailgun API for sending emails.
- Download the zip file at Github or clone the repo into your
site/modules
directory. - If you downloaded the zip file, extract it in your
sites/modules
directory. - In your admin, go to Modules > Refresh, then Modules > New, then click on the Install button for this module.
Prior to using this module, you must set up a domain in your Mailgun account to create an API key. Add the API key and domain to the module's settings.
Usage is similar to the basic WireMail implementation, although a few extra options are available. Please refer to the WireMail documentation for full instructions on using WireMail, and to the examples below.
The following are extra methods implemented by this module:
The following methods can be used in a chained statement:
cc(string|array|null $email) - Set a "cc" email address.
- Only used when
$batchMode
is set tofalse
. - Please refer to WireMail::to() for more information on how to use this method.
bcc(string|array|null $email) - Set a "bcc" email address.
- Only used when
$batchMode
is set tofalse
. - Please refer to WireMail::to() for more information on how to use this method.
addData(string $key, string $value) - Add custom data to the email.
- See https://documentation.mailgun.com/en/latest/user_manual.html#attaching-data-to-messages for more information.
addInlineImage(string $file, string $filename) - Add an inline image for referencing in HTML.
- Reference using "cid:" e.g.
<img src='cid:filename.ext'>
- Requires
curl_file_create()
(PHP >= 5.5.0) - See https://documentation.mailgun.com/en/latest/user_manual.html#sending-inline-images for more information.
addRecipientVariables(array $recipients) - Add/override recipient variables.
$recipients
should be an array of data, keyed by the recipient email address- See https://documentation.mailgun.com/en/latest/user_manual.html#batch-sending for more information.
addTag(string $tag) - Add a tag to the email.
- Only ASCII allowed
- Maximum length of 128 characters
- There is a maximum number of 3 tags allowed per email.
addTags(array $tags) - Add tags in a batch.
setApiKey(string $apiKey) - Override the Mailgun API Key module setting.
setBatchMode(bool $batchMode) - Enables or disables batch mode.
- This is off by default*, meaning that a single email is sent with each recipient seeing the other recipients
- If this is on, any email addresses set by
cc()
andbcc()
will be ignored - Mailgun has a maximum hard limit of recipients allowed per batch of 1,000. This module will split the recipients into batches if necessary. Read more about batch sending.
*This is set to on by default if ProMailer is installed.
setDeliveryTime(int $time) - The (unix)time the email should be scheduled for.
setDomainName(string $domain) - Override the "Domain Name" module setting.
setRegion(string $region) - Override the "Region" module setting.
- Valid regions are "us" and "eu"
- Fails silently if an invalid region is passed
setSender(string $domain, string $key) - Set a different API sender than the default.
- The third argument is
$region
which is optional - A shortcut for calling
setDomainName()
,setApiKey()
andsetRegion()
setTestMode(bool $testMode) - Override the "Test Mode" module setting.
- Disabled automatically for "Forgot Password" emails from ProcessWire
setTrackOpens(bool $trackOpens) - Override "Track Message Opens" module setting on a per-email basis.
- Open tracking only works for emails with
bodyHTML()
set - Disabled automatically for "Forgot Password" emails from ProcessWire
setTrackClicks(bool $trackClicks) - Override "Track Message Clicks" module setting on a per-email basis.
- Click tracking only works for emails with
bodyHTML()
set - Disabled automatically for "Forgot Password" emails from ProcessWire
send() - Send the email.
- Returns a positive number (indicating number of emails sent) or 0 on failure.
validateEmail(string $email) - Validates a single address using Mailgun's address validation service.
- Returns an associative array. To return the response as an object, set the second argument to false
- For more information on what this method returns, see Mailgun's documentation.
getHttpCode() - Get the API HTTP response code.
- A response code of
200
indicates a successful response
Send an email:
$mg = $mail->new();
$sent = $mg->to("[email protected]")
->from("[email protected]")
->subject("Message Subject")
->body("Message Body")
->send();
Send an email using all supported WireMail methods and extra methods implemented by WireMailgun:
$mg = $mail->new();
// WireMail methods
$mg->to([
"[email protected]" => "A User",
"[email protected]" => "Another User",
])
->from("[email protected]", "Company Name")
->replyTo("[email protected]", "Company Name")
->subject("Message Subject")
->bodyHTML("<p>Message Body</p>") // A text version will be automatically created
->header("key1", "value1")
->headers(["key2" => "value2"])
->attachment("/path/to/file.ext", "filename.ext");
// WireMailgun methods
$mg->cc("[email protected]")
->bcc(["[email protected]", "[email protected]"])
->addData("key", "value") // Custom X-Mailgun-Variables data
->addInlineImage("/path/to/file-inline.jpg", "filename-inline.jpg") // Add inline image
->addTag("tag1") // Add a single tag
->addTags(["tag2", "tag3"]) // Add tags in a batch
->setBatchMode(false) // A single email will be sent, both "to" recipients shown
->setDeliveryTime(time() + 3600) // The email will be delivered in an hour
->setSender($domain, $key, "eu") // Use a different domain to send, this one in the EU region
->setTestMode(true) // Mailgun won't actually send the email
->setTrackOpens(false) // Disable tracking opens
->setTrackClicks(false); // Disable tracking clicks
// Batch mode is set to false, so 1 returned if successful
$numSent = $mg->send();
echo "The email was " . ($numSent ? "" : "not ") . "sent.";
// If using batch mode, the recipient variable "name" is inferred from the `to` addresses, e.g.
$mg = $mail->new();
$mg->to([
"[email protected]" => "A User",
"[email protected]" => "Another User",
])
->setBatchMode(true)
->subject("Message Subject")
->bodyHTML("<p>Dear %recipient.name%,</p>")
->send();
// to = A User <[email protected]>, Another User <[email protected]>
// recipientVariables = {"[email protected]": {"name": "A User"}, "[email protected]": {"name": "Another User"}}
// bodyHTML[[email protected]] = <p>Dear A User,</p>
// bodyHTML[[email protected]] = <p>Dear Another User,</p>
// Alternatively, you can omit the `to` recipients if setting `recipientVariables` explictly e.g.
$mg = $mail->new();
$mg->setBatchMode(true)
->addRecipientVariables([
"[email protected]" => "A User", // "name" is inferred
"[email protected]" => [
"name" => "Another User",
"customVar" => "A custom variable",
],
])
->subject("Message Subject")
->bodyHTML("<p>Dear %recipient.name%,</p><p>Custom: %recipient.customVar%!</p>")
->send();
// to = A User <[email protected]>, Another User <[email protected]>
// recipientVariables = {"[email protected]": {"name": "A User"}, "[email protected]": {"name": "Another User", "customVar": "A custom variable"}}
// bodyHTML[[email protected]] = <p>Dear A User,</p><p>Custom: %recipient.customVar%!</p>
// bodyHTML[[email protected]] = <p>Dear Another User,</p><p>Custom: A custom variable!</p>
// %recipient.customVar% only prints for second email, so not a particularly useful example!
// You can also use `addRecipientVariables()` to extend/override the inferred `recipientVariables` e.g.
$mg = $mail->new();
$mg->to([
"[email protected]" => "A User",
"[email protected]" => "Another User",
])
->addRecipientVariables([
"[email protected]" => [
"title" => "A User (title)",
],
"[email protected]" => [
"name" => "Another User (changed name)",
"title" => "Another User (title)",
],
])
->setBatchMode(true)
->subject("Message Subject")
->bodyHTML("<p>Dear %recipient.name%,</p><p>Title: %recipient.title%!</p>")
->send();
// to = A User <[email protected]>, Another User (changed name) <[email protected]>
// recipientVariables = {"[email protected]": {"name": "A User", "title": "A User (title)"}, "[email protected]": {"name": "Another User (changed name)", "title": "Another User (title)"}}
// bodyHTML[[email protected]] = <p>Dear A User,</p><p>Title: A User (title)!</p>
// bodyHTML[[email protected]] = <p>Dear Another User (changed name),</p><p>Title: Another User (title)!</p>
$mg = $mail->new();
$response = $mg->validateEmail("[email protected]", false);
if($mg->getHttpCode() == 200) {
echo $response->result == "deliverable" ? "Valid" : "Not valid";
} else {
echo "Could not validate";
}
A similar module - WireMailMailgun - was initally developed by plauclair, with further development from gebeer and outflux3. WireMailgun started as a rewrite of outflux3's version, bringing the module more in line with ProcessWire conventions and coding style guide and adding some more features. WireMailgun is not compatible with these other versions.