First you need to set the SMTP_HOST, SMTP_PORT, SMTP_USER and SMTP_PASSWORD
environment variables.
To send emails, we will use the sendMail function of the Mailer class, which is located in the vendor/mail directory.
Example:
await Mailer.sendMail({
from: "your_email@email.com",
to: "user_email@email.com",
subject: "E-mail Subject",
text: "E-mail message",
html: "HTML code in string format",
});
We recommend using the renderHtml function that you can import from @hefestos/core
to turn your HTML email template into a string. We also recommend that you always send the email message in text in addition to HTML.
In renderHtml you will pass the file path with extension, for example: renderHtml("mails/contact.nj");
We recommend keeping email templates in the mails directory.
Complete Example:
import { ApiResponse, renderHtml } from "@hefestos/core";
const routes = Router();
routes.post("/mail", async (request, response) => {
try {
const htmlString = renderHtml("mails/contact.nj");
await Mailer.sendMail({
from: "your_email@email.com",
to: "user_email@email.com",
subject: "E-mail Subject",
text: "E-mail message",
html: htmlString,
});
return ApiResponse.success(response);
} catch (error: any) {
return ApiResponse.error(response, error);
}
});
export default routes;
We use the nodemailer library, for more information visit its official documentation.