SparkPost Mail Transport for XF 2.3

SparkPost Mail Transport for XF 2.3 3.1.1

No permission to download
Thanks to @Sim for this wonderful add-on!

Using SparkPost EU with this add-on​

If your SparkPost account is in the EU region, you'll see this when testing the add-on out of the box:

Code:
GuzzleHttp\Exception\ClientException:
POST https://api.sparkpost.com/api/v1/transmissions/ -> 401 Unauthorized
{"errors":[{"message":"Unauthorized."}]}

Why it happens​

EU and US are separate SparkPost environments. EU API keys only work with api.eu.sparkpost.com, not api.sparkpost.com.

Below are the steps to make the add-on work with SparkPost EU (currently at: https://app.eu.sparkpost.com/).
I'm sure there are better ways, but LLM's got me lazy.

Quick self-check if needed: which region does your key work in?​

Run these in terminal (safe "sandbox" send - no real mail goes out):

Bash:
# US host
curl -sS -X POST https://api.sparkpost.com/api/v1/transmissions \
  -H "Authorization: YOUR_EU_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"options":{"sandbox":true},"content":{"from":"postmaster@yourdomain.tld","subject":"XF test","text":"hi"},"recipients":[{"address":"test@example.com"}]}'

# EU host
curl -sS -X POST https://api.eu.sparkpost.com/api/v1/transmissions \
  -H "Authorization: YOUR_EU_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"options":{"sandbox":true},"content":{"from":"postmaster@yourdomain.tld","subject":"XF test","text":"hi"},"recipients":[{"address":"test@example.com"}]}'

If US = 401 and EU = 200, your key is EU (expected). Proceed to the next step

---

Point the add-on at the EU API host​


The add-on's Symfony Mailer transport builds the host from a region parameter.
Current builds don't pass it, so it defaults to US. 2 tiny patches you can use (pick one):

Option A. - Pass the region when constructing the transport (better)​

Edit:
src/addons/Hampel/SparkPostMail/SubContainer/SparkPost.php

Change:
PHP:
return new SparkPostApiTransport($apikey, $client);

to:
PHP:
return new SparkPostApiTransport($apikey, $client, null, null, 'eu');
// or, to hardcode the host explicitly:
// return new SparkPostApiTransport($apikey, $client, null, null, null, 'api.eu.sparkpost.com');

Option B - Hardcode the EU URL at the call site (sledgehammer)​

Edit:
src/addons/Hampel/SparkPostMail/vendor/hampel/symfonymailer-sparkpost/src/Transport/SparkPostApiTransport.php

Find this line in doSendApi():
PHP:
\sprintf('https://%s/api/v1/transmissions/', $this->host)
Replace with:
PHP:
'https://api.eu.sparkpost.com/api/v1/transmissions/'

After editing, in case of opcode or some other cache, you might need to restart PHP (php-fpm / LiteSpeed / Apache / Nginx whateva') so the cache picks up the changes.

Then re-run ACP → Tools → Test SparkPost.

If the first line now shows:
Code:
POST https://api.eu.sparkpost.com/api/v1/transmissions/
you're on the right host..
 
Back
Top Bottom