mailjet / MailjetSwiftMailer

A SwiftMailer transport implementation for Mailjet
MIT License
26 stars 23 forks source link

Ambiguity with variables, documentation and Swiftmailer API #5

Closed clement-michelet closed 7 years ago

clement-michelet commented 7 years ago

Hi,

TL;DR: Could you add a documentation on how to pass variables with Swiftmailer which is in compliance with Swiftmailer interfaces ?


From SMTP official documentation :

X-MJ-Vars - Global variables used for personalisation in a JSON format

My expectation is that I must provide a JSON encoded object with all my properties.

That expectation is confirmed by the fact that most documentations about headers use addTextHeader method (like here). The prototype of that method is the following :

    /**
     * Add a new basic text header with $name and $value.
     *
     * @param string $name
     * @param string $value
     */
    public function addTextHeader($name, $value = null);

So nothing warn me that passing a JSON encoded data is wrong at that time.

After digging, I figured out that passing JSON data doesn't work here and I need to pass array data.

So the current way of passing data is in violation with Swiftmailer interfaces. That violation creates noises on static analysis tools. Also, It makes unclear how it works.

Could you add a documentation on how to pass variables with Swiftmailer which is in compliance with Swiftmailer interfaces ?

Nightbr commented 7 years ago

Hey, the only solution so far is this:

    /**
     * Configure specific headers for message
     * @method configureHeaders
     * @param  \Swift_Message    $message
     * @return \Swift_Message
     */
    protected function configureHeaders(\Swift_Message $message, $templateId, array $vars)
    {
        //Configure Headers
        $headers = $message->getHeaders();

        $headers->addTextHeader('X-MJ-TemplateID', 'xxxxx');
        $headers->addTextHeader('X-MJ-TemplateLanguage', "true");
        $headers->addTextHeader('X-MJ-Vars', $vars);

        return $message;
    }

If you want to improve our library feel free to create a PR.

I think the problem comes from here: https://github.com/mailjet/MailjetSwiftMailer/blob/97b77e6a381dc272648a1178035d87c0dc0a3e11/SwiftMailer/MessageFormat/BaseMessagePayload.php#L64-L81

Nightbr commented 7 years ago

Hey again @clement-michelet , do you have made some further research on this subject? I set up a project where I tried to use X-MJ-Vars in Symfony (throught the MailjetBundle) and I also have an error:

Error: Method Swift_Mime_Headers_UnstructuredHeader::__toString() must not throw an exception, caught Symfony\Component\Debug\Exception\ContextErrorException: Warning: preg_split() expects parameter 2 to be string, array given

What we can do is to json_encode($vars) when we addTextHeader and in prepareHeaders, if it's a string (json) try to json_decode to create properly the payload and not trigger the SwiftMailer error.

@latanasov what do you think about this?

Note: see this from mailgun, they use json_encode https://github.com/tehplague/swiftmailer-mailgun-bundle/issues/47

Nightbr commented 7 years ago

@clement-michelet hey, I just release https://github.com/mailjet/MailjetSwiftMailer/releases/tag/1.0.6

Try it and reopen this if you have any related issue!