AWS Lambda
Overview
Prerequisites
Send emails using AWS Lambda and Mailtrap
/* global fetch */
const MAILTRAP_API_KEY = 'YOUR-MAILTRAP-API-KEY-HERE';
export const handler = async (event) => {
try {
const res = await fetch('https://send.api.mailtrap.io/api/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MAILTRAP_API_KEY}`
},
body: JSON.stringify({
from: { name: 'Mailtrap Test', email: 'YOUR-EMAIL-HERE' },
to: [{ email: 'RECIPIENT-EMAIL-HERE' }],
subject: 'Hello World',
html: '<strong>it works!</strong>',
})
});
if (res.ok) {
const data = await res.json();
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
};
}
return {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ error: `HTTP ${res.status}` }),
};
} catch (error) {
return {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
error: error instanceof Error ? error.message : 'Unknown error'
}),
};
}
};Configuration
Learn more
Last updated
Was this helpful?

