How do I use javascript to send Form data to an email address?

Asked

Viewed 42 times

0

Good morning, I am working on a code that uses a FORM with POST method and I need the data of this FORM when submitted to be forwarded to an email and not to a server. All this using only Javascript, I searched but not found in the other posts, I found solutions only with PHP. If anyone can help me how to perform this action using JS.

<form class="formulario" action="" method="post"> 

1 answer

1


What you want to do is not possible on the client’s side. According to own documentation of the element <form>:

The HTML element <form> represents a section of a document that contains interactive controls that allow the user to submit information to a particular web server.

Thus, taking into account that email servers are not, in fact, "web servers", it is not possible to send data to them. Usually e-mail servers use other protocols, such as SMTP, other than the protocols used by the web.

The most you can do is use the protocol mailto, which usually opens the operating system’s default email client with the already-defined fields:

<form method="get" action="mailto:[email protected]">
  <input name="subject" placeholder="Assunto" />
  <textarea name="body" placeholder="Mensagem"></textarea>
  <button type="submit">Enviar</button>
</form>

Note that this will not "send" the email, just open the default system client. To learn more how this protocol works in the element <form>, see the specification.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.