Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
Function call via the button

I wrote the code to send the email to node.js and they are in different files:

mail.js

require('dotenv').config()
const nodemailer = require('nodemailer')

let transporter = nodemailer.createTransport({
  host: 'smtp.mail.ru',
  port: 465,
  secure: true,
  auth: {
    user: process.env.EMAIL,
    pass: process.env.PASSWORD,
  },
})

const mailOptions = {
    from: 'Mailer Test <>',
    to: '<>',
    subject: ''
}

transporter.sendMail(mailOptions)

and button:

script.js

let submitButton = document.querySelector('.submit_button');
submitButton.addEventListener('click', function(e) {
    
})

how do I call execution in the buttons mail.js?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

mail.js is executed in a server environment, where the client (the browser) cannot access it. Your script.js is probably running on an HTML page. The frontend (browser) cannot directly access the backend.

I recommend setting up an Express server with a route to send the mail.

Download the express (and body-parser) package:

npm install express body-parser

# or

yarn add express body-parser

Make a file called index.js, and set up a basic express server:

const bodyParser = require('body-parser');
const express = require('express');
const app = express();

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

app.listen(3000, () => {
    console.log('App is listening on port 3000!');
});

Body parser allows you to parse data from the body, or from a URL, like function parameters.

Next, we tell the app to listen for connections on port 3000. The app will wait until a connection is received and then decide what to do next.

Now, we can add a route. A route is an endpoint, or a location in a URL For example: https://google.com/search would be the /search route https://stackoverflow.com/questions/1234 would be the /questions/1234 route

We'll add the /mail route, and take in two parameters: to, and subject. Your client will specify this when the request is sent.

Then, we'll execute the mail function that will be your mail.js code.

const bodyParser = require('body-parser');
const express = require('express');
const app = express();

require('dotenv').config()
const nodemailer = require('nodemailer')

function mail(to, subject) {
    const transporter = nodemailer.createTransport({
      host: 'smtp.mail.ru',
      port: 465,
      secure: true,
      auth: {
        user: process.env.EMAIL,
        pass: process.env.PASSWORD,
      },
    });

    const mailOptions = {
        from: 'Mailer Test <>',
        to: to,
        subject: subject
    }

    transporter.sendMail(mailOptions)
}

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

app.get('/mail', (req, res) => { // The req variable is the request, or the data, and the res variable is the response that you send back.
    const query = req.query; // The URL encoded data that your HTML page will send
    const to = query.to; // you will specify these in the HTML page
    const subject = query.subject;

    mail(to, subject);
});

app.listen(3000, () => {
    console.log('App is listening on port 3000!');
});

Use node index.js to start your server.

Now, in your script.js, you can send an ajax request to the URL: localhost:3000/mail?to=test@gmail.com&subject=Subject!

Happy coding!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!