Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Manuel Alejandro Matus de Quevedo
Courses Plus Student 6,407 PointsDoes anyone can explain me how to make a form send emails, and where to put that script in my document?
I'm putting this in my code:
<form id="signup-form" method="post" action="#"> <input type="email" name="email" id="email" placeholder="email" /> <input type="submit" value="Subscribe" /> </form>
But I'm not sure what else I have to input, like if I will need to add some JavaScript code or AJAX, I would like to know how can I be able to configure that option since I'm designing a "Coming Soon" webpage
4 Answers

niveditagautam
Courses Plus Student 3,001 PointsBasically your form should be posted to the server and that server should connect via SMTP to some mail provider like Gmail and send that e-mail.
You can't do that by just using JS or AJAX (these are client side). Although you can use make a call to a serverside code hat will send the mail using AJAX.

Manuel Alejandro Matus de Quevedo
Courses Plus Student 6,407 PointsDo you know how can I link that field to the server? Because I'm not sue what to do... It's my first website, and I just started to learn codon

niveditagautam
Courses Plus Student 3,001 PointsI think you got what you were looking for. But just to answer what you asked:
Create an HTML form like you have done already:
<form action="">
<input type="email" name="email" id="email" placeholder="email" />
<input type="submit" value="Subscribe" />
</form>
Then you will have to use a server side language like PHP to handle the request when you submit the form. Put the name of that file in action attribute. For ex:
<form action="sendmail.php">
Inside sendmail.php you have to write the code for connecting to some mail service provider. PHP has a function for this:
mail(to,subject,message,headers,parameters);
You can refer to the documentation of this function here:
http://php.net/manual/en/function.mail.php
A sample code for sendmail.php would be:
<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);
?>
Good luck!

Rachelle Wood
15,362 PointsNivedita basically answered your question for you but I will clarify by suggesting some languages and therefore some potential courses to look at on this site. From how I take your question, you are trying to get users to sign up to an email newsletter on your website by filling in and submitting a form? Is that correct?
It sounds like you have most of your front-end working though possibly not the interactive part that uses JavaScript (?). Why not look at the AJAX course on here? You will get a sense of what you can and can't do with AJAX and how it works (plus some superficial things about security).
Then you will need to store the email signups in a database on the server. This is the whole back-end part and requires knowledge of languages like PHP and MySQL. There is an entire track on PHP on this site. There is also something on MySQL that is wrapped in one of the Developer Tools courses (I think it is the Website Basics course). You will also need to know how to use server-side databases which is given in a Developer Tools course as well.
There is a way of doing back-end "stuff" with JavaScript too by using Node.js, but that might be like being baptized by fire if you are just learning how to code. There are several Node courses on Treehouse, but you have to already have solid knowledge of JavaScript and how servers work.
Good luck and be sure to keep your users' data safe!

Manuel Alejandro Matus de Quevedo
Courses Plus Student 6,407 PointsThanks for answering! Base hat I'm trying to do is just like user will input an email address, not to store the info, since my customer is requesting me to have the user's info send to a generic account (info@companyname.com). Will I have to use MySQL, Ajax or Server side info?

Rachelle Wood
15,362 PointsI am not totally sure I understand your question. What is it you are trying to do exactly? Capturing a user's information from submitting a website's form is not as straightforward as the information going from their browser to your email inbox if that is what you are asking. There is a server in there somewhere in between :)
You have to have somewhere to store that user's information if you are going to be asking a user to fill out a form on your website and then them submitting it. The interactivity of the webpage will be in JavaScript or jQuery if you prefer to use that instead. The actual posting and getting requests will be AJAX. The users' information will be stored in database form in something like MySQL on the server. You can manipulate the data with a language like PHP or by using Node.
If you want to completely circumvent the requirement to store information on a server, then my suggestion is to not have a form on your website at all and simply have a contact page with info@companyname.com on it so that customers can get directly in contact with you. That will only work though unless you are either going to have only a very few customers or if you are willing to sift through a lot of emails in order to record the email addresses.

Tom Bedford
15,645 PointsHi Manuel, forms are quite a step up from basic HTML and CSS. If you do not currently know the needed languages, or don't have the time right now, I would suggest using something like Wufoo where you can make a form and embed it in your existing site. It is free to use if you receive less than 100 form entries per month.
I used Wufoo on the first couple of sites I made a few years ago and it was very easy to add in without having to worry about the server communication, sanitisation, validation, browser compatibility, stopping spam robots and so on.

Manuel Alejandro Matus de Quevedo
Courses Plus Student 6,407 PointsThank you! I've already the website created, I just need to add this attribute then. One more question, If I already have the design of this subscribe option... what do I do to keep the same style/design?

Tom Bedford
15,645 PointsWhen I used it you were able to link to an image for the submit button (was in the theme options). You should be able to replicate the style that way.
I believe there are options to use your own css for styling though I am not sure of that is a paid feature - sorry I have not kept up to date with their developments.
niveditagautam
Courses Plus Student 3,001 Pointsniveditagautam
Courses Plus Student 3,001 PointsWhat are you using on server side?