Skip to content

Commit 91ad05e

Browse files
Mailgun EU Region Domains (#32)
* Added test for MAILGUN_BASEURL property * FIXED var scoping of attachments variable * Updated to make MAILGUN_APIURL optional Added support for Mailgun EU region by making MAILGUN_APIURL an optional property with https://api.mailgun.net/v3/ as the default. * Updated to handle a response that is not JSON Mailgun returns only "Forbidden" when the wrong Base URL region is used.
1 parent 1aa7be6 commit 91ad05e

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

.env.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
POSTMARK_API_KEY=
22
MAILGUN_API_KEY=
33
MAILGUN_DOMAIN=
4+
MAILGUN_BASEURL=

models/protocols/MailgunProtocol.cfc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
* - apikey : The mailgun secret api key
99
* - domain : The mailgun domain to send the email through
1010
*
11+
* An optional property, "baseURL" is required when using an EU region
12+
* - baseURL : The mailgun region where the Mailgun domain was created
13+
*
1114
* @author Scott Steinbeck <[email protected]>
1215
*/
1316
component
@@ -24,7 +27,6 @@ component
2427
MailgunProtocol function init( struct properties = {} ){
2528
variables.name = "Mailgun";
2629
variables.DEFAULT_TIMEOUT = 30; // in seconds
27-
variables.MAILGUN_APIURL = "https://api.mailgun.net/v3/";
2830
// super size it
2931
super.init( argumentCollection = arguments );
3032

@@ -40,6 +42,14 @@ component
4042
throw( message = "ApiKey is Required", type = "MailgunProtocol.PropertyNotFound" );
4143
}
4244

45+
// Check for Base URL property
46+
if ( !propertyExists( "baseURL" ) ) {
47+
// No baseURL key was found, so use the US default.
48+
variables.MAILGUN_APIURL = "https://api.mailgun.net/v3/";
49+
} else {
50+
variables.MAILGUN_APIURL = getProperty( "baseURL" );
51+
}
52+
4353
return this;
4454
}
4555

@@ -101,7 +111,7 @@ component
101111
data.delete( "bodyTokens" ); // cleanup payload
102112

103113
// Process the mail attachments and encode them how mailgun likes them
104-
attachments = arguments.payload
114+
var attachments = arguments.payload
105115
.getMailParams()
106116
.filter( function( thisParam ){
107117
return structKeyExists( arguments.thisParam, "file" );
@@ -185,7 +195,14 @@ component
185195
}
186196

187197
// Inflate HTTP Results
188-
var mailgunResults = deserializeJSON( httpResults.fileContent.toString() );
198+
if( isJSON( httpResults.fileContent.toString() ) ) {
199+
var mailgunResults = deserializeJSON( httpResults.fileContent.toString() );
200+
} else {
201+
results.messages = [ 'Error sending mail. Mailgun returned "#httpResults.fileContent.toString()#".' ];
202+
203+
return results;
204+
}
205+
189206
// Process Mailgun Results
190207
if ( mailgunResults.message eq "Queued. Thank you." ) {
191208
results.error = false;

test-harness/tests/specs/protocols/MailgunProtocolTest.cfc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ component extends="coldbox.system.testing.BaseTestCase" {
1414
variables.mailservice = getInstance( "MailService@cbmailservices" );
1515
variables.apikey = getUtil().getSystemSetting( "MAILGUN_API_KEY", "MAILGUN_API_KEY" );
1616
variables.domain = getUtil().getSystemSetting( "MAILGUN_DOMAIN", "MAILGUN_DOMAIN" );
17+
variables.baseURL = getUtil().getSystemSetting( "MAILGUN_BASEURL", "MAILGUN_BASEURL" );
1718
}
1819

1920
/*********************************** BDD SUITES ***********************************/
@@ -23,15 +24,17 @@ component extends="coldbox.system.testing.BaseTestCase" {
2324
describe( "Mailgun Protocol", function(){
2425
beforeEach( function( currentSpec ){
2526
// Create a mock instance of the protocol.
26-
variables.protocol = createMock( "cbmailservices.models.protocols.MailgunProtocol" ).init( {
27-
apiKey : variables.apikey,
28-
domain : variables.domain
27+
variables.protocol = createMock( "cbmailservices.models.protocols.MailgunProtocol" ).init( {
28+
apiKey : variables.apikey,
29+
domain : variables.domain,
30+
baseURL : variables.baseURL
2931
} );
3032
} );
3133

3234
it( "can be inited correctly", function(){
3335
expect( variables.protocol.propertyExists( "apiKey" ) ).toBeTrue();
3436
expect( variables.protocol.propertyExists( "domain" ) ).toBeTrue();
37+
expect( variables.protocol.propertyExists( "baseURL" ) ).toBeTrue();
3538
} );
3639

3740
it( "data is formatted for sending to mailgun", function(){

0 commit comments

Comments
 (0)