How to use custom mailgun variable inside email template? - mailgun

I'm using mailgun-js library to send emails from nodejs server.
How I can populate html email teamplate, which sends through mailgun, with some custom variable?
For recipient-variables it works like %recipient.custom-variable% in html template and with such code on server side:
let email = {
from: 'my#email.com',
to: emailsArray,
subject: 'my subject',
html: template,
'recipient-variables': emailsArray.map(e=>{e:{custom-variable: user.customThing}})
};
return mailgun.create(email);
but I have some variable which should be the same for all recipients.
How I can set the custom variable and how I can use it inside template (in manner of recipient-variable)?

It seems like it's not possible to use custom variables, only with recipient-variables.

Related

AWS Cognito - Custom email message with Lambda trigger being overwritten

I'm using a lambda function to customize confirmation emails with AWS Cognito. My lambda function seems to work fine and looks like this:
exports.handler = async (event, context, callback) => {
const sampleTemplate = `<html>
<body>
<div>${event.request.codeParameter}</div>
<div>${event.userName}</div>
</body>
</html>`
if (event.triggerSource === "CustomMessage_AdminCreateUser") {
event.response.emailSubject = 'Lets hope this works'
event.response.emailMessage = sampleTemplate
console.log(event.response) // Logs look as expected
}
callback(null, event);
};
The problem is that when the emails arrive the message body is being overwritten by the content in the User Pools > Message Customizations tab. The subject line is working fine, but the email body is being overwritten. For example, the cognito settings look like this:
And the emails look like this:
As you can see, the lambda function worked for setting the email's subject line, but not the actual content. I can't find any setting to turn off that content and it can't be left blank... Any help is much appreciated.
For anyone finding this, I was able to find the answer. When using the CustomMessage_AdminCreateUser event, cognito will silently throw an error if you use event.userName in the template. Instead use event.request.usernameParameter and it will work
If after doing everything, your custom email template doesn't show up then check the following:
Verification type is set to "code" not "link". ( Your Pool > General settings > message customizations ) Ref: https://github.com/amazon-archives/amazon-cognito-identity-js/issues/521#issuecomment-358383440
Email HTML template shouldn't exceed more than 20,000 UTF characters. Ref: https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-custom-message.html#aws-lambda-triggers-custom-message-example
Check this tutorial - https://medium.com/#hasnat_33856/customize-your-cognito-verification-emails-with-html-pages-using-lambda-function-e6fff7ebfb94
We MUST include both the "{username}" and "{####}" placeholder for the custom template for CustomMessage_AdminCreateUser to work. We can place this ourselves in the html file or via the event object's values for the keys event.request.usernameParameter and event.request.codeParameter respectively.
In summary, the html file for CustomMessage_AdminCreateUser must include these two values:
event.request.usernameParameter (has value "{username}") and
event.request.codeParameter (has value "{####}")

How can I add an unsubscribe link in a single email with Mailgun using SMTP?

I am having trouble figuring out how to put an unsubscribe link in my email that is SMTP integrated with Mailgun.
Any thoughts?
Mailgun provides you with several unsubscribe variables:
1) %unsubscribe_url% -- link to unsubscribe recipient from all messages sent by given domain
2) %tag_unsubscribe_url% -- link to unsubscribe from all tags provided in the message
3) %mailing_list_unsubscribe_url% -- link to unsubscribe from future messages sent to a mailing list
If you include these variables in your emails, any recipient that clicks on the url will be automatically unsubscribed and those email addresses will be blocked from receiving future emails from that domain or message tag as appropriate.
reference https://documentation.mailgun.com/user_manual.html#tracking-unsubscribes
You can use it in a href link:
Click here to unsubscribe
Instead of giving entire domain as %unsubscribe_url% , it is better to add %tag_unsubscribe_url% only, so that all the tags which are part for the current email will get unscubscribe.
Here is the code
HTML link will be like
<a style="color: #8798AD;margin: 0;text-decoration: none;" href="%tag_unsubscribe_url%">Unsubscribe</a>
This will add unsubscribe link to all tags which are part of the current send email request.
so, it my send email request has 2 tags (i.e. Orage and Mango) this two tag will get unsubscribe by mailgun.
So if you send any email which contains one of these tag, then that email will not get deliver to client.
Java code example:
String from = "alpesh#jikadra.com";
String to = "alpesh#stack.com";
String subject="Sample email";
String replyTo="no-reply#jikadra.com";
String html = "<body><h1>Hello</h1><a style="color: #8798AD;margin: 0;text-decoration: none;" href="%tag_unsubscribe_url%">Unsubscribe</a></body>";
List<String> tags = new ArrayList<String>();
tags.add("Mango");
tags.add("Orange");
MailGunRequest mailGunRequest = new MailGunRequest(from, to, subject, html, tags, replyTo);
ResponseEntity<MailGunResponse> mailGunResponseResponseEntity = mailGunClient.sendEmail(mailGunRequest);
for more reference refere : https://documentation.mailgun.com/en/latest/user_manual.html#sending-via-api

Translate email messages

I would like to translate email messages. I have a method in my model which is called after_save. This method creates mailer:
MyMailer.delay.notify_on_new_object self
The mailer uses mail message to send it:
mail(to: #email, subject: t(:subject, scope: "mailers.object"))
The problem is that every time english version of email is sent. No matter which locale is set in request. I know that request informations are not passed to models. So how can I set proper locale from request before sending email?
Have you tried something like:
I18n.locale = :es # or a locale variable given to the mailer
mail(to: #email, subject: t(:subject, scope: "mailers.object"))

How to use Backbone collection's create method correctly

I found that when I use the collection.create to create a new model, backbone will send a post request, but the post data is incorrect
for example
collection.create({name:'test'})
backbone will send POST data using "{name:'test'}" as key, and "" as value,
but I want the POST data by using name as key, 'test' as value,
can anybody no how to setting it,
I use django as the server
thanks in advance
Unless you change it backbone's collections use Backbone.sync to communicate with your backend.
In the docs they say:
With the default implementation, when Backbone.sync sends up a
request to save a model, its attributes will be passed, serialized as
JSON, and sent in the HTTP body with content-type application/json
So I guess you need to do something like this in your django view
json.load(request.POST)
or use a custom sync function that does not serialize the data to json
You'll need to parse the raw post data string and parse it into a python dict.
import json
data = json.loads(request.raw_post_data)
You can also set
Backbone.emulateJSON = true;
as per http://backbonejs.org/#Sync-emulateJSON

How to send data as key - value pairs instead of string via POST using XHR

I'm creating two POST calls. One using a django form and one using angular js via a resource xhr.
The angular setup looks like this:
myModule.factory('gridData', function($resource) {
//define resource class
var root = {{ root.pk }};
var csrf = '{{ csrf_token }}';
return $resource('{% url getJSON4SlickGrid root.pk %}:wpID/', {wpID:'#id'},{
get: {method:'GET', params:{}, isArray:true},
update:{method:'POST', headers: {'X-CSRFToken' : csrf }}
});
});
With creating an xhr post request as such:
item.$update();
This post request is send to the server as expected, but when I want to access the QueryDict I cannot access the data passed using:
name = request.POST.get('name', None)
name is always None like this.
The issue behind this is that the QueryDict object is getting parsed quite strange.
print request.POST
<QueryDict: {u'{"name":"name update","schedule":0"}':[u'']}>
Whereas I would have expected this result, which I got when I send the data via a "normal" Post request:
<QueryDict: {u'name': [u'name update'], u'schedule': [u'0']}>
So it seems to be that Django receives something in the POST request which instructs Django to parse the parameters into one string. Any idea how to circumvent this?
Update:
I found this discussion where they say that the issue is if you provide any content type other than MULTIPART_CONTENT the parameters will be parsed into one string. I checked the content-type send with the POST request and it is really set to 'CONTENT_TYPE': 'application/json;charset=UTF-8'. Thus this is likely the issue. Therefore my question is: How can I set the CONTENT_TYPE for a xhr post request created using angular.js resources to MULTIPART_CONTENT?
you could either:
fiddle with the client to send data instead of json
use json.loads(request.raw_post_data).get('name', None) (django < 1.4)
use json.loads(request.body).get('name', None) (django >= 1.4)
The Angular documentation talks about transforming requests and responses
To override these transformation locally, specify transform functions as transformRequest and/or transformResponse properties of the config object. To globally override the default transforms, override the $httpProvider.defaults.transformRequest and $httpProvider.defaults.transformResponse properties of the $httpProvider.
you can find an example here as was previously pointed at.