https://discord.gg/botpress logo
#🤝help
New error when trying to send email via SendGrid API
# 🤝help
r

rhythmic-oyster-77498

01/08/2024, 2:39 PM
Used this code to successfully setup a custom node to send an email: https://botpress.com/docs/cloud/getting-started/tips-and-tricks/#send-emails-using-sendgrid-api Now it is resulting in the following error: [TypeError, Cannot read properties of undefined (reading 'length')]. My API credentials are working as expected and nothing has changed to cause this error.
f

fresh-fireman-491

01/08/2024, 2:56 PM
Could you share your code without the API key?
r

rhythmic-oyster-77498

01/08/2024, 3:00 PM
It is the exact same from the tutorial. Even reloading the code from the link above and changing my API key results in the same error.
f

fresh-fireman-491

01/08/2024, 4:09 PM
I also got that
f

freezing-carpenter-9818

01/08/2024, 4:18 PM
this following code should work well. I don't get any errors my end.
const sendEmail = async (to, subject, body) => { const apiKey = 'YOUR API KEY' const url = 'https://api.sendgrid.com/v3/mail/send' const payload = { personalizations: [ { to: [{ email: to }], subject: subject } ], from: { email: 'YOUR SEND FROM EMAIL' }, content: [{ type: 'text/plain', value: body }] } const headers = { Authorization:
Bearer ${apiKey}
, 'Content-Type': 'application/json' } await axios.post(url, payload, { headers }) } const to = 'SEND TO EMAIL HERE' const subject = 'EMAIL SUBJECT HERE' const body =
EMAIL BODY HERE. YOU CAN ADD VARIABLES
await sendEmail(to, subject, body)
email body must be inbetween backticks > these ``
if you want to add variables in the body they must be formated like this: ${workflow.variablename}
c

crooked-van-25152

01/08/2024, 4:34 PM
Thanks @freezing-carpenter-9818 !
r

rhythmic-oyster-77498

01/08/2024, 4:38 PM
@freezing-carpenter-9818 tried the code above, still receiving same error.
my apologies @freezing-carpenter-9818 , the code should work. I realized the error was on my end and due to new requirements for sendgrid api. the sending email did not match my verified senders (typo) Here is the entire error showing when trying curl : The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements" thanks again!
f

freezing-carpenter-9818

01/08/2024, 5:07 PM
yes you must verify a sender and use that email address for the send from email
let me know if you get it working
r

rhythmic-oyster-77498

01/08/2024, 6:18 PM
yes, it is working. it was a typo on my end for the from email
c

crooked-van-25152

01/08/2024, 6:58 PM
Good to know it's working now! Marking this thread as solved.