SendGrid: Disable Clicktrack using Node - sendgrid

I am using the sendgrid-nodejs library to send emails using SendGrid.
I want to disable click-tracking on a per-email basis.
I understand that you can include an attribute within dynamic templates to disable click tracking:
Click tracking can be turned off for individual links by including the clicktracking=off attribute inside the anchor of an HTML link before the href. For example, <a clicktracking=off href="http://example.com">link text</a> would not be tracked.
However, I wish to control this programmatically.
According to SendGrid documentation, it is possible to disable click-tracking by using the clicktrack filter:
{
"filters": {
"clicktrack": {
"settings": {
"enable": 0,
"enable_text": false
}
}
}
}
Looking at the Mail constructor, it appears we have the ability to set headers. The type bindings indicate it expects header values to be a string.
headers?: { [key: string]: string }
Note: I can confirm this per SendGrid's error return (if attempting to pass an object):
{
"body": {
"errors": [
{
"message": "Invalid type. Expected: string, given: object.",
"field": "headers",
"help": "http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.headers"
}
]
}
}
Regardless of what I pass, nothing seems to have any impact. The emails are being sent successfully, but click-tracking is not being disabled.
const { SENDGRID_KEY } = process.env
const mail = require('#sendgrid/mail')
mail.setApiKey(SENDGRID_KEY)
mail.send({
headers: {
// this doesn't have any impact
"X-SMTPAPI": JSON.stringify({
filters: {
clicktrack: {
settings: {
enable: 0,
enable_text: false
}
}
}
}),
// neither does this
"filters": JSON.stringify({
clicktrack: {
settings: {
enable: 0,
enable_text: false
}
}
}),
},
to: 'somebody#email.com',
from: 'nobody#email.com',
templateId: 'd-xxxxxxxxxxxxxxxxxxxxxxxx',
dynamic_template_data: {
subject: 'Hello World'
}
})
Why isn't this working?

I've found my answer. There is a trackingSettings property available:
const { SENDGRID_KEY } = process.env
const mail = require('#sendgrid/mail')
mail.setApiKey(SENDGRID_KEY)
mail.send({
trackingSettings: {
clickTracking: {
enable: false,
enableText: false
}
},
to: 'somebody#email.com',
from: 'nobody#email.com',
templateId: 'd-xxxxxxxxxxxxxxxxxxxxxxxx',
dynamic_template_data: {
subject: 'Hello World'
}
})

Related

How to use Custom HTTP request and paginations, sort, search in Vue 2.x

I am an engineer who makes web systems in Tokyo.
I'm making a search system using Grid.js, but I faced a problem.
I don't know the solution because it's not in the documentation.
Since this system uses Vue 2.x, it uses axios.post with Custom HTTP Requset.
I was able to get the list, but I'm having trouble implementing sorting, pagination, and keyword search.
I want to send parameters by Post request.
Please tell me how to implement this.
The code is below
data() {
return {
columns: [
{name: 'user name', id: 'user_name'},
{name: 'email', id: 'email'},
],
page: {
enabled: true,
limit: 100,
server: {
body: (prev, page) => {
console.log(page) // OK, show page number 0,1,2...
return {
page: page
}
}
},
},
sort: {
},
search: {
server: {
// url: (prev, keyword) => `${prev}?q=${keyword}`
// what's this.
}
},
server: {
url: '/api/v2/users/list',
method: 'POST',
async data (opt) {
let response = await axios.post(opt.url)
return {
data: response.data.results.map(item => {
return {
username: item.username,
email: item.email,
}
}),
total: response.data.count,
}
}
},
};
OK.
Set POST payload this.
data() {
return {
columns: [
{name: 'user name', id: 'user_name'},
{name: 'email', id: 'email'},
],
page: {
enabled: true,
limit: 100,
server: {
body: (prev, page) => {
console.log(page) // OK, show page number 0,1,2...
return {
page: page
}
}
},
},
sort: {
},
search: {
server: {
// url: (prev, keyword) => `${prev}?q=${keyword}`
// what's this.
}
},
server: {
url: '/api/v2/users/list',
method: 'POST',
body: {},
async data (opt) {
let response = await axios.post(opt.url)
return {
data: response.data.results.map(item => {
return {
username: item.username,
email: item.email,
}
}),
total: response.data.count,
}
}
},
};

get github issues by their ids through graphql endpoints

I am trying to get the list of issues by their ids from Github using graphql, but looks like I am missing something or its not possible.
query ($ids:['517','510']!) {
repository(owner:"owner", name:"repo") {
issues(last:20, states:CLOSED) {
edges {
node {
title
url
body
author{
login
}
labels(first:5) {
edges {
node {
name
}
}
}
}
}
}
}
}
The above query is giving me response as below,
{
"errors": [
{
"message": "Parse error on \"'\" (error) at [1, 14]",
"locations": [
{
"line": 1,
"column": 14
}
]
}
]
}
Kindly help me identify if its possible or that I am doing something wrong here.
You can use aliases in order to build a single request requesting multiple issue object :
{
repository(name: "material-ui", owner: "mui-org") {
issue1: issue(number: 2) {
title
createdAt
}
issue2: issue(number: 3) {
title
createdAt
}
issue3: issue(number: 10) {
title
createdAt
}
}
}
Try it in the explorer
which gives :
{
"data": {
"repository": {
"issue1": {
"title": "Support for ref's on Input component",
"createdAt": "2014-10-15T15:49:13Z"
},
"issue2": {
"title": "Unable to pass onChange event to Input component",
"createdAt": "2014-10-15T16:23:28Z"
},
"issue3": {
"title": "Is it possible for me to make this work if I'm using React version 0.12.0?",
"createdAt": "2014-10-30T14:11:59Z"
}
}
}
}
This request can also be simplified using fragments to prevent repetition:
{
repository(name: "material-ui", owner: "mui-org") {
issue1: issue(number: 2) {
...IssueFragment
}
issue2: issue(number: 3) {
...IssueFragment
}
issue3: issue(number: 10) {
...IssueFragment
}
}
}
fragment IssueFragment on Issue {
title
createdAt
}
The request can be built programmatically, such as in this example python script :
import requests
token = "YOUR_TOKEN"
issueIds = [2,3,10]
repoName = "material-ui"
repoOwner = "mui-org"
query = """
query($name: String!, $owner: String!) {
repository(name: $name, owner: $owner) {
%s
}
}
fragment IssueFragment on Issue {
title
createdAt
}
"""
issueFragments = "".join([
"""
issue%d: issue(number: %d) {
...IssueFragment
}""" % (t,t) for t in issueIds
])
r = requests.post("https://api.github.com/graphql",
headers = {
"Authorization": f"Bearer {token}"
},
json = {
"query": query % issueFragments,
"variables": {
"name": repoName,
"owner": repoOwner
}
}
)
print(r.json()["data"]["repository"])
I don't think you can fetch for issues and pass in an array of integers for their ids.
But you can search for a single issue by id like so (this works for me)
query ($n: Int!) {
repository(owner:"owner", name:"repo-name") {
issue (number: $n) {
state
title
author {
url
}
}
}
}
where $n is {"n": <your_number>} defined.
If you have an array of ids, then you can just make multiple queries to GitHub.
Sadly, with this approach, you cannot specify what the state of the issue to be. But I think the logic is that once you know the issue Id, you shouldn't care what state it is, since you have that exact id.

(intermediate value).sendEmail(...).promise is not a function

I'm using aws-sdk in my service to send emails. I'm getting below exception to a code which was working fine before.
const aws = require('aws-sdk');
var params = {
Destination: {
ToAddresses: [
'checkMail#gmail.com'
]
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: "HTML_FORMAT_BODY"
},
Text: {
Charset: "UTF-8",
Data: "this is sample"
}
},
Subject: {
Charset: 'UTF-8',
Data: 'Test email'
}
},
Source: 'AWS Services<awsEmails#awsService.com>'
ReplyToAddresses: [
'AwsServices<noreply#awsServices.com>'
],
};
// Create the promise and SES service object
var emailPromise = new aws.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
emailPromise.then(
function(data) {
//my logic on success goes here
}).catch(
function(err) {
//my logic on error goes here
});
I have tried using different API calls for email from AWS but all returns the same error.
Avoid require ALL aws-sdk if it's just to use a single service. For using SES, you can yarn add #aws-sdk/client-ses and then use it const { SESClient, SendEmailCommand } = require("#aws-sdk/client-ses");
I show you here a full exemple of sending email with SES in a nodeJS lambda function:
const {
SESClient,
SendEmailCommand,
} = require("#aws-sdk/client-ses");
const REGION = "eu-west-3"; // Use you AWS region
const ses = new SESClient({ region: REGION });
exports.handler = async function (event) {
const path = event.path;
if (path === "/send-email") {
const peopleAmount = 12;
const params = {
Source: "John Wick <john.wick#killer.com>",
Destination: {
ToAddresses: ["adresse1#test.com"],
},
Message: {
Body: {
Html: {
Data: `<span>This email is about <b>${peopleAmount}</b> people.</span>`,
},
},
Subject: {
Data: "Email Title",
},
},
};
try {
const data = await ses.send(new SendEmailCommand(params));
return {
statusCode: 200,
body: peopleAmount,
};
} catch (e) {
console.error(e, e.stack);
return {
statusCode: 400,
body: "Sending failed",
};
}
}
}
I hope it helps, here is documentation to use SES email template.

Unable to update Data

Am trying to update the json data through an api call.
I was able to GET the data without any issues, as am not passing any Options in the request.
For UPDATE,
//saga.js
export function* BlurideaTitler(opt) {
const id = opt.id; // 4
const updatedTitle = opt.newTitle; // "title changed"
let options = {
crossDomain: true,
method: 'PUT',
json: true,
headers: {'Content-Type': 'application/json'},
body: {
title: updatedTitle
}
};
const requestURL = `http://localhost:3000/ideas/${id}`;
try {
yield call(request, requestURL, options);
} catch (err) {
console.log(err);
}
}
// request.js
export default function request(url, options) {
return fetch(url, options)
.then(checkStatus)
.then(parseJSON);
}
//db.json
JSON am trying to update.,
{
"ideas": [
{
"id": 4,
"title": "My fourth Idea",
"body": "Description of my fourth idea",
"created_date": "14-Apr-2019"
}
]
}
This is supposed to update the value of title. But it throws error'Bad request' . Can someone please let me know what am missing here.

Sengrid template substitution tags not replaced when sending email in Meteor app

In Meteor application that incorporates Sendgrid transaction email templates for user invitations and notifications, I can't manage to replace substitution tags. Templated email is received, but without any difference.
Email.send({
from: "hello#domain.com",
to:email,
subject: "Subject",
sub: {
"{name}":post.createdBy,
"{title}":post.title,
},
headers: {
"X-SMTPAPI": {
"filters": {
"templates": {
"settings": {
"enable": 1,
"template_id": "xxxx"
}
}
}
},
"Content-Type" : "text/html"
}
});
I'm not using API directly, but rather Meteor Email package, but don't see that possible issue:
Meteor.startup(function () {
process.env.MAIL_URL = 'smtp://username:password#smtp.sendgrid.net:587';
});
This is my shortened email template:
Hey {name},
your post {title} has a new comment.
You need to put the subs in the X-SMTPAPI header as well. The X-SMTPAPI header itself should also contain valid JSON in a string.
Try this:
var xsmtpapi = {
"filters": {
"templates": {
"settings": {
"enable": 1,
"template_id": "xxxx"
}
}
},
"sub": {
"{name}": post.createdBy,
"{title}": post.title
}
}
Email.send({
from: "hello#domain.com",
to:email,
subject: "Subject",
sub: {
"{name}":post.createdBy,
"{title}":post.title,
},
headers: {
"X-SMTPAPI": JSON.stringify(xsmtpapi),
"Content-Type" : "text/html"
}
});
What I ended up doing was using smtpapi-nodejs NPM package.
The simple example would be:
var nodemailer = require('nodemailer');
var smtpapi = require('smtpapi');
var header = new smtpapi();
header.setFilters({
"templates": {
"settings": {
"enable": 1,
"template_id": xxx-template-id-xxx
}
}
});
header.addSubstitution('-name-', post.createdBy);
header.addSubstitution(-title-', post.title);
var headers = { 'x-smtpapi': header.jsonString() };
// Use nodemailer to send the email
var settings = {
host: "smtp.sendgrid.net",
port: parseInt(587, 10),
requiresAuth: true,
auth: {
user: "sendgrid_username",
pass: "sendgrid_password"
}
};
var smtpTransport = nodemailer.createTransport(settings);
var mailOptions = {
from: "Fred Foo <foo#blurdybloop.com>",
to: "bar#blurdybloop.com",
subject: "Hello",
text: "Hello world",
html: "<b>Hello world</b>",
headers: headers
}
smtpTransport.sendMail(mailOptions, function(error, response) {
smtpTransport.close();
if (error) {
console.log(error);
} else {
console.log("Message sent: " + response.message);
}
});