How to do a Ubmit sending filled data in the form to an email?

Asked

Viewed 19,533 times

3

<div id="mainDiv">
    <div id="marca">
        <img class="imagens" src="media/img/titulo.png" alt="Titulo">
    </div>

    <div id="anaMaria">
        <img class="imagens" src="media/img/fundoCadastroDois.png" alt="FundoCadastroDois">
    </div>

    <div id="cadastro">
        <p>....</p>
        <form id="contatoForm" method="post" action="mailto:[email protected]">
            <input class="cells" type="text" name="nome" placeholder="nome">
            <input class="cells" type="text" email="email" placeholder="email">
            <button type="submit" value="click" id="enviar"></button>
        </form>
    </div>
</div>

I implemented this code above, but once I do form Ubmit, it opens my own email and with the data filled as if I were sending a new email. What I want is for the user to fill with name and email in the fields and this data goes to my inbox. I looked for solutions but most use the same that I am doing or with href, which does not solve the problem. I know it’s simple but I need something simple to finish.

  • To send email you need an SMTP server. What language are you using?

  • 1
  • I think there is an error of interpretation in Guilherme’s question, it seems to me (by the code and content of the text), who wants to fill the email and not send it itself, so his form uses mails. Correct me if I’m wrong.

  • 1

    It seems to me that he wants to send, but let’s wait for the author clarify (note: as I interpreted different, I rejected his editing suggestion, @Gabrielgartz).

  • @bfavaretto If you’re right I help you by clicking on the close and I save my answer to someone who has this question and it is useful.

  • One thing is fact, regardless of which language you are using, is missing the name in input email...

  • 1

    Guys, I really wasn’t clear on my question, what I want is for the user to fill in name and email in the fields and this data goes into my inbox. But from the answers some of you have given me, there’s no way to do this without a server.

  • Thank you for clarifying, William. So it seems to me a duplicate.

  • @Gabrielgartz She’s a duplicate, but I wouldn’t exclude her answer.

  • @bfavaretto I did what I promised as soon as I read the boy’s comment. You were right, the misinterpretation was mine.

  • I have doubts whether it really is a duplicate. It may be, it seems, but they both have interesting answers. Can they be merged? It is a good parameter to know if it is duplicate. Even if the merge is not done.

  • @bigown I believe they can be merged, considering the current answers. The only argument against considering duplicate is that the other question demands that nothing server-side is used, and that one is not (and using something server-site may be the answer the author is looking for here).

  • What language do you use? Java, ASP, PHP? Any framework with them like Struts or JSF (if Java is my line of knowledge)?

  • @Philippegioseffi will use Java with Struts.

Show 9 more comments

3 answers

5

The browser does not send email, but it is possible to create a link that already calls the user’s email client, with address, subject and formatted content so that the email can be sent.

Following example:

<a href="mailto:[email protected]?Subject=Aqui%20vai%20o%20assunto&Body=E%20aqui%20o%20corpo">preparar email</a>

This same technique can be used next to your form, example:

<form id="contatoForm" method="get" action="mailto:[email protected]" target="_blank">
    <input type="hidden" name="Subject" value="Meu assunto">
    <input type="hidden" name="Body" value="">
    <input class="cells" type="text" name="nome" placeholder="nome">
    <input class="cells" type="text" name="email" placeholder="email">
    <button type="submit" value="click" id="enviar"></button>
</form>

You will need a small Javascript to format your text.

Native Javascript:

document.getElementById('contatoForm').addEventListener('submit', function () {
    var nome = this.querySelector('input[name=nome]'), nome = nome.value;
    var email = this.querySelector('input[name=email]'), email = email.value;
    var texto = 'Olá destinatário, \nMeu nome é '+ nome +' e meu email é '+ email;
    this.querySelector('input[name=Body]').setAttribute('value', texto);
});

DEMO

Version in jQuery:

$('#contatoForm').on('submit', function () {
    var nome = $(this).find('input[name=nome]'), nome = nome.val();
    var email = $(this).find('input[name=email]'), email = email.val();
    var texto = 'Olá destinatário, \nMeu nome é '+ nome +' e meu email é '+ email;
    $(this).find('input[name=Body]').attr('value', texto);
});

DEMO 2

Considerations:

  • The form action should be GET (and not POST, some browsers will accept POST, not all);
  • I added target to the form for compatibility with email clients built into the browser;
  • Your email input was with email=email and is name="email", could cause an error when selecting the field;
  • Don’t forget that the DOM must have been loaded so you can add the Systener to form event;
  • 2

    Well, I considered the answer of @Gabriel Gartz to be correct, because what I wanted is not to be done without hosting my page on a server, so I adjusted my page to do what he gave me as an answer to top! Thank you all for your help!

  • @Guilhermevianna good to know that my answer helped you, it was nostalgic to write this answer, I had to remember how to format email with Uri, something that comes from the early days of browsers.

3


According to your comment I would use this form directing it via POST to a Struts action and in this action I would use Velocity to build the HTML template of what email will be.

Then, to actually send the email, you have to have login information, password, POP or IMAP path of the server as well as the port to send using the email API itself Java EE. I particularly use Apache Commons Mail that internally uses the Java EE API, but simplifies for us developers how to do it.

1

Browsers do not send email. Your code is the closest you will get using just the browser.

You can’t email only with HTML & Javascript -- in web applications, you can only send emails securely on the server side.

There are ways to send email without having a server request AJAX to an external service as this, but they are not recommended because they are insecure (it is easy to email your name), with high potential for abuse.

  • I get it, I’ll host it...doing that you know tell me how to make it as simple as possible @Alice?

  • @Guilhermevianna So, I think the simplest way is to send with Gmail, so you don’t have to set up an SMTP server. If you are going to use PHP on the server, you can follow this tutorial.

Browser other questions tagged

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