I'm facing a issue in sending a email, everything works cool i get response code 202 however, when i go to sendgrid activity feed i can see all the emails are dropped. By clicking on extra details it shows Invalid SMTPAPI header. Any help would be appreciated. HELP ME
@codesinghanoop would you be able to print out the JSON that you are passing to SendGrid? Please remove any API Keys or identifiable email addresses before sharing it here though!
This will help us to troubleshoot what's going on.
@mbernier Thanks for the fast reply. Below is the required object(mail.toJSON ()).
{ from: { email: '', name: undefined },
personalizations:
[ { to: [Array],
cc: undefined,
bcc: undefined,
subject: undefined,
headers: undefined,
substitutions: [Object],
custom_args: undefined,
send_at: undefined } ],
subject: 'Test email',
content: [ { type: 'text/plain', value: ' ' } ],
attachments: undefined,
template_id: '',
sections: undefined,
headers: undefined,
categories: undefined,
custom_args: undefined,
send_at: undefined,
batch_id: undefined,
asm: undefined,
ip_pool_name: undefined,
mail_settings: undefined,
tracking_settings: undefined,
reply_to: undefined }
Hi @mbernier were you able to figure out where i'm doing wrong ?
Hi @codesinghanoop,
Do you mind sharing the source code you are using to generate the call to SendGrid?
Thanks!
Elmer
Hi @thinkingserious i'm attaching my code below thanks in advance.
var helper = require ('sendgrid').mail;
var sg = require ('sendgrid') (
API_KEY
);
var emailobj = {};
emailobj.sendEmail = function (params, callb) {
var fromEmail = new helper.Email (params.from);
var toEmail = new helper.Email (params.to, 'anoop');
var subject = params.subject || 'Test email';
var content = new helper.Content ('text/plain', ' ');
// create mail object from helper
var mail = new helper.Mail (fromEmail, subject, toEmail, content);
// add templateID to mail object
mail.setTemplateId (params.template_id || ' ');
// substitions data in template
if (params.substitution) {
var arr = Object.keys (params.substitution);
console.log ('the subsitution is', arr);
arr.forEach (function (item) {
mail.personalizations[0].addSubstitution (
new helper.Substitution ('%' + item + '%', params.substitution[item])
);
});
}
var emailObj = mail.toJSON ();
console.log ('the email obj is', mail.toJSON ());
var request = sg.emptyRequest ({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON (),
});
sg.API (request, function (error, response) {
if (error) {
console.log ('Error response received');
return callb (error);
}
if (response.statusCode == 202) {
console.log (response);
return callb (null, 'success');
}
});
};
It is probably this line:
mail.setTemplateId (params.template_id || ' ');
If you have the templateID param sent in the request, but it is not sent - it could be causing the failure. Can you switch this statement to an if statement?
if (params.template_id) {
mail.setTemplateId(params.template_id);
}
@mbernier No Success still the email is dropping.
Hi @mbernier its working after removing mail.setTemplateId(params.template_id);
If the template_id is causing you issues then you may need to do a deeper check on params.template_id to make sure that it is not in fact set in the if statement I provided. Something like is_string(params.template_id) && params.template_id.length > 5 or something like that.
@mbernier thanks a lot for your help you saved my job 馃憤
@codesinghanoop No problem at all! Would you be OK to share the specific change that you made that ended up working?
i think there was something to take with the template_Id and i removed it for now. After defining the hardcoded value of template_Id it worked for 2-3 emails and after that every email is dropping. So i commented out the template_Id. Please have a look into it.
Just to make sure: Is the template ID that you are using one that is defined in your SendGrid Transactional templates UI/API?
ok my bad i was using previous account template ID
Thanks for following up @codesinghanoop!