DEV Community

Play Button Pause Button
Vuelancer
Vuelancer

Posted on

Sending emails in Deno using SMTP module

Hey Developers!

I have created a tutorial for sending emails from your deno project using third party module called smtp.


Introduction

  • You can send email from your deno application.
  • Where we use this thing?
    • Sending verification email to the user after his registration on our project.
    • Sending emails like monthly newsletter, blogs, etc.

Getting Started

import { SmtpClient } from "https://deno.land/x/smtp/mod.ts";
import "https://deno.land/x/dotenv/load.ts";
Enter fullscreen mode Exit fullscreen mode
  • Creating an instance of SmtpClient
const client = new SmtpClient();
Enter fullscreen mode Exit fullscreen mode
  • Accessing environmental variables
const { SEND_EMAIL, PWD, RECV_EMAIL } = Deno.env.toObject();
Enter fullscreen mode Exit fullscreen mode
  • Configuring & Creating the connection
const connectConfig: any = {
  hostname: "smtp.gmail.com",
  port: 465,
  username: SEND_EMAIL,
  password: PWD,
};
await client.connectTLS(connectConfig);
Enter fullscreen mode Exit fullscreen mode
  • Sending the e-mail to the rec. account
await client.send({
  from: SEND_EMAIL,
  to: RECV_EMAIL,
  subject: "Welcome!",
  content: "Hi from Vuelancer!",
});
Enter fullscreen mode Exit fullscreen mode
  • Finally, closing the smtp client connection
await client.close();
Enter fullscreen mode Exit fullscreen mode

Run the app

$ deno run --allow-read --allow-env --allow-net server.ts
Enter fullscreen mode Exit fullscreen mode

Or else clone this repo and sculpt it!

~ Thank You!

Support Me

Top comments (0)