35

Is it possible to implement mailto: function on submit button like <input type="submit" />? I use ASP.NET MVC. Maybe there is some tricky controller action result to achieve this. Could you please help me?

P.S. I know that I can make anchor looks like a button.

Pavel Shchegolevatykh
  • 2,568
  • 5
  • 29
  • 32

6 Answers6

54

In HTML you can specify a mailto: address in the <form> element's [action] attribute.

<form action="mailto:youraddr@domain.tld" method="GET">
    <input name="subject" type="text" />
    <textarea name="body"></textarea>
    <input type="submit" value="Send" />
</form>

What this will do is allow the user's email client to create an email prepopulated with the fields in the <form>.

What this will not do is send an email.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 3
    Thank you. But there is still a problem. When I add subject with spaces in it spaces changes to plus ('+') signs. Why is it? How to fix that? – Pavel Shchegolevatykh Sep 28 '12 at 13:45
  • 2
    Try enctype="text/plain" attribute on
    – Dg Jacquard Mar 27 '14 at 01:57
  • 1
  • 1
    in firefox, using 'post' makes it not recognize the subject as a subject. the fields get posted in the body. using 'get' translates spaces to plus signs, also on enctype=text/plain. – commonpike Oct 23 '14 at 15:36
  • 1
    @olibre, if you'd actually tested that change you'd know that it doesn't actually solve the problem. – zzzzBov Nov 24 '14 at 14:23
  • The HTML5 standard specifies that POST should serialize form elements serialized into the email body and GET should include them in the URL. This is why you must use GET to set `subject` and `body` but POST cannot. It is also why you cannot avoid `+` in subject/body. https://www.w3.org/TR/html5/forms.html#submit-mailto-headers – William Entriken Jan 17 '17 at 21:46
  • If you want to cheat, use a `POST` request and edit the action to include `?subject=`. This lets you URL-encode the subject and avoid `+`s in your mail client. The best solution is to use javascript to workaround HTML's failure to properly URL-encode the body. – William Entriken Jan 17 '17 at 21:57
  • This approach has a problem with security. At least on chrome, you will see a message like "Your connection to this site is not fully secure". – Helio Albano Oct 27 '20 at 12:47
  • enctype="multipart/form-data" ? – Jean G.T Dec 21 '21 at 17:51
54

This seems to work fine:

<button onclick="location.href='mailto:em@i.l';">send mail</button>
ahsteele
  • 26,243
  • 28
  • 134
  • 248
Greald Henstra
  • 568
  • 5
  • 6
  • 2
    Nice, in addition to this i also found [this quide](https://www.rapidtables.com/web/html/mailto.html) useful about how to pre-set various parts of the email via url query parameters. – Viktor Borítás Jun 02 '21 at 03:55
4

What you need to do is use the onchange event listener in the form and change the href attribute of the send button according to the context of the mail:

<form id="form" onchange="mail(this)">
  <label>Name</label>
  <div class="row margin-bottom-20">
    <div class="col-md-6 col-md-offset-0">
      <input class="form-control" name="name" type="text">
    </div>
  </div>

  <label>Email <span class="color-red">*</span></label>
  <div class="row margin-bottom-20">
    <div class="col-md-6 col-md-offset-0">
      <input class="form-control" name="email" type="text">
    </div>
  </div>

  <label>Date of visit/departure </label>
  <div class="row margin-bottom-20">
    <div class="col-md-3 col-md-offset-0">
      <input class="form-control w8em" name="adate" type="text">
      <script>
        datePickerController.createDatePicker({
          // Associate the text input to a DD/MM/YYYY date format
          formElements: {
            "adate": "%d/%m/%Y"
          }
        });
      </script>
    </div>
    <div class="col-md-3 col-md-offset-0">
      <input class="form-control" name="ddate" type="date">
    </div>
  </div>

  <label>No. of people travelling with</label>
  <div class="row margin-bottom-20">
    <div class="col-md-3 col-md-offset-0">
      <input class="form-control" placeholder="Adults" min=1 name="adult" type="number">
    </div>
    <div class="col-md-3 col-md-offset-0">
      <input class="form-control" placeholder="Children" min=0 name="childeren" type="number">
    </div>
  </div>

  <label>Cities you want to visit</label><br />
  <div class="checkbox-inline">
    <label><input type="checkbox" name="city" value="Cassablanca">Cassablanca</label>
  </div>
  <div class="checkbox-inline">
    <label><input type="checkbox" name="city" value="Fez">Fez</label>
  </div>
  <div class="checkbox-inline">
    <label><input type="checkbox" name="city" value="Tangier">Tangier</label>
  </div>
  <div class="checkbox-inline">
    <label><input type="checkbox" name="city" value="Marrakech">Marrakech</label>
  </div>
  <div class="checkbox-inline">
    <label><input type="checkbox" name="city" value="Rabat">Rabat</label>
  </div>

  <div class="row margin-bottom-20">
    <div class="col-md-8 col-md-offset-0">
      <textarea rows="4" placeholder="Activities Intersted in" name="activities" class="form-control"></textarea>
    </div>
  </div>


  <div class="row margin-bottom-20">
    <div class="col-md-8 col-md-offset-0">
      <textarea rows="6" class="form-control" name="comment" placeholder="Comment"></textarea>
    </div>
  </div>

  <p><a id="send" class="btn btn-primary">Create Message</a></p>
</form>

JavaScript

function mail(form) {
    var name = form.name.value;
    var city = "";
    var adate = form.adate.value;
    var ddate = form.ddate.value;
    var activities = form.activities.value;
    var adult = form.adult.value;
    var child = form.childeren.value;
    var comment = form.comment.value;
    var warning = ""
    for (i = 0; i < form.city.length; i++) {
        if (form.city[i].checked)
            city += " " + form.city[i].value;
    }
    var str = "mailto:abc@x.com?subject=travel to morocco&body=";
    if (name.length > 0) {
        str += "Hi my name is " + name + ", ";
    } else {
        warning += "Name is required"
    }
    if (city.length > 0) {
        str += "I am Intersted in visiting the following citis: " + city + ", ";
    }
    if (activities.length > 0) {
        str += "I am Intersted in following activities: " + activities + ". "
    }
    if (adate.length > 0) {
        str += "I will be ariving on " + adate;
    }
    if (ddate.length > 0) {
        str += " And departing on " + ddate;
    }
    if (adult.length > 0) {
        if (adult == 1 && child == null) {
            str += ". I will be travelling alone"
        } else if (adult > 1) {
            str += ".We will have a group of " + adult + " adults ";
        }
        if (child == null) {
            str += ".";
        } else if (child > 1) {
            str += "along with " + child + " children.";
        } else if (child == 1) {
            str += "along with a child.";
        }
    }

    if (comment.length > 0) {
        str += "%0D%0A" + comment + "."
    }

    if (warning.length > 0) {
        alert(warning)
    } else {
        str += "%0D%0ARegards,%0D%0A" + name;
        document.getElementById('send').href = str;
    }
}
ahsteele
  • 26,243
  • 28
  • 134
  • 248
1

Or you can create a form with action:mailto

<form action="mailto:rohit@k.com"> 

check this out.

http://webdesign.about.com/od/forms/a/aa072699mailto.htm

But this actually submits a form via email.Is this what you wanted? You can also use just

<button onclick=""> and then some javascript with it to ahieve this.

And you can make a <a> look like button. There can be a lot of ways to work this around. Do a little search.

geekman
  • 2,224
  • 13
  • 17
1

Just include "a" tag in "button" tag.

<button><a href="mailto:..."></a></button>
Nancy
  • 59
  • 1
  • 1
    Hacky, but was exactly what I was looking for (not in a form, but on a button). – anonameuser22 Oct 01 '15 at 05:03
  • 4
    That ` – Null Jul 08 '19 at 09:32
  • This is in my opinion the easiest solution and works well, however @Null is correct: you don't need the ` – MikhailRatner Nov 29 '21 at 15:39
  • This is invalid HTML code and should never be used! ```Error: The element a must not appear as a descendant of the button element.``` Some screen readers will also have issues with that. – Kayzah Mar 22 '23 at 18:13
1

The full list of possible fields in the html based email-creating form:

  • subject
  • cc
  • bcc
  • body
<form action="mailto:youraddr@domain.tld" method="GET">
  <input name="subject" type="text" /></br>
  <input name="cc" type="email" /><br />
  <input name="bcc" type="email" /><br />
  <textarea name="body"></textarea><br />
  <input type="submit" value="Send" />
</form>

https://codepen.io/garfunkel61/pen/oYGNGp

ahsteele
  • 26,243
  • 28
  • 134
  • 248
garfunkel61
  • 1,459
  • 12
  • 10