Email Services For Sending Emails

I want to be able to send emails to people with JavaScript, for example:

send('john@gmail.com', 'Your passcode is: ' + passcode)

You mean Node.js? I can help you with that:

const { createTransport } = require("nodemailer");
const directTransport = require("nodemailer-direct-transport");

const { REPL_SLUG, REPL_OWNER } = process.env;

const host = `${REPL_SLUG.toLowerCase()}.${REPL_OWNER.toLowerCase()}.repl.co`;
const from = `mail@${host}`;
const transport = createTransport(directTransport({ name: host }));

function send(to, subject, html) {
  transport.sendMail({ from, to, subject, html }, (err, data) => {
    if (err) throw err;
    else {
      console.log(data);
    }
  })
}

Hopefully this helps!

Sorry, no, JavaScript, You know like, Its Node.js but not Node, Idk if that will work.

Also, Im pretty Shure you need a email service.

Ok, well you have to have a backend to send emails.

Here is an example of using an ExpressJS and Nodemailer to send emails:

index.js (backend)

const express = require("express");
const { createTransport } = require("nodemailer");
const directTransport = require("nodemailer-direct-transport");
const { join } = require("path");
const { urlencoded, json } = express;
const cors = require("cors");
const { REPL_SLUG, REPL_OWNER } = process.env;
const host = `${REPL_SLUG.toLowerCase()}.${REPL_OWNER.toLowerCase()}.repl.co`;
const from = `mail@${host}`;
const app = express();
const transport = createTransport(directTransport({ name: host }));

app.use(cors());
app.use(urlencoded({ extended: true }));
app.use(json());

app.get("/", (req, res) => {
  res.sendFile(join(__dirname, "index.html"));
});

app.post("/mail", (req, res) => {
  const { to, subject, html } = req.body;
  transport.sendMail({ from, to, subject, html }, (err, data) => {
    if (err) throw err;
    res.send({ err, data });
  }
}

index.html (frontend)

<!DOCTYPE html>
<html>
  <head>...</head>
  <body>
     ...
     <script>
       async function send(to, subject, html) {
         let res = await fetch("/mail", {
           method: "POST",
           headers: { "Content-Type": "application/json" },
           body: JSON.stringify({ to, subject, html })
         });
         let json = await res.json();
         return json;
       }
       // Send an email
       send("john@gmail.com", "Passcode", "Your passcode is ...").then((data) => {
         if(data.err) throw data.err;
         else {
           console.log(data.data);
         }
       });
    </script>
  </body>
</html>
1 Like

No you don’t, not for this. (nodemailer if that’s wht you mean)

Im trying it, whats the backend tho?

Create a Nodejs Repl and put in the files index.html.

how do you see the webpage? I put the EXACT same code, it also gave this error:

/home/runner/email/index.js:26
  }
  ^

SyntaxError: missing ) after argument list

Ok,yes, put a ) at the end of the code.

I tried on a new line and on the same last line and it showed a different error both times:

/home/runner/email/index.js:26
  }
  ^

SyntaxError: missing ) after argument list

nvm, Ill just use the first Node.js thing you showed me, I added:

send("myemail", "PassCode", "Your passcode is: ...")

And it installed nodemailer, and then it said:

      ...
      responseCode: 550,
      command: 'DATA',
      domain: 'gmail.com',
      exchange: 'gmail-smtp-in.l.google.com',
      recipients: [ 'myemail' ]
    }
  ]
}

Node.js v18.12.1
repl process died unexpectedly: exit status 1