Javascript 折叠JSON字符串以发送标头变量-Mailgun
我正在尝试使用JS Mailgun API发送电子邮件。让它正常工作,直到我像这样将模板变量放入“h:X-Mailgun-variables”中,其中jsonString非常大(17000多个字符): 引用,这建议我通过定期插入CRLF字符来“折叠”JSON。这让我想到了这一点,但仍然不起作用,尽管日志记录确实显示了规则的换行符,并且符合JSON:Javascript 折叠JSON字符串以发送标头变量-Mailgun,javascript,json,email,http-headers,mailgun,Javascript,Json,Email,Http Headers,Mailgun,我正在尝试使用JS Mailgun API发送电子邮件。让它正常工作,直到我像这样将模板变量放入“h:X-Mailgun-variables”中,其中jsonString非常大(17000多个字符): 引用,这建议我通过定期插入CRLF字符来“折叠”JSON。这让我想到了这一点,但仍然不起作用,尽管日志记录确实显示了规则的换行符,并且符合JSON: const jsonString = JSON.stringify(templateVars).split('},').join('},\r \n'
const jsonString = JSON.stringify(templateVars).split('},').join('},\r \n');
对如何正确“折叠”我的JSON以便在我的邮件中使用大型模板变量有何见解
更新:
根据请求,添加我的代码。当数据只有几个公司/帖子时,这种方法有效,但当我有很多公司/帖子时,我会得到400个错误:
function dispatchEmails(data) {
const DOMAIN = 'test.net';
const mg = mailgun({apiKey: API_KEY, domain: DOMAIN});
const templateVars = {
date: theDate,
previewText: 'preview',
subject: 'subject',
subhead: 'subhead',
companies: data.companies.map(company => {
return {
url: company.url,
totalParts: data.totalParts,
currentPart: data.currentPart,
companyData: {
name: company.name,
website: company.website,
description: company.description
},
posts: _.map(company.news, item => {
return {
category: item.category,
date: new Date(item.date),
url: item.sourceUrl,
title: item.title,
source: item.publisherName,
description: item.description,
}
})
}
})
};
const jsonString = JSON.stringify(templateVars).split('},').join('},\r \n');
const mailData = {
from: 'test@test.com',
to: 'recipient@test.com',
subject: 'subject',
template: 'template',
'h:X-Mailgun-Variables': jsonString
};
return mg.messages().send(mailData)
.then(body => {
return body;
})
.catch(err => {
return {error: err};
});
}
只是跳出框框思考,但为什么要在电子邮件标题中传递那么多数据?我假设你在接收端有一些东西将解析电子邮件头。如果您不向他们发送数据,而是向他们发送一个密钥,他们可以将该密钥回调到您端的API中,以获取数据您是否看过这个?我尝试过那些编码方法,但仍然没有成功。我知道我的json格式正确,因为如果我输入一个较短的版本,它就会通过。但当对象较大时,它会出错。因此,我正在寻找一种方法来正确地“折叠”我的json,以符合Mailgun的规范。我觉得我现在已经正确地实现了(见帖子),但仍然不起作用。这可能并不总是起作用,因为折叠要求对象的数据不大。您需要至少在您的应用程序中提供一个示例json结构question@TarunLalwani刚刚用生成JSON的代码进行了更新。我尝试折叠,因为Mailgun文档告诉我,如果发送的是很长的JSON,就应该使用它,所以假设这就是解决方案。但要接受建议/建议!也许你应该联系他们的支持团队
const jsonString = JSON.stringify(templateVars).split('},').join('},\r \n');
function dispatchEmails(data) {
const DOMAIN = 'test.net';
const mg = mailgun({apiKey: API_KEY, domain: DOMAIN});
const templateVars = {
date: theDate,
previewText: 'preview',
subject: 'subject',
subhead: 'subhead',
companies: data.companies.map(company => {
return {
url: company.url,
totalParts: data.totalParts,
currentPart: data.currentPart,
companyData: {
name: company.name,
website: company.website,
description: company.description
},
posts: _.map(company.news, item => {
return {
category: item.category,
date: new Date(item.date),
url: item.sourceUrl,
title: item.title,
source: item.publisherName,
description: item.description,
}
})
}
})
};
const jsonString = JSON.stringify(templateVars).split('},').join('},\r \n');
const mailData = {
from: 'test@test.com',
to: 'recipient@test.com',
subject: 'subject',
template: 'template',
'h:X-Mailgun-Variables': jsonString
};
return mg.messages().send(mailData)
.then(body => {
return body;
})
.catch(err => {
return {error: err};
});
}