resend / resend-laravel

Resend's Official Laravel SDK
https://resend.com/docs/send-with-laravel
MIT License
87 stars 5 forks source link

Argument #2 ($value) must be of type string, null given #38

Closed ndijkstra closed 1 year ago

ndijkstra commented 1 year ago

I'm getting this error from time to time:

Symfony\Component\Mime\Header\Headers::addTextHeader(): Argument #2 ($value) must be of type string, null given, called in /vendor/symfony/mime/Header/Headers.php on line 152

This is triggered by vendor/resend/resend-laravel/src/Transport/ResendTransportFactory.php: 66

jayanratna commented 1 year ago

I'll take a look at the issue. Is it possible for you to provide the code for your Mailable class or API request payload if you're using the Facade.

ndijkstra commented 1 year ago

Thanks for your swift reply! Is this enough for you?

$mailer->send('emails.empty', [
    'msg'      => $this->data['message'],
    'subject'  => $this->data['subject'],
    'fromName' => $this->data['fromName'],
    'logo'     => ($this->data['logo'] ?? null) !== null ? Storage::url($this->data['logo']) : null,
    'locale'   => $this->data['locale'] ?? null,
], function ($m) {
    $m->from($this->data['fromEmail'], $this->data['fromName']);

    $email = explode(',', $this->data['email']);

    $m->to(trim($email[0]), $this->data['name'])->subject($this->data['subject']);

    if (isset($this->data['replyToEmail']) && $this->data['replyToEmail']) {
        $m->replyTo($this->data['replyToEmail'], $this->data['replyToName'] ?? null);
    }
});
jayanratna commented 1 year ago

This is perfect thanks! Give me a day or two while I look into this for you. I think you might also be able to refactor this code to be more readable. I'll update you soon.

jayanratna commented 1 year ago

@ndijkstra I wasn't able to reproduce the error on my end. Could you please send through some dummy data for $this->data that fails?

ndijkstra commented 1 year ago

I see there are some 422 errors visible in the Resend logs. It should have thrown that exception, instead of this error about null passing in the header.

Response body:

{
    "statusCode": 422,
    "name": "invalid_parameter",
    "message": "Invalid `from` field. The email address needs to follow the `email@example.com` or `Name <email@example.com>` format"
}
jayanratna commented 1 year ago

You're absolutely right about the error being returned as an exception. I actually already have a fix for that waiting for review: https://github.com/resendlabs/resend-php/pull/35

@bukinoshita could you please review the above PR as soon as you can. ❤️

jayanratna commented 1 year ago

@ndijkstra could you please try reinstall resend/resend-laravel and ensure that resend/resend-php 0.7.2 is installed? This will fix the invalid_parameter exception issue.