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

Sending variables into templates pre-loaded into Mailgun #51

Open
JamieW87 opened this issue Sep 14, 2022 · 2 comments
Open

Sending variables into templates pre-loaded into Mailgun #51

JamieW87 opened this issue Sep 14, 2022 · 2 comments

Comments

@JamieW87
Copy link

JamieW87 commented Sep 14, 2022

I had an issue with trying to pass variables into a template that was in the Mailgun UI using the send() command.

Following the documentation for ts-mailgun, nothing would transfer the data across to the template when using the templateVars object in send().

After looking through the mailgun documentation, i found this example curl for sending an email.

curl -X POST https://api.mailgun.net/v3/example.com/messages 
      -F template="billing" 
      -H "X-Mailgun-Variables: {"firstName": "John",
"lastName": "Doe",
	"number": "12345",
	"data":"Mar 01, 2019",
	"amount1": "23.00",
	"amount2": "10.20",
	"total": "33.20"}"

Yet i noticed in dist/ts-mailgun.js that the send body is this:

// Create message parts
            var message = {
                from: _this.fromTitle + " <" + _this.fromEmail + ">",
                to: to,
                subject: subject,
                html: body,
                'recipient-variables': templateVars
            };

By changing recipient-variables to h:X-Mailgun-Variables I achieved the desired result and the variables passed through successfully.

I am happy to open a PR with the changes if that is suitable?

@DrewImm
Copy link
Member

DrewImm commented Sep 15, 2022

Recipient variables is for sending a one-off email to many recipients; the HTML template is passed along with the send() command https://documentation.mailgun.com/en/latest/user_manual.html#batch-sending-1

This was prior to Mailgun supporting templates stored in mailgun; so the parameter name templateVars is now ambiguous/misleading

To use a template stored in mailgun, you should be able to with:

mailer.send(
	'[email protected]',
	'Hello',
	'', // Body is blank
	{}, // Recipient vars is blank
	{ // Send options parameter allows passing info directly to Mailgun
		template: 'my-template',
		'h:X-Mailgun-Variables': ...
	}
);

https://www.npmjs.com/package/ts-mailgun#mail-options

Please let me know if this works or not and I can troubleshoot. Any PRs or suggestions to improve this functionality or to improve the documentation would be greatly appreciated!

@JamieW87
Copy link
Author

Thanks @DrewImm , I made the necessary code changes and this worked fine.
I did try to create a branch yesterday but it said I didn't have the right privileges. Is this something you need to enable?
I was just going to tackle this specific use case in the docs.

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