Creating a personalized contact form with a mailto link that also includes the ability to automatically copy information into the email

I'm in the process of developing a basic contact form that includes input fields for name and subject, as well as a send button (which is an <a> element inside a <div>. I am utilizing an <a> tag because I intend to use mailto functionality).

My goal is to transfer the entered name and subject from the form to the email upon clicking "send".

Could you please provide guidance on how to achieve this?

Here is the link to the Jsfiddle

Below is my code snippet:

p{
  width:50%;
  color: #666;
  text-align: center;
  margin: 0 auto;
}
.name_input{
  display:block;
  margin : 50px auto;
  padding-left:10px;
  border-radius:5px;
  border:2px solid rgb(255,194,0);
  width: 50%;
  height:30px;
}

.btn{
 text-align:center;
  background-color:rgb(255,194,0);
  padding:10px;
  margin: 10px auto;
  width:30%;
  cursor :pointer;
}

.btn:hover{
  background-color: #666;
}

a{
  text-decoration:none;
  color:#FFF;
}
<form>
  <p>
  Enter the subject and your name, click "send" to have the subject included in the email subject line, and your name in the email body.
  </p>
   <input class="name_input" type="text" name="contact-object" id="contact-object" placeholder="The object"/>
  <input class="name_input" type="text" name="contact-name" id="contact-name" placeholder="Your name"/>
  
  <div class="btn">
  <a  href="mailto:www.myWebSite.com?subject=THE OBJECT DYNAMICALLY &body=THE NAME DYNAMICALLY">send</a>

  </div>
</form>

Answer №1

If you want to use mailto as the action attribute for a form, you can do so by following this example:

<form action="mailto:www.myWebSite.com" method="GET" enctype="text/plain">
  <p>
  Input the desired subject and your name, then click "send" - the subject will be in the email's subject line, and your name will appear in the body of the email.
  </p>
   <input class="name_input" type="text" name="subject" id="contact-object" placeholder="Subject"/>
  <input class="name_input" type="text" name="body" id="contact-name" placeholder="Your Name"/>

  <div class="btn">
  <input type="submit" value="Submit" />

  </div>
</form>

Answer №2

If you want your email link to open like a normal mailto link, you'll need some javascript. The first step is to create a function that constructs the link for you.

function buildEmailLink() {
    var recipient = document.getElementById('recipient-email').value;
    var subject = document.getElementById('email-subject').value;

    var emailLink = "mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c716559717d75705c7873717d7572327f7371">[email protected]</a>?Subject=Regarding: " + recipient + "From: " + subject;

    // Make sure to URI escape the link
    return encodeURI(emailLink);
}

Next, create a function to insert this link into the appropriate element.

function addEmailLink() {
    document.getElementById('email-element').href = buildEmailLink();
}

Lastly, make sure these functions are triggered whenever there's a change in the input fields.

<input class="email_input" type="text" name="recipient-email" id="recipient-email" placeholder="Recipient Email" onchange="addEmailLink();"
onkeyup="this.onchange();" onpaste="this.onchange();" oninput="this.onchange()/>

<input class="email_input" type="text" name="email-subject" id="email-subject" placeholder="Email Subject" onchange="addEmailLink();"
onkeyup="this.onchange();" onpaste="this.onchange();" oninput="this.onchange()/>;

Answer №3

To complete this task, JavaScript (js) will be required.

The first step is to ensure that the href attribute is added correctly to the <a> tag, as shown below:

<a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80e9eee6efc0edf9d7e5e2d3e9f4e5aee3efed">[email protected]</a>?subject=Your Subject&body=Your Body>send</a>

A possible solution would involve creating a function like this:

var mailButton = function mailButton(mailto, title, mailBody){
   var href = "mailto:"+mailto+"?subject="+title+"&body="+mailBody;
   var a = document.createElement(a);
   a.setAttribute('href', href);
   return a
}

Next, include a function to manage form submission and click events for the <a> element

function fakesubmit(e){
   e.preventDefault();
   var title = document.getElementById('contact-object').value;
   var mailBody = document.getElementById('contact-name').value;
   var a = mailButton(<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="731a1d151c331e0a241611201a07165d_101c1e">[email protected]</a>, title, mailBody);
   a.click();
}

Finally, create your form:

<form onsubmit="fakesubmit(e)">
  <p>
  Enter the object and your name, click on "send". The object will be in the email subject and your name will appear in the email body.
  </p>
   <input class="name_input" type="text" name="contact-object" id="contact-object" placeholder="Type the object"/>
  <input class="name_input" type="text" name="contact-name" id="contact-name" placeholder="Your name"/>

  <input type="submit" class="btn" value="Send"/>
</form>

If you require further assistance, feel free to ask.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve a text and save it into a variable

Is there a way to extract a specific string and save it as a variable? For example, if I have the following URL: http://sub.site.com/WordsHere-t.jpg I am looking to extract just WordsHere. The length of this string can vary and will not always be 9 chara ...

Associate the right-click action with Angular Material

I'm currently working with Angular Material. My goal is to develop a directive that allows me to trigger a right-click event on an element. This is what I have attempted so far: JavaScript: app.directive('rightClick', ["$parse", function ...

Text arranged in a vertical orientation with a group of text aligned along the baseline

I am trying to create the following layout: https://i.sstatic.net/HYNDw.png Here is what I need: The word total and the number 120 should align vertically in the center The words per month should align at the bottom with respect to the number 120 The ...

Is it just me, or does the float left - float right combination only break in IE

Oh dear, it seems like IE9 is causing some trouble again. The other "capable" browsers handle this HTML just fine, including IE8 which isn't even that great. Here's the code snippet: <div class="contact_info_city" id="contact_info_cityX"> ...

Is it acceptable to include a checkbox and a hidden input within a label element?

When creating a list of possible products for users to buy, I use the ul tag combined with li. Each product element includes a checkbox that allows users to select the product or not. For products with related information, I want to store this data in a h ...

Ways to combine duplicate entries within a column using Ruby on Rails?

I need some help with a filtering issue related to sign names. I am trying to merge the content together if there is more than one name of the sign. You can refer to the image attached for better clarity, where I have two server names but I only want to di ...

Setting up the php/MVC framework on a server

Currently, I have developed a project using HTML/PHP and need guidance on uploading it to the server. My knowledge of servers is limited, but I do understand that the first page should be an index.php or index.htm file placed in the public_html folder. How ...

A single list is utilized by multiple HTML5 selects

If I have 10 fields in a form, each requiring a select option from the year 1950 to 2017, can I create one list from that range and have each select reference it? Or do I need to make ten separate lists for each select? Edit: An example could be the birth ...

PHP script to populate a table with images based on certain conditions

I have a table in HTML that displays results from a SQL database on the right hand side. The code snippet for populating each cell is as shown below: <tr> <td>House</td> <td><?php echo $row_buyerdetails['tod_hous ...

Is there a way to make a try-catch block pause and wait for a response before moving

I've been successfully retrieving data from my Firestore database, but I've encountered a major issue that I can't seem to resolve... Whenever I click the "Read Data" button, I have to press it twice in order to see the console log of the d ...

Using Jquery to extract URL parameters

Can anyone suggest a jQuery function I can use to fetch URL parameters? I've been utilizing the following function, which works well; however, it encounters issues when the URL doesn't have any parameters. I would like it to return an empty stri ...

JavaScript: Creating a new entry in a key-value paired array

I am in the process of creating a dynamic menu for use with jQuery contextMenu. I have encountered an issue when trying to add a new element, as it keeps showing the error message 'undefined is not a function'. The menu functions correctly witho ...

Is there a way for me to retrieve information from a different website using a URL, similar to how Digg's submit button works

Currently, I am in the process of developing a website with the cakePHP framework. As a beginner in PHP and web programming, my goal is to implement a feature similar to Digg's submit button. This functionality would involve inputting a URL, which the ...

Validating forms using Ajax in the Model-View-Controller

I am facing an issue with my Ajax form, where I need to trigger a JavaScript function on failure. The code snippet looks like this: using (Ajax.BeginForm("UpdateStages", new AjaxOptions { HttpMethod = "POST", OnSuccess = "refreshSearchResults(&apo ...

Tips for expanding the content of a blogger page to fill the entire frame of the page

Check out this page: . Currently, the video on the page does not fill up the entire screen. Any suggestions for a solution? ...

Accessing external data in Angular outside of a subscription method for an observable

I am struggling to access data outside of my method using .subscribe This is the Service code that is functioning correctly: getSessionTracker(): Observable<ISessionTracker[]> { return this.http.get(this._url) .map((res: Response) => ...

Rect cannot be resized using mouse events

I am currently working on resizing the rectangle inside the SVG using mouse events. To achieve this, I have created another circle shape at the right bottom edge of the rectangle and implemented resize events on that shape. However, I'm facing an issu ...

Error in Webpack: JSX elements that are adjacent must be enclosed within a wrapper tag

After adding a new component and integrating it into my Main component, I encountered an error when running webpack. The error message displayed was: "Adjacent JSX elements must be wrapped in an enclosing tag" Below is the snippet of code where the iss ...

Looking for Assistance with Creating a Non-Adhesive Footer in CSS?

My goal is to have the footer remain at the bottom of the page, which I achieved using the following CSS: position: absolute; bottom: 0px; While this code does bring the footer to the bottom as desired, the issue arises when the window is resized to a sm ...

Unable to move cursor in contenteditable section

I am currently working on developing a rich text editor in React, but I have encountered an issue that has me stuck. The problem I am facing is that as I type each character, the insertion point does not update accordingly, causing the cursor to remain stu ...