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

Ambiguity with variables, documentation and Swiftmailer API #5

Closed
clement-michelet opened this issue Oct 25, 2017 · 3 comments
Closed

Comments

@clement-michelet
Copy link

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 ?

@clement-michelet clement-michelet changed the title Ambiguity with variables Ambiguity with variables, documentation and Swiftmailer API Oct 25, 2017
@Nightbr
Copy link
Member

Nightbr commented Oct 25, 2017

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:

protected static function prepareHeaders(Swift_Mime_Message $message, $mailjetHeaders)
{
$messageHeaders = $message->getHeaders();
$mailjetData = [];
foreach (array_keys($mailjetHeaders) as $headerName) {
/** @var \Swift_Mime_Headers_UnstructuredHeader $value */
if (null !== $value = $messageHeaders->get($headerName)) {
// Handle custom headers
$mailjetData[$mailjetHeaders[$headerName]] = $value->getValue();
// remove Mailjet specific headers
$messageHeaders->removeAll($headerName);
}
}
return $mailjetData;
}

@Nightbr
Copy link
Member

Nightbr commented Oct 26, 2017

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 tehplague/swiftmailer-mailgun-bundle#47

@Nightbr
Copy link
Member

Nightbr commented Oct 27, 2017

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

@Nightbr Nightbr closed this as completed Oct 27, 2017
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

2 participants