beyondcode / laravel-mailbox

Catch incoming emails in your Laravel application
https://beyondco.de/docs/laravel-mailbox/getting-started/introduction
MIT License
1.04k stars 127 forks source link

InboundEmail->forward not working Laravel 10 #122

Open mathewparet opened 9 months ago

mathewparet commented 9 months ago

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('someone@example.com');
}

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

Is this project maintained?

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

hrsa commented 6 months ago

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();
    }
}
bardolf69 commented 6 months ago

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

bardolf69 commented 6 months ago

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());
        });
    }
willcastillo commented 2 months ago

@bardolf69 Thanks for posting the solution. My 2cs:

We should create a new model that extends InboundEmail and then register it in config/mailbox.php:

App\Models'InboundEmail:

<?php

namespace App\Models;

use Illuminate\Support\Facades\Mail;
use BeyondCode\Mailbox\InboundEmail as BaseModel;

class InboundEmail extends BaseModel
{

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

config/mailbox.php: 'model' => \App\Models\InboundEmail::class,