0
votes

I've been following the Laravel Mail docs https://laravel.com/docs/5.4/mail to send email via a SparkPost account. When I try and send I'm getting

ClientException
Client error: `POST https://api.sparkpost.com/api/v1/transmissions` resulted in a `401 Unauthorized` response:
{"errors": [ {"message": "Unauthorized."} ]}

As per the docs I've got the sparkpost key in config/services.php and though the docs don't mention anything about it (why not!?) a bit of Googling convinced me to set the mail driver etc. in my .env file. I then have a web route to check the config being used,

Route::get('test', function()
{
    dd(Config::get('mail'));
});

which gives

array:9 [▼
  "driver" => "sparkpost"
  "host" => "smtp.sparkpostmail.com"
  "port" => "587"
  "from" => array:2 [▶]
  "encryption" => "tls"
  "username" => "my-sparkpost-account-login-email-address"
  "password" => "my-sparkpost-account-login-password"
  "sendmail" => "/usr/sbin/sendmail -bs"
  "markdown" => array:2 [▶]
]

I actually want to use a mailable and views which I've set up, but just to test the sending I bypassed these and set the following route to test:

Route::get('/sparkpost', function () {
  Mail::send('emails.test', [], function ($message) {
    $message
      ->from('[email protected]', 'Me')
      ->to('my-email-address', 'My-Name')
      ->subject('A Test Email');
  });
});

which got the error at the top (testing with my mailables and views gave the exact same error which makes me think they are correctly set up - assuming the above test route is correct).

The on thing I'm wondering about is I'm testing on my local machine, so test URL is http://localhost/seg/public/sparkpost - I'm wondering if this is tripping SparkPost up (i.e. do I have to actually be sending from a server running at the same domain as the sending domain configured in sparkpost, or is it just the sending address in the email that has to match?)

Thanks for any suggestions!

1
The JSON you post does not seem to align with your settings. The Error is from the REST HTTP interface but the setting appear to be for SMTP. I think you need to find the REST settings and set those with the username/apiKey. Also be sure the user name is set to "SMTP_Injection"Yepher
And you don't have to be sending from a server on the same domain you setup. That error is almost always either you did not give the API key proper permissions, or the username/password are not configured properly.Yepher

1 Answers

0
votes

if you use smtp driver,

You need go to Account > SMTP Relay to get credentials for SMTP, it's not your sparkpost account credentials.

if you use sparkpost driver,

you need put api key of sparkpost into config/services.php, there is no need to input smtp username and password if you use sparkpost driver