Skip to content

Sending Emails with SMTP2GO

LaunchFa.st makes it easier for you to send emails with SMTP2GO with an API and SDK baked-in, ready to use.

Make sure to update the .env file or hosting provider’s environment variable to have the necessary variables for SMTP2GO:

Terminal window
# Obtain the SMTP2GO Username and Password
# https://support.smtp2go.com/hc/en-gb/articles/13645646122777-SMTP-Users
SMTP2GO_USERNAME="..."
SMTP2GO_PASSWORD="..."

Following are the two methods with which you can send emails:

1. POSTing to the baked-in API

To send emails over an API, setup a PRIVATE_ACCESS_KEY environment variable, so that all the API calls do need to have that particular token as the x-access-key header, match to what’s stored in the server. It is to ensure that the person sending the email is an admin / allowed to.

A. POSTing to the emails API from a server-side API

Here’s how you’d make the request for each framework from the server-side:

src/routes/api/random.ts
import type { APIContext } from 'astro'
export async function GET({ request }: APIContext) {
// From a server-side API that is being fetched
await fetch(new URL('/api/email/smtp2go/trigger', new URL(event.request.url).origin).toString(), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-access-key': env.PRIVATE_ACCESS_KEY,
},
body: JSON.stringify({
to: email,
subject: 'How?',
text: 'I love SMTP2GO.',
}),
})
}
export async function POST({ request }: APIContext) {
// From a server-side API that is being POST-ed to
await fetch(new URL('/api/email/smtp2go/trigger', new URL(event.request.url).origin).toString(), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-access-key': env.PRIVATE_ACCESS_KEY,
},
body: JSON.stringify({
to: email,
subject: 'How?',
text: 'I love SMTP2GO.',
}),
})
}
B. POSTing to the emails API from client-side

Here’s how you’d make the request for each framework from the client-side:

src/routes/page.astro
<script>
fetch('/api/email/smtp2go/trigger', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-access-key': 'Insert that private access token safely?',
},
body: JSON.stringify({
to: email,
subject: 'How?',
text: 'I love SMTP2GO.',
}),
})
</script>

2. Use Nodemailer SDK inside your server-side code

Here’s how you’d make the request for each framework from the server-side with SDK:

src/routes/api/random.ts
import nodemailer from 'nodemailer'
import type { APIContext } from 'astro'
export async function POST({ request }: APIContext) {
// From a server-side API that is being POST-ed to
// Send an email using nodemailer
// https://www.smtp2go.com/setupguide/node-js-script/
const smtpTransport = nodemailer.createTransport({
host: 'mail.smtp2go.com',
auth: {
user: import.meta.env.SMTP2GO_USERNAME,
pass: import.meta.env.SMTP2GO_PASSWORD,
},
port: 2525, // 8025, 587 and 25 can also be used.
})
await smtpTransport.sendMail({
text: context.text,
subject: context.subject,
from: context['verified_sender'] ?? 'jain71000@gmail.com',
to: typeof context.to === 'string' ? [context.to] : context.to,
})
}