Recipients issue
edi opened this issue · 7 comments
I have this very weird issue when trying to post a transmissioni using recipients from the API.
$response = $sparky->request( 'GET', 'recipient-lists/test-list?show_recipients=true' )->wait();
$recipients = $response->getBody()['results']['recipients'];
echo json_encode( $recipients, JSON_PRETTY_PRINT );
$transmission = $sparky->transmissions->post([
'campaign_id' => 'test_campaign',
'content' => [ 'template_id' => 'templateid' ],
'recipients' => $recipients
]);
$response = $transmission->wait();
[
{
"address": {
"email": "dulumaneduardcosmin@gmail.com",
"name": "Eduard"
},
"return_path": ""
},
{
"address": {
"email": "edi@duluman.ro",
"name": "Eduard"
},
"return_path": ""
},
{
"address": {
"email": "dulumana@gmail.com",
"name": "Test"
},
"return_path": ""
}
]
Fatal error: Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error:
POST https://api.sparkpost.com/api/v1/transmissions/
resulted in a400 Bad Request
response: { "errors": [ { "message": "At least one valid recipient is required", "code": 5002 } ] }
To note the fact that I installed this via Composer
so everything works fine.
Weird thing is that if I try using an inline recipients list ( as in manually writing the same exact array of recipients ), it goes through perfectly.
Any ideas on this one ?
Hey @Eduardjs. Try the sending the same post request but use the content fields instead of the template. Let me know what happens.
@avrahamgoldman Hello!
Array
(
[0] => Array
(
[address] => Array
(
[email] => tesas@gmail.com
[name] => Geir
)
[return_path] =>
)
[1] => Array
(
[address] => Array
(
[email] => edi@duluman.ro
[name] => Eduard
)
[return_path] =>
)
[2] => Array
(
[address] => Array
(
[email] => yrdy@gmail.com
[name] => Tor
)
[return_path] =>
)
[3] => Array
(
[address] => Array
(
[email] => dulumaneduardcosmin@gmail.com
[name] => Example
)
[return_path] =>
)
)
<br />
<b>Fatal error</b>: Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `POST https://api.sparkpost.com/api/v1/transmissions/` resulted in a `400 Bad Request` response:
{ "errors": [ { "message": "At least one valid recipient is required", "code": 5002 } ] }
' in /var/www/vhosts/site.com/email.site.com/lib/sparkpost/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107
Stack trace:
#0 /var/www/vhosts/site.com/email.site.com/lib/sparkpost/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))
#1 /var/www/vhosts/site.com/email.site.com/lib/sparkpost/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 /var/www/vhosts/site.com/email.site.com/lib/sparkpost/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)
#3 /var/www/vh in <b>/var/www/vhosts/site.com/email.site.com/lib/sparkpost/lib/SparkPost/SparkPostPromise.php</b> on line <b>69</b><br />
I have replaced the emails here with some fake ones.
@avrahamgoldman Any news on this one ?
I have used the following code which returned into the same error above.
$transmission = $sparky->transmissions->post([
'campaign_id' => 'test_1',
// 'content' => [ 'template_id' => 'test-email',
'content' => [
'from' => [
'name' => 'Bio365.no',
'email' => 'post@bio365.no',
],
'subject' => 'test email',
'html' => '<html><body><h1>test welcome!</h1><p>test first email!</p></body></html>',
],
'recipients' => $recipients
]);
And the same error as above came out. I will try json encoding the recipients list and see what happens
Later edit. I have tried json encoding the recipients, the following comes out:
Warning: Illegal string offset 'address' in /var/www/vhosts/test.com/email.test.com/lib/sparkpost/lib/SparkPost/Transmission.php on line 96
Fatal error: Uncaught exception 'Exception' with message 'Invalid address format: [' in /var/www/vhosts/test.com/emai.test.com/lib/sparkpost/lib/SparkPost/Transmission.php:151
The JSON encoded
recipients list looks like this:
[
{
"address": {
"email": "geircnb@gmail.com",
"name": "Geir"
},
"return_path": ""
},
{
"address": {
"email": "edi@duluman.ro",
"name": "Eduard"
},
"return_path": ""
},
{
"address": {
"email": "torjimm@gmail.com",
"name": "Tor"
},
"return_path": ""
},
{
"address": {
"email": "dulumaneduardcosmin@gmail.com",
"name": "Example"
},
"return_path": ""
}
]
It is very frustrating because.. it's the array I get from the API. And if you compare the JSON encoded version from the docs , the recipients
array looks exactly the same.
Please advise.
Hi @Eduardjs, sorry about the delay. It looks like the last error you got is because you passed in the recipients as a JSON encoded string - it takes an Array.
Your original problem comes from the way that the API is giving back the recipients. The API is returning each recipient with a blank return_path
. The error
{ "errors": [ { "message": "At least one valid recipient is required", "code": 5002 } ] }
is because there is no email specified in that field. The best solution for this is to strip that value out of each of the recipients.
In between getting the recipients and the transmission, loop through each recipient and unset return_path
.
for ($i = 0; $i < count($recipients); $i++) {
unset($recipients[$i]['return_path']);
}
Please let me know if you run into any other issues.
Works fine after removing the return_path
.
foreach ( $recipients as &$recipient )
unset( $recipient['return_path'] );
Took the opportunity to optimize your code a bit. Thank you very much for pointing that out.
Hello! I sent 2 emails to the support, nobody replied so I thought asking here. I have successfully sent the transmission using the code above, id 84366240151899596
. At that point in time, using GET /transmissions
would have returned the specific transmission as being submitted. Problem is that in my dashboard, I cannot see anything. The list had 24k recipients, knowing the limit is 20k daily, was the transmission "denied" ? What's the expected behaviour in this case ? I thought it would just take 20k and ignore the rest of 4 thousands.
Why would it show "submitted" if after 4 days I have nothing in my GET /transmissions
nor in the dashboard ?
Hi again @Eduardjs,
The best place to ask general questions is in our community slack. You can also check out this support article which might answer your question.