我正在使用带有Node js +库C&& c = std::move(v.front());
// ...
delete &c._a; // destroy object
和@sendgrid/mail
的Sendgrid的新API(v3)。
我要实现的目标:每周向我的所有联系人(除那些取消订阅该群组的人)发送摘要。我使用通过Sendgrid创建的模板,这要归功于它的template_id和动态模板数据,以便用每周数据填充邮件。
问题:
我可以使用/v3/marketing/singlesends向我的所有联系人发送带有模板的市场营销活动,但是不能与动态模板数据一起发送。
我可以一次发送一封包含模板+动态模板数据的电子邮件,但是为此,我需要首先检索我的所有联系人,而我只能从该端点/v3/marketing/contacts检索最后50个联系人(他们禁用了分页)。我可能已经能够从此端点/contactdb/recipients检索到所有这些文件,问题是我在他们发布新API后创建了我的Sendgrid帐户,因此我无法访问它。
关于如何执行此操作的任何想法?
当前代码:
1)配置广告系列
@sendgrid/client
2)发送广告系列
const sgClient = require('@sendgrid/client')
var params={
'name': 'Weekly Digest #'+nb,
'sender_id': sg_sender_id,
'suppression_group_id': sg_unsub_group_id,
'template_id': id_template,
'dynamicTemplateData': template_params,
//also tried with 'dynamic_template_data' instead, not working
'filter': {'send_to_all': true}
}
let url='/v3/marketing/singlesends/' + campaign_id.toString('base64')
const request = {
method: 'PATCH',
url: url,
body: params
}
sgClient.setApiKey(config.sendgrid_key)
sgClient.request(request)
.then(([response, body]) => {
console.log('body:', body)
})
.catch(error => {
console.error('error:', error.toString())
})
答案 0 :(得分:0)
我找到了Mailchimp问题的解决方案,因此现在可以准确地解决该问题。 截至2020年9月,不可能通过SendGrid API做到这一点,但是您可以像我一样做类似的事情。我的答案是基于此处的这篇文章:https://stackoverflow.com/a/53096852/4163583
我的目标是在编辑器中设计某些部分,并希望通过API填充动态内容。因此,我在不同位置添加了几行代码,以添加动态代码。
export const sendOutDailyDigest = async () => {
const campaignDetails = {
// Details about your campagin e.g. subject or sender
};
try {
// I'm retrieving here my data from the DB
const data: any = await retrieveDataForDailyDigest();
// This is the data that I want to inject in the template
const dynamicData = {
producthunt: fillProducthuntProductTable(data.producthunt),
betalist: fillBetalistProductTable(data.betalist),
crunchbase: fillCrunchbaseTable(data.crunchbase),
yesterdayDate: `Yesterday's Highlights`,
};
// Sending out the campaign
await sendOutMailchimpCampaign(
campaignDetails,
MAILCHIMP_LIST,
MC_SEGMENT_DAILY_DIGEST,
DAILY_DIGEST_TEMPLATE_ID,
dynamicData
);
} catch (error) {
console.error("Error during sendOutDailyDigest ", error);
}
};
export const fillProducthuntProductTable = (products: any) => {
let tablebody = "";
products.forEach((p: any) => {
const productRow = `
<tr>
<td><a href="${p.url}" alt="">${p.title}</a></td>
<td>${p.description}</td>
<td>${p.category}</td>
<td>${p.upvotes}</td>
</tr>
`;
tablebody += productRow;
});
return `
<table style="width:100%">
<tr>
<th>Startup Name</th>
<th>Description</th>
<th>Category</th>
<th>Upvotes</th>
</tr>
${tablebody}
</table>`;
};
sendOutMailchimpCampaign
中的工作。重要的呼叫是create campaign,update campaign content和send out campaign。
const sendOutMailchimpCampaign = async (
campaignDetails: any,
mailchimpList: string,
mailchimpSegment: number,
templateId: number,
dynamicData: any
) => {
try {
// Creating a campaign
const campaign: any = await createMailchimpCampaign(
mailchimpList,
mailchimpSegment,
campaignDetails
);
// Updating the campaign with the dynamic data that I fetched from the db
await updateCampaignContent(campaign.id, templateId, dynamicData);
// Sending out the campaign
return new Promise((resolve, reject) => {
mailchimp.campaigns
.send(campaign.id)
.then((_: any) => {
console.log("Successfully sent out daily digest.");
resolve();
})
.catch((error: any) => {
console.log("Error sending out daily digest: ", error);
reject();
});
});
} catch (error) {
console.error("Error during sendOutMailchimpCampaign ", error);
}
};
// Function for creating the above mentioned campaign like this:
export const createMailchimpCampaign = (
mailchimpList: string,
mailchimpSegment: number,
campaignDetails: any
) => {
return new Promise((resolve, reject) => {
mailchimp.campaigns
.create({
type: "regular",
recipients: {
list_id: mailchimpList,
segment_opts: {
saved_segment_id: mailchimpSegment,
},
},
settings: campaignDetails,
})
.then((response: any) => {
resolve(response);
})
.catch((error: any) => {
console.log(error);
reject(error);
});
});
};
// Function for updating the campaign with the dynamic content
const updateCampaignContent = async (
campaignId: number,
templateId: number,
dynamicData: any
) => {
return new Promise((resolve, reject) => {
mailchimp.campaigns
.setContent(campaignId, {
template: {
id: templateId,
sections: dynamicData,
},
})
.then((_: any) => {
resolve();
})
.catch((error: any) => {
console.log(error);
reject(error);
});
});
};
就是这样。代码在TypeScript中,因此我在这里向所有Javascript朋友表示歉意,但是我想您可以翻译它。我希望这可以帮助某人并节省一些时间,即使这不是SendGrid中的解决方案。