resend-laravel
resend-laravel copied to clipboard
Request to the Resend API failed. Reason: Syntax error
I've reported this to resend via the message box but also reporting here.
I've got fairly standard mailables in my laravel 11 project. Every 100 emails or so I get this random error. Its happened 3x now and on different mailables. Because my jobs are set to retry a few times, this always goes out successfully on the 2nd try.
Which is even more confusing because if there was truly a syntax error we'd have another fail?
There is nothing for the fail in the resend log gui.
And I'm having a hard time where I can take it from here.
Using these versions: "resend/resend-laravel": "^0.14.0", "laravel/framework": "^11.33",
Just spotted this stacktrace in Horizon that I thought might be helpful: https://gist.github.com/flatcapco/7ae2ef10bdc4c8d7160dc333c6cf8871
@flatcapco are you using the batch-mail api by a chance?
I had the same issue when I wanted to send batch-mails via resend.
example code:
$mails = $users->map(function ($user) {
$mail = new MailClass($params); // as an example
return [
'from' => "...",
'to' => [ "..."],
'subject' => "...",
'html' => "...",
];
});
// the batch API endpoint allows 100 mails at once at most
$mails->chunk(100)->each(function($chunk){
$resend->batch->send($chunk->toArray());
});
with the snippet above the first call was always successful but all the following requests got an error:
{
"name": "validation_error",
"message": "The `request.body` field must be an `array`.",
"statusCode": 422
}
I then noticed that the index of the array chunks caused the issue. If the array doesn't start at 0, the request body is handled as an object instead of an array.
By changing
$mails->chunk(100)->each(function ($chunk) use ($resend) {
$batch = $chunk->values()->toArray(); // resetting the array-index
$resend->batch->send($batch);
});
the batch-mails worked as expected.
PS: if you are using be aware of the rate-limiting, I think its' 1 batch mail job per second.
Hey @SahinU88 I'm not using batch mail sadly so that doesn't fix it they are all queued up a separate mails
Not sure if I can help but could you provide a code snippet showing how you use the mail service?
Just like @flatcapco mentioned, it happens randomly and it doesn't add up because all other emails should fail ideally.
This exception has been bugging me for so long as well, that I have decided to mute it for now in my exception handler 🤷♂️
Having this error too, did anyone managed to resolve it?
Nope I was told its a bigger problem that they are working hard to fix.
Good to know it's not just us. We're up to 87.
Hi all, I have been experimenting with better error handling for non-JSON errors. Let me know what you think.
https://github.com/resend/resend-php/pull/77
Marking this as complete. Please let me know if this issue still occurs on the latest version of the library (v0.23.0).