WD13
WD13
CDCloudflare Developers
Created by WD13 on 9/4/2024 in #workers-help
Email worker error (reconstructing FROM address)
ignore that, it is working now. Errors look like this { "message": [ "Error processing email:", "ReferenceError: EmailMessage is not defined" ], "level": "error", "timestamp": 1725464885937 }, { "message": [ "Stack trace:", "ReferenceError: EmailMessage is not defined\n at Object.email (worker.js:29:24)" ], "level": "error", "timestamp": 1725464885937 }
11 replies
CDCloudflare Developers
Created by WD13 on 9/4/2024 in #workers-help
Email worker error (reconstructing FROM address)
nope, doesnt look like it now!
11 replies
CDCloudflare Developers
Created by WD13 on 9/4/2024 in #workers-help
Email worker error (reconstructing FROM address)
tried a different way... export default { async email(message, env, ctx) { try { // Get the "From" address from the incoming email let originalFrom = message.headers.get('from'); console.log(Original From: ${originalFrom}); // Extract the email address let emailRegex = /"[^"]*" <([^<]+?)at([^>]+?)@[^>]+>/; let match = originalFrom.match(emailRegex); let modifiedFrom; // If match is found, reconstruct the email address if (match) { let username = match[1].trim(); // let domain = match[2].trim(); // // Create the new "From" address, ensuring only username and domain are included modifiedFrom = ${username}@${domain}; console.log(Modified From: ${modifiedFrom}); } else { // No match found, use the original sender's address as fallback modifiedFrom = originalFrom; console.log(No match found. Using original sender address: ${modifiedFrom}); } // Create a new email with the modified "From" address and forward it let newMessage = new EmailMessage({ from: modifiedFrom, to: "verified@email.com", subject: message.headers.get('subject'), text: await message.text(), cc: message.headers.get('cc'), bcc: message.headers.get('bcc'), }); // Send the new email await newMessage.send(); console.log('Email forwarded successfully with modified From address'); } catch (err) { console.error("Error processing email:", err); console.error("Stack trace:", err.stack); } } }; yes the email has been updated to a verified one but I am still recieving nothing. This code doesn't even seem to forward or process the original message. Pretty confused now!
11 replies
CDCloudflare Developers
Created by WD13 on 9/4/2024 in #workers-help
Email worker error (reconstructing FROM address)
shows up as forwarded on the overview page but I do no recieve the email??
11 replies
CDCloudflare Developers
Created by WD13 on 9/4/2024 in #workers-help
Email worker error (reconstructing FROM address)
How would I go about that? Something like this? export default { async email(message, env, ctx) { try { // Get the "From" address from the incoming email let originalFrom = message.headers.get('from'); console.log(Original From: ${originalFrom}); // Extract the email address let emailRegex = /"[^"]*" <([^<]+?)at([^>]+?)@[^>]+>/; let match = originalFrom.match(emailRegex); let modifiedFrom; // If match is found, reconstruct the email address if (match) { let username = match[1].trim(); // e.g., let domain = match[2].trim(); // e.g., .com // Create the new "From" address, ensuring only username and domain are included modifiedFrom = ${username}@${domain}; console.log(Modified From: ${modifiedFrom}); } else { // No match found, use the original sender's address as fallback modifiedFrom = originalFrom; console.log(No match found. Using original sender address: ${modifiedFrom}); } await env.EMAILS.send({ from: modifiedFrom, to: "**@gmail.com", // Send to the desired recipient subject: message.headers.get('subject') || "No Subject", text: await message.text(), // Use the body of the original email }); console.log('Email forwarded successfully with modified From address'); } catch (err) { console.error("Error processing email:", err); console.error("Stack trace:", err.stack); } } };
11 replies
CDCloudflare Developers
Created by WD13 on 9/4/2024 in #workers-help
Email worker error (reconstructing FROM address)
export default {
// Handle HTTP requests
async fetch(request) {
// Respond to HTTP requests with a simple message
return new Response('This worker is configured to handle email events.', {
status: 200,
headers: { 'Content-Type': 'text/plain' }
});
},

// Handle email events
async email(message, env, ctx) {
try {
// Get the "From" address from the incoming email
let originalFrom = message.headers.get('from');
console.log(Original From: ${originalFrom});

// Extract the email address (assuming format: originaluser_at_domain.com_duckuser@duck.com)
let emailRegex = /"[^"]*" <([^<]+?)_at_([^>]+?)@[^>]+>/;
let match = originalFrom.match(emailRegex);

let modifiedFrom;

// If match is found, reconstruct the email address
if (match) {
let username = match[1].trim(); // e.g., originaluser
let domain = match[2].trim(); // e.g., domain.com

// Create the new "From" address, ensuring only username and domain are included
modifiedFrom = ${username}@${domain};
console.log(Modified From: ${modifiedFrom});
} else {
// No match found, use the original sender's address as fallback
modifiedFrom = originalFrom;
console.log(No match found. Using original sender address: ${modifiedFrom});
}

// Forward the email to forwardemail@domain.com
await message.forward("forwardemail@domain.com");

console.log('Email forwarded successfully');

} catch (err) {
console.error("Error processing email:", err);
console.error("Stack trace:", err.stack);
}
}
};
export default {
// Handle HTTP requests
async fetch(request) {
// Respond to HTTP requests with a simple message
return new Response('This worker is configured to handle email events.', {
status: 200,
headers: { 'Content-Type': 'text/plain' }
});
},

// Handle email events
async email(message, env, ctx) {
try {
// Get the "From" address from the incoming email
let originalFrom = message.headers.get('from');
console.log(Original From: ${originalFrom});

// Extract the email address (assuming format: originaluser_at_domain.com_duckuser@duck.com)
let emailRegex = /"[^"]*" <([^<]+?)_at_([^>]+?)@[^>]+>/;
let match = originalFrom.match(emailRegex);

let modifiedFrom;

// If match is found, reconstruct the email address
if (match) {
let username = match[1].trim(); // e.g., originaluser
let domain = match[2].trim(); // e.g., domain.com

// Create the new "From" address, ensuring only username and domain are included
modifiedFrom = ${username}@${domain};
console.log(Modified From: ${modifiedFrom});
} else {
// No match found, use the original sender's address as fallback
modifiedFrom = originalFrom;
console.log(No match found. Using original sender address: ${modifiedFrom});
}

// Forward the email to forwardemail@domain.com
await message.forward("forwardemail@domain.com");

console.log('Email forwarded successfully');

} catch (err) {
console.error("Error processing email:", err);
console.error("Stack trace:", err.stack);
}
}
};
11 replies