Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda