require('dotenv').config();
const { MailtrapClient } = require('mailtrap');
const nodemailer = require('nodemailer');
/* The official mailtrap package doesn't support sending test emails, so we send a mail first with nodemailer */
/* Initialize nodemailer with SMTP configuration from environment variables. */
const transport = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS
}
});
/* Asynchronously send a test email using nodemailer. */
async function sendTestEmail() {
const info = await transport.sendMail({
from: process.env.EMAIL_FROM,
to: "user@domain.com",
subject: "Mailtrap Email Testing",
html: '<h1>Mailtrap Email Testing</h1>',
text: 'Mailtrap Email Testing'
});
console.log("Message sent: %s", info.messageId);
return info;
}
/* Configure the Mailtrap client and fetch email information for testing and automation. */
const client = new MailtrapClient({
token: process.env.MAILTRAP_TOKEN,
testInboxId: process.env.TEST_INBOX_ID,
accountId: process.env.ACCOUNT_ID
});
const inboxesClient = client.testing.inboxes;
const messagesClient = client.testing.messages;
/* Send the test email and then retrieve it from Mailtrap for analysis. */
sendTestEmail().then(() => {
inboxesClient.getList()
.then(async (inboxes) => {
if (inboxes && inboxes.length > 0) {
const firstInboxId = inboxes[0].id;
console.log(`First inbox ID: ${firstInboxId}`);
const messages = await messagesClient.get(firstInboxId);
if (messages && messages.length > 0) {
const firstMessageId = messages[0].id;
console.log(`First message ID: ${firstMessageId}`);
const analysis = await messagesClient.getHtmlAnalysis(firstInboxId, firstMessageId);
console.log('HTML Analysis:', analysis);
const htmlMessage = await messagesClient.getHtmlMessage(firstInboxId, firstMessageId);
console.log('HTML Message:', htmlMessage);
const textMessage = await messagesClient.getTextMessage(firstInboxId, firstMessageId);
console.log('Text Message:', textMessage);
const headers = await messagesClient.getMailHeaders(firstInboxId, firstMessageId);
console.log('Mail Headers:', headers);
const eml = await messagesClient.getMessageAsEml(firstInboxId, firstMessageId);
console.log('Message as EML:', eml);
const htmlSource = await messagesClient.getMessageHtmlSource(firstInboxId, firstMessageId);
console.log('HTML Source:', htmlSource);
const rawMessage = await messagesClient.getRawMessage(firstInboxId, firstMessageId);
console.log('Raw Message:', rawMessage);
const spamScore = await messagesClient.getSpamScore(firstInboxId, firstMessageId);
console.log('Spam Score:', spamScore);
const emailMessage = await messagesClient.showEmailMessage(firstInboxId, firstMessageId);
console.log('Email Message:', emailMessage);
const updateStatus = await messagesClient.updateMessage(firstInboxId, firstMessageId, {
isRead: false
});
console.log('Update Status:', updateStatus);
// Forward the message (needs to be a confirmed email for forwarding in mailtrap)
// await messagesClient.forward(firstInboxId, firstMessageId, 'mock@mail.com');
// console.log('Message forwarded.');
// Delete the message
const response = await messagesClient.deleteMessage(firstInboxId, firstMessageId);
console.log('Delete Response:', response);
} else {
console.log('No messages found in the first inbox.');
}
} else {
console.log('No inboxes found.');
}
})
.catch(error => {
console.error('Error fetching inboxes or messages:', error);
});
}).catch(console.error);