Kryptonian
Kryptonian8mo ago

Sending email with SMTP and Deno, with InReplyTo headers failing

It seems that currently it's not possible to send emails using denomailer with InReplyTo headers, as it chockes on the in in the header name. Any other ideas as to how I should do sending email? I'm using the following code currently, or trying to:
import { SMTPClient as DenoMailerSMTPClient } from "https://deno.land/x/denomailer@1.6.0/mod.ts";

type Smtp = {
host: string;
port: number;
};

type EmailObject = {
from: string;
cc?: string;
subject: string;
content: string;
inReplyTo?: string; // Added inReplyTo field
references?: string; // Added references field
};

export async function send_smtp_email(
smtp_res: Smtp,
to_email: string,
from_email: string,
subject: string,
content: string,
cc_email?: string,
inReplyTo?: string,
references?: string,
) {
const client = new DenoMailerSMTPClient({
debug: {
allowUnsecure: true,
},
connection: {
hostname: smtp_res.host,
port: smtp_res.port,
},
});

const headers: { [key: string]: string } = {
from: from_email,
to: to_email,
cc: cc_email || '', // Use the cc_email parameter here
subject: subject,
inReplyTo: inReplyTo || '', // Add In-Reply-To header if provided
references: references || '', // Add References header if provided
};

await client.send({
headers,
content,
});

await client.close();
return `Email sent from ${from_email} to ${to_email}`;
}

export async function main(email: EmailObject, smtpResource: Smtp) {
if (email != null) {
const currentDate = new Date().toLocaleDateString("fi-FI");

const emailBody = "Hei"
`On ${currentDate}, ${email.from} wrote:\n` +
`\"${email.content}\"\n\n`

const sendEmail = await send_smtp_email(
smtpResource,
email.from,
"sender",
`Re: ${email.subject}`,
emailBody,
"cc",
email.inReplyTo,
email.references,
);

return sendEmail;
} else {
return "NOOP";
}
}
import { SMTPClient as DenoMailerSMTPClient } from "https://deno.land/x/denomailer@1.6.0/mod.ts";

type Smtp = {
host: string;
port: number;
};

type EmailObject = {
from: string;
cc?: string;
subject: string;
content: string;
inReplyTo?: string; // Added inReplyTo field
references?: string; // Added references field
};

export async function send_smtp_email(
smtp_res: Smtp,
to_email: string,
from_email: string,
subject: string,
content: string,
cc_email?: string,
inReplyTo?: string,
references?: string,
) {
const client = new DenoMailerSMTPClient({
debug: {
allowUnsecure: true,
},
connection: {
hostname: smtp_res.host,
port: smtp_res.port,
},
});

const headers: { [key: string]: string } = {
from: from_email,
to: to_email,
cc: cc_email || '', // Use the cc_email parameter here
subject: subject,
inReplyTo: inReplyTo || '', // Add In-Reply-To header if provided
references: references || '', // Add References header if provided
};

await client.send({
headers,
content,
});

await client.close();
return `Email sent from ${from_email} to ${to_email}`;
}

export async function main(email: EmailObject, smtpResource: Smtp) {
if (email != null) {
const currentDate = new Date().toLocaleDateString("fi-FI");

const emailBody = "Hei"
`On ${currentDate}, ${email.from} wrote:\n` +
`\"${email.content}\"\n\n`

const sendEmail = await send_smtp_email(
smtpResource,
email.from,
"sender",
`Re: ${email.subject}`,
emailBody,
"cc",
email.inReplyTo,
email.references,
);

return sendEmail;
} else {
return "NOOP";
}
}
3 Replies
lfanew
lfanew8mo ago
I'll be honest, I tried quite a few Deno mail/SMTP clients and had trouble with each one (including the one you're using, it had trouble with whitespaces showing up as encoded =20 and other weird behavior). I just ended up using nodemailer (from NPM) which has been working perfectly fine https://www.npmjs.com/package/nodemailer https://docs.deno.com/runtime/manual/node/npm_specifiers
npm
nodemailer
Easy as cake e-mail sending from your Node.js applications. Latest version: 6.9.13, last published: 3 days ago. Start using nodemailer in your project by running npm i nodemailer. There are 6830 other projects in the npm registry using nodemailer.
npm: specifiers | Deno Docs
Since version 1.28, Deno has native support for importing npm packages. This is
Kryptonian
Kryptonian8mo ago
Funny thing should you mentiion about the wierd =20, I was wondering why that is, but I have just lived with it. :D What other wierd behavior did you find?
lfanew
lfanew8mo ago
I can't recall all of them it's been a while, but after about 2 or 3 oddities we switched our mailer script to use nodemailer. We also tried the original one denomailer was based on, it didn't have as many issues but was less featureful than what we needed