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

Can't access to html body of the email #58

Open
clemcoder opened this issue Feb 21, 2023 · 5 comments
Open

Can't access to html body of the email #58

clemcoder opened this issue Feb 21, 2023 · 5 comments

Comments

@clemcoder
Copy link

Hello,

I try to test the html content of an email (I want to check a particular link inside).
Here is the code :

        $asyncMessengerTransport = $this->messenger('async');
        $asyncMessengerQueue = $asyncMessengerTransport->queue();
        
        $asyncMessengerQueue->assertCount(1);

        $firstMessage = $asyncMessengerQueue->first()->getMessage();
        $email = $firstMessage->getMessage();
        $htmlBody = $email->getHtmlBody();

Unfortunately getHtmlBody always returns null.
Is there a probleme in this code ?
Is it possible to test the content of the email ?

Thanks.

@kbond
Copy link
Member

kbond commented Feb 21, 2023

Hi @clemcoder!

I'm wondering if it is there but during serialization, it is lost from getHtmlBody(). What does the following output?

dd($email->getBody()->toString());

Do you see the html?

Is it possible to test the content of the email ?

I recommend checking out zenstruck/mailer-test. This provides email-related assertions and supports queued emails.

@clemcoder
Copy link
Author

It outputs "Symfony\Component\Mime\Exception\LogicException: A message must have a text or an HTML part or attachments.".
I don't see the html, just the twig template name in the htmlTemplate attribute of TemplatedEmail object .
However I do see the content of the sended message in MailDev.

I didn't know about zenstruck/mailer-test, I'll take a look.

Thanks for your help.

@kbond
Copy link
Member

kbond commented Feb 21, 2023

Oh! You'll need to process the queue before you can see the html.

I think the following will work:

$asyncMessengerTransport = $this->messenger('async');
        
$asyncMessengerTransport->queue()->assertCount(1);
$asyncMessengerTransport->process();

$firstMessage = $asyncMessengerTransport->acknowledged()->first()->getMessage();
$email = $firstMessage->getMessage();
$htmlBody = $email->getHtmlBody();

@clemcoder
Copy link
Author

Nop, unfortunately already tried.
I just test your code and htmlBody always null :(.

@kbond
Copy link
Member

kbond commented Feb 21, 2023

Hmm ok, I guess processing the email doesn't manipulate the message. At the end of the day I believe this makes sense and is expected.

I guess you can't use messenger-test to test the processing of emails. zenstruck/mailer-test should work for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants