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

Add example of usage with laravel Mailable class #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mahdiabderraouf
Copy link

@mahdiabderraouf mahdiabderraouf commented Dec 18, 2024

Hello everyone,

Changes

In this PR i added an example on how to use this package with laravel Mailable class especially with mailjet variables.

Context

Im using this package to send mailjet emails with MJML templates containing variables {{var:name}} and ive had a hard time figuring out how to pass variables to the API call without using the client directly.

Explanation

I noticed that you are using symfony mailjet package, which uses the class Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetApiTransport, by reading the source code i found out that symfony mailjet mailer transform all headers on the Symfony\Component\Mime\Email class to the JSON payload before sending the email.
You can see: https://github.com/symfony/mailjet-mailer/blob/1387c1fa5f10f0d9fd2ef9716b47f2cadd67519a/Transport/MailjetApiTransport.php#L39

So by setting headers on the Mailable class, the email sending works with variables and other mailjet properties.

public function headers(): Headers
{
    return new Headers(
        text: [
            'X-MJ-Vars' => json_encode([
                'invoiceNumber' => $this->invoiceNumber
            ]),
            'X-MJ-TemplateLanguage' => true,
        ]
    );
}

public function content(): Content
{
    return new Content(
        htmlString: '<html><body>Hello, invoice paid {{var:name}}</body></html>',
    );
}

I'm not sure if this is an actual feature of the package or more like a workaround.
Please correct me if im wrong, thanks in advance.

Best regards,
Abderraouf MAHDI.

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

Successfully merging this pull request may close these issues.

1 participant