@hbertoson/astro-mailer

1.0.1 • Public • Published

Astro Mailer

Astro Mailer is an Astro integration that enables you to send email notifications via any SMTP service using Nodemailer. This is perfect for handling form submissions, alerts, or transactional messages in your Astro project.

Features

  • Send email notifications from your Astro site using SMTP.
  • Capture form data and pass it into customizable email templates.
  • Supports custom headers such as X-Entity-Ref-ID and List-Unsubscribe.
  • Compatible with Astro’s server-side routing and form handling.
  • Flexible SMTP configuration for any provider (e.g., Gmail, SendGrid, Mailgun, etc).

Prerequisites

You’ll need access to any SMTP server. For example:

  • Gmail
  • SendGrid
  • Amazon SES
  • Mailgun
  • SMTP2GO
  • Postmark
  • Mailjet
  • Zoho Mail
  • Office 365
  • Any other SMTP service

Installation

Install via the Astro CLI:

pnpm astro add @hbertoson/astro-mailer
npm astro add @hbertoson/astro-mailer
yarn astro add @hbertoson/astro-mailer

Or manually:

  1. Install dependencies:
pnpm add @hbertoson/astro-mailer
  1. Add it to your Astro config:
import mailer from '@hbertoson/astro-mailer';

export default defineConfig({
  integrations: [
    mailer({
      smtp: {
        host: 'smtp.example.com',
      },
      templates: {
        welcome: './src/components/emails/WelcomeEmail.astro',
      },
    }),
  ],
});

.env Setup

SMTP_USER=your@email.com
SMTP_PASS=yourpassword
FROM_EMAIL=no-reply@yourdomain.com

Configuration Options

Option Type Required Description
fromEmail string Sender email address
smtp object SMTP configuration for Nodemailer
templates object Named Astro components for emails
preventThreading boolean Prevent Gmail threading
unsubscribeUrl string Add List-Unsubscribe header
verbose boolean Enable debug logging

Usage Example

<body>
  <h1>Astro Mailer</h1>
  <button>Send Email</button>
		<script>
			const button = document.querySelector('button') as HTMLButtonElement;
			button.addEventListener('click', async () => {
				const emailData: BaseEmailRequest = {
					to: 'dev@hunterbertoson.tech',
					subject: 'Test Email',
					templateName: 'email',
					props: {
						name: 'Hunter',
						content: 'This is a test email. From my Astro Mailer Integration',
					},
				};
				const res = await fetch('api/email', {
					method: 'POST',
					headers: {
						'Content-Type': 'application/json',
					},
					body: JSON.stringify(emailData),
				});
				if (res.ok) {
					const data = await res.json();
					console.log('Email sent successfully:', data);
				} else {
					console.error('Error sending email:', res.statusText);
				}
			});
		</script>
</body>

Roadmap

  • [ ] Add CC and BCC support
  • [ ] Async form binding example
  • [ ] File attachments
  • [ ] Email queues with retry
  • [ ] SMTP transport pool option

Contributing

This repo is structured as a monorepo:

  • playground: example Astro project to test the integration
  • package: source code of the integration

Start developing:

pnpm i --frozen-lockfile
pnpm dev

Acknowledgements

MIT License

Package Sidebar

Install

npm i @hbertoson/astro-mailer

Weekly Downloads

5

Version

1.0.1

License

MIT

Unpacked Size

6.12 kB

Total Files

3

Last publish

Collaborators

  • hbertoson