Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda