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

122
Visualizações
Sending email in Javascript

According to Sending emails with Javascript, one way to do it is the following:

function sendMail() {
    var link = "mailto:me@example.com"
             + "?cc=myCCaddress@example.com"
             + "&subject=" + encodeURIComponent("This is my subject")
             + "&body=" + encodeURIComponent(document.getElementById('myText').value)
    ;
    window.location.href = link;
}

However I'm trying to personalize it, sending emails with variables. I did this:

function sendMail(subject="test", body, mail="test@gmail.com") {
  var link = `mailto:${mail}`
           + "&subject=" + encodeURIComponent(`${subject}`)
           + "&body=" + encodeURIComponent(`${body}`)
  ;
  window.location.href = link;

But, when sending the email, I achieve this fail:

fail (image)

It seems like it is not recognizing each variable. How to solve it?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The link have to be like that :

var link = `mailto:${mail}`
           + "?subject=" + encodeURIComponent(`${subject}`)
           + "&body=" + encodeURIComponent(`${body}`);
about 4 years ago · Juan Pablo Isaza Relatório

0

The query string should be start with ? not &

change &subject to ?subject

it should be //abc@example.com?subject=blahblahblah&body=testtest

about 4 years ago · Juan Pablo Isaza Relatório

0

Just try this code for sending email. I am sure it will work..

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://smtpjs.com/v3/smtp.js"></script>  
    <script type="text/javascript">
        function sendEmail() {
            Email.send({
                Host: "smtp.gmail.com",
                Username : "<sender’s email address>",
                Password : "<email password>",
                To : '<recipient’s email address>',
                From : "<sender’s email address>",
                Subject : "<email subject>",
                Body : "<email body>",
            })
            .then(function(message){
                alert("mail sent successfully")
            });
        }
    </script>
</head>
<body>  
    <form method="post">
        <input type="button" value="Send Email" onclick="sendEmail()"/>
    </form>  
</body>

And just remember if you are using JavaScript in any other file then just link it will your HTML file! And this is the not fully coded by me I have done some research from internet.

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