resend-laravel icon indicating copy to clipboard operation
resend-laravel copied to clipboard

Request to the Resend API failed. Reason: Syntax error

Open flatcapco opened this issue 11 months ago • 9 comments
trafficstars

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",

flatcapco avatar Dec 08 '24 17:12 flatcapco

Just spotted this stacktrace in Horizon that I thought might be helpful: https://gist.github.com/flatcapco/7ae2ef10bdc4c8d7160dc333c6cf8871

flatcapco avatar Dec 09 '24 11:12 flatcapco

@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.

SahinU88 avatar Dec 28 '24 09:12 SahinU88

Hey @SahinU88 I'm not using batch mail sadly so that doesn't fix it they are all queued up a separate mails

flatcapco avatar Dec 28 '24 14:12 flatcapco

Not sure if I can help but could you provide a code snippet showing how you use the mail service?

SahinU88 avatar Dec 28 '24 14:12 SahinU88

image Hi, We are also facing the same issue. We have had these errors over 143 times now.

Just like @flatcapco mentioned, it happens randomly and it doesn't add up because all other emails should fail ideally.

opheus2 avatar Jan 05 '25 22:01 opheus2

This exception has been bugging me for so long as well, that I have decided to mute it for now in my exception handler 🤷‍♂️

Cannonb4ll avatar Jan 14 '25 13:01 Cannonb4ll

Having this error too, did anyone managed to resolve it?

wassim avatar Jan 22 '25 11:01 wassim

Nope I was told its a bigger problem that they are working hard to fix.

flatcapco avatar Jan 22 '25 12:01 flatcapco

Good to know it's not just us. We're up to 87.

Image

austincarpenter avatar Feb 19 '25 07:02 austincarpenter

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

jayanratna avatar Jun 06 '25 03:06 jayanratna

Marking this as complete. Please let me know if this issue still occurs on the latest version of the library (v0.23.0).

jayanratna avatar Oct 18 '25 23:10 jayanratna