postalserver / postal-php

A PHP library for the Postal e-mail platform
66 stars 26 forks source link

NoRecipients error when trying to send RawMessage #18

Open craigrileyuk opened 1 month ago

craigrileyuk commented 1 month ago

Postal Server: self-hosted 3.3.4

Trying to send a message using the raw RFC 2822 spec and I'm getting a "NoRecipients" error back from the API.

$message = new RawMessage();

$message->mailFrom($this->attrs->from);
$message->rcptTo($recipient);
$message->data($data);

$this->client->send->raw($message);

Everything seems formatted properly if I dump the message before it leaves, but the API always returns an error.

{
    "data": {
        "message": "NoRecipients: There are no recipients defined to receive this message",
        "code": 0,
        "file": "/Users/username/Code/Valet/project/vendor/postal/postal/src/Client.php",
        "line": 78,
        "trace": [
            {
                "file": "/Users/username/Code/Valet/project/vendor/postal/postal/src/Client.php",
                "line": 40,
                "function": "validateResponse",
                "class": "Postal\\Client",
                "type": "->",
                "args": [
                    {}
                ]
            },
            {
                "file": "/Users/username/Code/Valet/project/vendor/postal/postal/src/SendService.php",
                "line": 33,
                "function": "prepareResponse",
                "class": "Postal\\Client",
                "type": "->",
                "args": [
                    {},
                    "Postal\\Send\\Result"
                ]
            },
            {
                "file": "/Users/username/Code/Valet/project/packages/package/src/Transport.php",
                "line": 127,
                "function": "raw",
                "class": "Postal\\SendService",
                "type": "->",
                "args": [
                    {
                        "attributes": {
                            "rcpt_to": [
                                "my.name@mydomain.co.uk"
                            ],
                            "mail_from": "Postal <postal@mydomain.co.uk>",
                            "data": "base64data="
                        }
                    }
                ]
            },
        ]
    }
}
craigrileyuk commented 1 month ago

I got it to send making the following changes to this package:

    // SendService.php line 30
    public function raw(RawMessage $message): Result
    {
        return $this->client->prepareResponse(
            $this->client->getHttpClient()->post('send/raw', [
                'json' => $message->attributes,
            ]),
            Result::class,
        );
    }
willpower232 commented 1 month ago

Thanks for letting us know about this, looks like the other message got refactored away from using an attributes array.

Would you be able to test out a similar refactor in the RawMessage class?

craigrileyuk commented 1 month ago

Yeah sure.

craigrileyuk commented 1 month ago

Pull request version sends fine now

willpower232 commented 1 month ago

Thanks for the work, I've also added in a test for the endpoint URIs so should help people in the future. You should be able to updated to 2.0.2 and confirm it still works and feel free to close when its all settled.