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

InboundEmail->forward not working Laravel 10

Open mathewparet opened this issue 1 year ago • 5 comments

I get the below error when trying to forward email.

Error:

Symfony\Component\Mime\Message::setBody(): Argument #1 ($body) must be of type ?Symfony\Component\Mime\Part\AbstractPart, string given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php on line 23 {"exception":"[object] (TypeError(code: 0): Symfony\\Component\\Mime\\Message::setBody(): Argument #1 ($body) must be of type ?Symfony\\Component\\Mime\\Part\\AbstractPart, string given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php on line 23 at /var/www/html/vendor/symfony/mime/Message.php:45)

Code:

 Mailbox::catchall(InboundEmail $inboundEmail)
{
$inboundEmail->forward('[email protected]');
}

Looks like there was a similar issue with replies to Laravel 9 - https://github.com/beyondcode/laravel-mailbox/pull/103

mathewparet avatar Feb 19 '24 11:02 mathewparet

Is this project maintained?

I want to use it, but ... cannot use it if its not maintained.

rtconner avatar Mar 15 '24 21:03 rtconner

I had the same issue. It looks like Symfony mailer changed syntaxis (https://laracasts.com/discuss/channels/laravel/body-must-be-of-type-mimepartabstractpart-string-given).

@rtconner @mathewparet in case you haven't found a solution yet - here is what i did. It kinda works like a forward solution...

public function forwardToAdmin(InboundEmail $email): void
    {
        Mail::to(config('app.admin.email'))->send(new ForwardEmail($email));
    }
class ForwardEmail extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(public InboundEmail $inboundEmail)
    {
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: "FROM: " . $this->inboundEmail->fromName() . " (" . $this->inboundEmail->from() . ") - " . $this->inboundEmail->subject(),
        );
    }

    public function content(): Content
    {
        return new Content(
            htmlString: $this->inboundEmail->html()
        );
    }

    public function attachments(): array
    {
        return $this->inboundEmail->attachments();
    }
}

hrsa avatar May 07 '24 18:05 hrsa

Fix issues with forwarding emails since Laravel 9.0 https://github.com/beyondcode/laravel-mailbox/pull/127

bardolf69 avatar May 24 '24 02:05 bardolf69

Now just need a maintainer to approve the pull request. in the interrim if you want to fix the code yourself you can go to vendor > beyondcode > laravel-mailbox > src > InboundEmail.php and find this section (line 168 in mine)

public function forward($recipients)
    {
        return Mail::send([], [], function ($message) use ($recipients) {
            $message->to($recipients)
                ->subject($this->subject())
                ->setBody($this->body(), $this->message()->getContentType());
        });
    }

and change to

public function forward($recipients)
    {
        return Mail::send([], [], function ($message) use ($recipients) {
            $message->to($recipients)
                ->subject($this->subject())
                ->text($this->text())
                ->html($this->html());
        });
    }

bardolf69 avatar May 24 '24 02:05 bardolf69