0

I have the following contact form:

<div class="container red_layer_footer">
<form action="/var/www/cgi-bin/FormMail.pl" method="POST">
<input type=hidden name="recipient" value="mymail@gmail.com">
<input type=hidden name="subject" value="Nuova mail">
<input type=hidden name="redirect" value="http://www.evogale.it/grazie.html">

<h2>CONTATTI</h2>

<div class="name">
<label for="name"></label>
<input type="text" placeholder="Nome" name="name" id="name_input" required>
</div>

<div class="email">
<label for="email"></label>
<input type="email" placeholder="Mail" name="email" id="email_input" required>
</div>

<div class="message">
<label for="message"></label>
<textarea name="message" placeholder="Messaggio" id="message_input" cols="30" rows="5" required></textarea>
</div>

<div class="submit">
<input type="submit" value="Invia Messaggio" id="form_button">
</div>

<input type=hidden name="required" value="email,name,message">
</form> <!-- // End form -->
</div> <!-- End #container --> 

In an html file, and I want it to send email via an SMTP. How should i modify the code to make that happen? Looking around on the web I've seen that I might add some jQuery code?

Brandolo
  • 1
  • 1
  • 1
  • You can't send an email from a browser. See https://stackoverflow.com/questions/7381150/how-to-send-an-email-from-javascript – Teemu Jan 11 '21 at 11:29
  • I can see that your backend is Perl CGI script, you might just want to use a Perl module like [`Email::Sender::Simple`](https://metacpan.org/pod/Email::Sender::Simple) to send the emails. Some stock-standard approaches are in [this post](https://stackoverflow.com/questions/10008277/sending-mail-via-smtp-in-perl). – costaparas Jan 11 '21 at 11:31

1 Answers1

1

To be able to send emails, you need to provide the correct SMTP server when you set up your email client. Most of the internet systems use SMTP as a method to transfer mail from one user to another. It is a push protocol. In order to use SMTP you need to configure your Gmail. You need to change two settings of your gmail account from which you are sending the mail i.e.

1. Revoke 2-step verification

2. Enabling less secure apps to access Gmail. You can easily do this by clicking on the link Enable

After this just create a html file and include SMTP in your tag :

<script src="https://smtpjs.com/v3/smtp.js"></script>

Below is the html code which you will need to run in order to send the mail.

    <script src= 
        "https://smtpjs.com/v3/smtp.js"> 
      </script> 
      
      <script type="text/javascript"> 
        function sendEmail() { 
          Email.send({ 
            Host: "smtp.gmail.com", 
            Username: "sender@email_address.com", 
            Password: "Enter your password", 
            To: 'receiver@email_address.com', 
            From: "sender@email_address.com", 
            Subject: "Sending Email using javascript", 
            Body: "Well that was easy!!", 
          }) 
            .then(function (message) { 
              alert("mail sent successfully") 
            }); 
        } 
      </script> 
<body> 
  <form method="post"> 
    <input type="button" value="Send Email" 
        onclick="sendEmail()" /> 
  </form> 
</body> 

NOTE : Best and Secure way to use above method with SecureToken as above method is for understanding purpose, above approach is highly insecure as it exposes credentials to the users , for how to enable Security you can check this https://smtpjs.com/

Dhrumil shah
  • 611
  • 4
  • 23
  • 1
    This approach is highly insecure as it exposes credentials to the client. – costaparas Jan 11 '21 at 11:42
  • @costaparas there is another way with `SecureToken ` , you can also check https://smtpjs.com/ – Dhrumil shah Jan 11 '21 at 11:44
  • 'We've thought of that, so instead you can encrypt your SMTP credentials, and lock it to a single domain, and pass a secure token instead of the credentials instead' – Dhrumil shah Jan 11 '21 at 11:45
  • I have my doubts with routing this through their API endpoint `https://smtpjs.com/v3/smtpjs.aspx`, they're not a well-known or reputable service as far as I'm aware. – costaparas Jan 11 '21 at 12:02
  • The smtp is not going to be google, tho. Is the code still valid? If so, how can I place the content of the fields in the email? Thanks – Brandolo Jan 11 '21 at 13:08
  • Yes, if your `From Email id` is from Gmail then you have to follow above 2 steps , else you can also use this code with different email id , and you can set value or email content in `body` ,-> `Body: "Well that was easy!!`", – Dhrumil shah Jan 11 '21 at 13:12
  • For Example :- `var name=document.getElementById("name").value` Get value using this and set in `Body`; – Dhrumil shah Jan 11 '21 at 13:15
  • and you have to find `Host` if you are using another host , like `yahoo` have `smtp.mail.yahoo.com` – Dhrumil shah Jan 11 '21 at 13:29
  • @Brandolo is it useful to you ? – Dhrumil shah Jan 12 '21 at 04:59
  • Hi @Dhrumilshah, I've tried it by just copy-pasting your code on the page, to see if the email would work, but it won't.. I'm currently checking for errors – Brandolo Jan 12 '21 at 14:05