Undefined 'sendRequest' error in Email Sending Functionality using Workers - Potential Issue with Of

I have been working on replicating the email sending functionality using workers, as detailed in the official blog. While transcribing the provided code snippet and running it in the worker, I've encountered an error stating that 'sendRequest' is undefined. Attached is the code snippet from the official blog (https://blog.cloudflare.com/sending-email-from-workers-with-mailchannels/https://blog.cloudflare.com/sending-email-from-workers-with-mailchannels/). Upon checking the code, I noticed that the 'sendRequest' function was not preceded by the 'const' keyword. I am unsure whether this omission could be the root cause of the issue, or whether there may be another underlying problem that I might be overlooking. Could you please clarify whether the code example given in the blog is complete, or if there could possibly be an error within the example? If the example is correct, is there something I might be doing incorrectly that's causing the error?
5 Replies
James
James11mo ago
Hey. Yeah the code in the blog post isn't complete. Something like this is probably more like what you want:
export default {
async fetch(request) {
const send_request = new Request('https://api.mailchannels.net/tx/v1/send', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
personalizations: [
{
to: [{ email: 'test@example.com', name: 'Test Recipient' }],
},
],
from: {
email: 'sender@example.com',
name: 'Workers - MailChannels integration',
},
subject: 'Look! No servers',
content: [
{
type: 'text/plain',
value: 'And no email service accounts and all for free too!',
},
],
}),
});
const response = await fetch(send_request);
return response;
},
}
export default {
async fetch(request) {
const send_request = new Request('https://api.mailchannels.net/tx/v1/send', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
personalizations: [
{
to: [{ email: 'test@example.com', name: 'Test Recipient' }],
},
],
from: {
email: 'sender@example.com',
name: 'Workers - MailChannels integration',
},
subject: 'Look! No servers',
content: [
{
type: 'text/plain',
value: 'And no email service accounts and all for free too!',
},
],
}),
});
const response = await fetch(send_request);
return response;
},
}
bkyerv
bkyerv11mo ago
what a lightening quick response. thank you I will check it and get back
bkyerv
bkyerv11mo ago
I have used the example above along with setting the txt record for the domain as per https://community.cloudflare.com/t/introducing-mailchannels-domain-lockdown/523913 however I am not receiving the email I sent using this code snippet. Could you please explain what could be the reason for not receiving the email? I am using gmail address as a recepient
Cloudflare Community
Introducing MailChannels Domain Lockdown™
TL;DR To lock your domain name down to a specific Workers account, create a TXT record on a subdomain called _mailchannels and set the value to v=mc1 cfid=yourworker.workers.dev. After this record is published, MailChannels will reject all email messages sent from your domain that do not originate from your Cloudflare Workers account. Bolsteri...
bkyerv
bkyerv11mo ago
here is repo
bkyerv
bkyerv11mo ago
GitHub
GitHub - bkyerv/email-sender
Contribute to bkyerv/email-sender development by creating an account on GitHub.