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

InboundEmail->forward not working Laravel 10 #122

Open
mathewparet opened this issue Feb 19, 2024 · 5 comments
Open

InboundEmail->forward not working Laravel 10 #122

mathewparet opened this issue Feb 19, 2024 · 5 comments

Comments

@mathewparet
Copy link

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
@rtconner
Copy link

Is this project maintained?

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

@hrsa
Copy link

hrsa commented May 7, 2024

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

bardolf69 commented May 24, 2024

Fix issues with forwarding emails since Laravel 9.0 #127

@bardolf69
Copy link

bardolf69 commented May 24, 2024

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

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

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

5 participants