0
<script src="https://smtpjs.com/v3/smtp.js">
</script>
<script>
var n=0;
</script>
<button type="button" onclick="Email.send({
    Host : 'smtp.gmail.com',
    Username : '[user@gmail.com]',
    Password : '[app specifc password]',
    To : '[reicpent@gmail.com]',
    From : '[user@gmail.com]',
    Subject : 'Subject' ,
    Body : n
}){
.then(
  message => alert(message)
);
}">
test
</button>

Pretty much what the question says. I have tested this and if I put 'Body' in after "Body:" then it will send the email. What I want it to do is send the value of the variable. I am fairly new to JavaScript so simple explanations would be best.

I am using this code from this webpage :https://www.smtpjs.com/

draymon
  • 15
  • 5
  • In addition to the above answer If you are new in javascript and web development you should know that sending emails should be done in the back end but some times it could be done from the front side. I wish if you read this. [previous question](https://stackoverflow.com/questions/7381150/how-to-send-an-email-from-javascript#:~:targetText=In%20general%2C%20sending%20an%20email,using%20javascript%20as%20mentioned%20above.) – Ali Abbas Dec 13 '19 at 08:22

1 Answers1

2

You have to move that Javascript code for the onclick() function into a separate function inside the <script> tag so that you can get the value of n while sending the mail. Something like this:

var n = 0;

function sendMail() {
  Email.send({
    Host: 'smtp.gmail.com',
    Username: 'user@gmail.com',
    Password: '[app specifc password]',
    To: 'reicpent@gmail.com',
    From: 'user@gmail.com',
    Subject: 'Subject',
    Body: n
  }).then((message) => alert(message));
}
<script src="https://smtpjs.com/v3/smtp.js">
</script>
<script>
</script>
<button type="button" onclick="sendMail()">
test
</button>

You need to solve the message which you are getting from the above code because that is something you have to fix according to your requirement.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • This works. Thank you. Now I can continue building my website – draymon Dec 13 '19 at 07:47
  • That isn't working for me. It either sends my var as is (when set in quotes) or I get an error that I need a body text. What am I missing here? – skarpeta Aug 24 '21 at 11:31