php - Unable to create an Email from an instance of "SymfonyComponentMimeMessage" as the body is too complex -

admin2025-04-17  3

How do I solve this error: Unable to send message with the "Symfony\Component\Mailer\Transport\AbstractApiTransport" transport: Unable to create an Email from an instance of "Symfony\Component\Mime\Message" as the body is too complex.

Below is my code

use Twig\Environment;
use App\Core\MailStruct;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mime\Email;
use Twig\Loader\FilesystemLoader;
use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Crypto\SMimeSigner;
use Symfony\Contracts\Service\Attribute\Required;

   public function __construct(MailerInterface $mailer)
    {
        $this->mailer = $mailer;
    }

   public function send(MailStruct $mail): void
    {
        
        $email = (new TemplatedEmail())
        ->from('mailgun@*****.mailgun')
        ->to('b***@yahoo')
        ->replyTo('mailgun@*****.mailgun')
        ->priority(Email::PRIORITY_HIGH)
        ->subject('Time for Symfony Mailer!')
        ->htmlTemplate('test.html.twig')
        ->locale('de')
        ->context([
            'expiration_date' => '77',
            'username' => 'stellaGregory',
            'address' => '6 Kamasojo',
        ]);

        $loader = new FilesystemLoader('../templates/');
        $twigEnv = new Environment($loader);
        $twigBodyRenderer = new BodyRenderer($twigEnv);
        $twigBodyRenderer->render($email);

        try {
            $signer = new SMimeSigner(__DIR__.'/certificate/certificate.crt', __DIR__.'/certificate/private.key');
            $email = $signer->sign($email);
            $this->mailer->send($email);   
        } catch (\Throwable $th) {
            throw $th;
        }
    }

From the look of the code, the error emanates because of the mail is signed. If I remove the signed function, it works smoothly, but I want email service providers to prioritize my email messages, hence the email messages would remain signed.

Mailer Config: MAILER_DSN=roundrobin(mailgun+api://*****55643c7e*****f8361b5*****27c-e61ae8dd-7*****:sandboxef6*****2742745ce*****c048689*****.mailgun@default?region=us)

转载请注明原文地址:http://anycun.com/QandA/1744902331a89238.html