Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request to the Resend API failed. Reason: Syntax error #71

Open
flatcapco opened this issue Dec 8, 2024 · 6 comments
Open

Request to the Resend API failed. Reason: Syntax error #71

flatcapco opened this issue Dec 8, 2024 · 6 comments

Comments

@flatcapco
Copy link

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
Copy link
Author

flatcapco commented Dec 9, 2024

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

@SahinU88
Copy link

SahinU88 commented Dec 28, 2024

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

@flatcapco
Copy link
Author

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

@SahinU88
Copy link

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

@opheus2
Copy link

opheus2 commented Jan 5, 2025

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.

@Cannonb4ll
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants