Sending email via workers with mailjet API

The mailjet api provides curl command:
curl -s \
-X POST \
--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
https://api.mailjet.com/v3.1/send \
-H 'Content-Type: application/json' \
-d '{
"SandboxMode":"true",
"Messages":[
{
"From":[
{
"Email":"[email protected]",
"Name":"Your Mailjet Pilot"
}
],
"HTMLPart":"<h3>Dear passenger, welcome to Mailjet!</h3><br />May the delivery force be with you!",
"Subject":"Your email flight plan!",
"TextPart":"Dear passenger, welcome to Mailjet! May the delivery force be with you!",
"To":[
{
"Email":"[email protected]",
"Name":"Passenger 1"
}
]
}
]
}'
curl -s \
-X POST \
--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
https://api.mailjet.com/v3.1/send \
-H 'Content-Type: application/json' \
-d '{
"SandboxMode":"true",
"Messages":[
{
"From":[
{
"Email":"[email protected]",
"Name":"Your Mailjet Pilot"
}
],
"HTMLPart":"<h3>Dear passenger, welcome to Mailjet!</h3><br />May the delivery force be with you!",
"Subject":"Your email flight plan!",
"TextPart":"Dear passenger, welcome to Mailjet! May the delivery force be with you!",
"To":[
{
"Email":"[email protected]",
"Name":"Passenger 1"
}
]
}
]
}'
This is what I'm trying to do in my worker:
async function sendEmailOrLog(env: Bindings, username: string, recipient: string, sender_pre_at: string, subject: string, content: string) {
// Prepare email
const response = await fetch('https://api.mailjet.com/v3.1/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa(`${env.MAILJET_API_KEY}:${env.MAIILJET_SECRET_KEY}`),
},
body: JSON.stringify({
SandboxMode: 'false',
Messages: [
{
From: [
{
Email: `${sender_pre_at.toLowerCase()}@instructor-ai.com`,
Name: `Instructor-AI`,
},
],
HTMLPart: `<h3>${content}</h3>`,
Subject: subject,
TextPart: content,
To: [
{
Email: recipient,
Name: username,
async function sendEmailOrLog(env: Bindings, username: string, recipient: string, sender_pre_at: string, subject: string, content: string) {
// Prepare email
const response = await fetch('https://api.mailjet.com/v3.1/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa(`${env.MAILJET_API_KEY}:${env.MAIILJET_SECRET_KEY}`),
},
body: JSON.stringify({
SandboxMode: 'false',
Messages: [
{
From: [
{
Email: `${sender_pre_at.toLowerCase()}@instructor-ai.com`,
Name: `Instructor-AI`,
},
],
HTMLPart: `<h3>${content}</h3>`,
Subject: subject,
TextPart: content,
To: [
{
Email: recipient,
Name: username,
Am I doing something wrong? I get 400 error
2 Replies
Chaika
Chaika4mo ago
not enough context and you cut off your code. 400 Error from the worker? from the mailjet api? What's the exact response code and response body from mailjet, is what I'd troubleshoot first. One big thing I notice is curl is in sandbox mode and workers isn't
TechParadox
TechParadoxOP4mo ago
It just said 400 bad request, as if it isn't sending json. I gave up and ended up using zohoflow which was much easier, but I also had a problem sending json so ended up sending form data. I cut off the code because of the character limit. I don't have it anymore, thanks for the reply though. 400 error was from the mailjet API, all it said was bad request. Mailjet support told me email sent but obviously it did not If I try again in future I will update here but I'm just trying to get this project completed in the next two weeks and don't have much spare time for working on my computer right now
Want results from more Discord servers?
Add your server