cloudflare-smtp
v0.1.2
Published
Tiny SMTP client for Cloudflare Workers with TLS and STARTTLS support.
Maintainers
Readme
cloudflare-smtp
Tiny SMTP client for Cloudflare Workers. It uses Cloudflare's cloudflare:sockets API and supports plain SMTP, implicit TLS, and STARTTLS.
Install
bun add cloudflare-smtpnpm install cloudflare-smtpYou can also install directly from GitHub:
npm install github:Bruol/cloudflare-smtpQuick Start
import { sendMail } from 'cloudflare-smtp';
export default {
async fetch() {
await sendMail(
{
host: 'smtp.example.com',
port: 465,
username: 'smtp-user',
password: 'smtp-password',
from: 'Website <[email protected]>',
to: '[email protected]'
},
{
subject: 'Hello from a Worker',
text: 'This email was sent through SMTP from Cloudflare Workers.'
}
);
return new Response('sent');
}
};Transport Modes
By default, cloudflare-smtp chooses the transport from the port:
- Port
465: implicit TLS - Port
587: STARTTLS - Any other port: plain SMTP
You can also set it explicitly:
await sendMail(
{
host: 'smtp.example.com',
port: 587,
secureTransport: 'starttls',
username: env.SMTP_USER,
password: env.SMTP_PASSWORD,
from: '[email protected]',
to: ['[email protected]', '[email protected]']
},
{
subject: 'STARTTLS example',
text: 'STARTTLS was negotiated before authentication.'
}
);await sendMail(
{
host: 'smtp.example.com',
port: 465,
secureTransport: 'tls',
username: env.SMTP_USER,
password: env.SMTP_PASSWORD,
from: '[email protected]',
to: '[email protected]'
},
{
subject: 'TLS example',
text: 'The socket connected with TLS from the start.'
}
);Available secureTransport values:
auto: choose from the port numbertls: implicit TLS, usually port465starttls: connect in STARTTLS mode and upgrade before authentication, usually port587off: plain SMTP
API
sendMail(config, message)
Sends one plain-text email.
config:
host: SMTP hostnameport: SMTP port, defaults to465fortlsand587otherwiseusername: SMTP usernamepassword: SMTP passwordfrom: sender addressto: one recipient or an array of recipientssecureTransport: optional transport modeheloName: optional EHLO name, defaults tocloudflare-smtp.localmessageIdDomain: optional domain for the generatedMessage-ID, defaults tohost
message:
subject: email subjectreplyTo: optional reply-to addresstext: plain text body
Notes
This package is built for Cloudflare Workers and requires the cloudflare:sockets runtime module. It is not a Node.js SMTP client.
The current message builder sends plain-text email with UTF-8 headers and body content.
Publishing
This repository publishes to npm from GitHub Actions with npm trusted publishing.
In npm package settings, add this trusted publisher:
- Publisher: GitHub Actions
- Organization or user:
Bruol - Repository:
cloudflare-smtp - Workflow filename:
publish.yml
Then publish a new version by pushing a matching tag:
npm version patch
git push --follow-tags