I am trying to write a function to send an email when the user clicks on "submit".
Any ideas how to do so?
Lets say I have a
<script>
function mail()
{
var players = document.getElementById('players').value;
var slots = document.getElementById('slots').value;
}
</script>
and I want to generate an mail with those three vars.
Thanks all :)!
You can't send emails from (client-side) JavaScript alone. What you can do is write a server-side program that takes inputs (maybe as a POST or XHR request), creates an email out of it and sends it.
You may need to post the details to a server which would mail or you can use the mailto function to invoke default mail client on the client's computer like
<form action="mailto:sample#stackoverflow.com" method="post" enctype="text/plain" >
variable1:<input type="text" name="v1">
variable2:<input type="text" name="v2">
<input type="submit" name="submit" value="Submit">
</form>
You will have to use it in combination of a server side language and javascript. You can use javascript to write the "interface" to the mail function. But you will need to look into php or another language.
A php example of this is
You can find more details on the manual here http://us.php.net/manual/en/book.mail.php
pinehead.tv fighting for smarter newbies
"You may need to post the details to a server which would mail or you can use the mailto function to invoke default mail client on the client's computer like
variable1:
variable2:
"
This answer above will not work for people who do not have a mail client installed on their os. the mailto feature in html will open up the users "outlook" type program to send the mail.
Related
I have HTML form with a textbox where user enter his email - let say to register for a newsletter and a button to join.
When clicking the button I'd like to send myself an email with a constant subject like: 'a new user joined your newsletter' and in the body of the email have the text entered by the user.
So with PHP and Javascript code its possible - i'm looking for a pure html code that does the same (in my index.html file)
using <a href: mailto...> or <form action=mailto:... method=post> or <button onclick:mailto...> opens my mail application :(
Is there a way to send that email in the background (without opening the mail application) with the data from textbox in the email body?
if Yes to Q.1 then is there a way to add a fixed subject
No, there is no way you can send email using pure html. Email is sent over SMTP protocol. A browser operates in HTTP protocol. So it's not possible to send email even using pure javascript. Only server can send data using SMTP protocol.
No because you can not send an E-Mail without using an SMTP server. This server must be contacted by PHP or another server-side script (server must receive a form request and process it). You can not contact a SMTP server via pure HTML (that would be really insecure btw).
Regarding mailto: This only works if your clients have installed a mail client on their local machine. This mail client must be properly configured in order to - guess what - contact an SMTP server in order to send the mail.
However it is possible passing a subject via mailto:
https://css-tricks.com/snippets/html/mailto-links/
I have been researching for that problem for many hours, and didn't find anything.
So, I have a static html page and button inside it, like that:
<body>
<button id="0">SEND EMAIL TO my#email.com</button>
</body>
And if I press this button, message "Hello" will be sent to my#email.com from my2#email.com
Is it possible to do that thing using only html or javascript or jquery (because i know only that languages)?
There are three ways to do it
Harder Way
You have to implement server code to send a mail
Less Harder Way
You have to use mailgun or sendgrid rest api to send a mail using javascript.
Simpler Way
You have to use https://formspree.io/ to send a mail from your HTML.
Update:
Recently I found a way to send email using Google script. You don't need the backend. Explained here https://github.com/dwyl/html-form-send-email-via-google-script-without-server
You can use :
<body>
<a href = 'mailto:my#email?body="Yourbody"&subject="a subject".com'>SEND EMAIL TO my#email.com</a>
</body>
It will open a mail manager (outlook, gmail, ...) to send a new mail. You can describe the body and the subject inside the link
Otherwise you can send data to PHP with a form tag and send an email this PHP.
Directly sending mail from static web page is not possible.
You can use third party service, i am using service from formspree and it is working fine for me.
All you have to do is create your account in formspree, verify gmail address and use below snippet in your HTML page.
<form action="http://formspree.io/your#email.com" method="POST">
<input type="email" name="_replyto">
<textarea name="body">
</textarea>
<input type="submit" value="Send">
</form>
formspree is self explainatory.
I have very simple web site (which is actually single page), there is one input field and a button.
I need to store data submitted by users somewhere on server side. Perfect way could be simple text file and new lines appended to it after each button click. Log file will be also ok.
As I understand it is not possible with JavaScript itself. I'm looking for easiest solution, preferably with no server-side programming (but if it is required, it should be as easy as possible and work out-of-box). I can use some side service if it could be helpful.
Please help.
Thanks in advance.
UPD. Just want to rephrase the main question. I do not really need to store something on server side. I need to collect some input from users. Is it possible? It would also be ok if it for example will be just sent to my e-mail.
For a very simple form-to-server-log script:
Your form:
<form action="save-to-log.php" method="POST">
<fieldset>
<legend>Add to log</legend>
<p>
Message:
<textarea name="message"></textarea>
</p>
<p>
<input type="submit" value="SAVE" />
</p>
</fieldset>
</form>
Then save-to-log.php
<?php
$log_file_name = 'mylog.log'; // Change to the log file name
$message = $_POST['message']; // incoming message
file_put_contents($log_file_name, $message, FILE_APPEND);
header('Location: /'); // redirect back to the main site
if it's a unix host you'll need to add 755 permissions to the directory of the log so PHP has access to write to it. Other than that, you'll have a form that keeps appending information to mylog.log.
Follow-Up
If you don't necessarily need it store on the server (you mentioned email) you can use the following instead as the PHP script:
<?php
$to_email = 'kardanov#domain.com';
$subject = 'User feedback from site';
$message = $_POST['message'];
// this may need configuring depending on your host. If you find the email isn't
// being sent, look up the error you're receiving or post another question here on
// SO.
mail($to_email, $subject, $message);
header('Location: /');
You can't store information on the server without some sort of server side script.
There are two different places to store data, on the client and on the server.
On the client side, there are lots of ways from cookies to Store.js, however it sounds like you want to store the information on the server.
To store on the server you need some sort of application that can receive posts from javascript/http and save them in a file.
In your case a very simple PHP script like the below would be perfect:
<?php
//Was the request (post or get) parameter data supplied?
if(!empty($_REQUEST['data']) {
$file = 'log.txt';
$data = $_REQUEST['data']."\n";
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
}
?>
How about dumping JSON to a file with PHP and then loading it on request?
How to safely write JSON data to file using PHP
If you want to get the data sent to your email address, there are several free services that can do this without installing any server side applications... A PHP or CGI script is still being used but it is hosted by the service, not by you.,
All you have to do is paste some code into your site and then all submitted data will be sent to your email address., A lot of people don't have the know-how to do this on their own, or their hosting service will not allow send-mail to work. Thats why these services exist. And of course most of them are supported by ads that are placed in the email that you get from the form. Anyway, here is the link for a good service I found. You can also Google "Free Form Processing" to find more.
https://secure.tectite.com/hf/auth/GetStarted?WWWTECTITE
Hope this helps.
I am developing a static website where in i want to send an email when HTML "Send Button" is clicked. IF it is possible it would be nice if u can share the code
Thanks
short answer: No!
Long answer:
No, you can't send emails from static pages even using javascript. What you can do is to use ajax to send email from your html form.
It is possible ;-)
<input type="submit" value="Send" onclick="window.location ='mailto:email#address.com' "/>
In short: Forget it.
If you want to send email with the slightest resemblance of reliability, use a server side process. If you want to use JavaScript then you can use Ajax to pass the data to that server side process and/or SSJS.
It is not possible to send email from Javascript, you'll need to write a page in a language like PHP to receive the POST data and use the mail() command there.
I'm sorry but just with a static website you can't do it. One imaginable possibility would be make a very complex javascript code to access one webmail account you have, login there and send the email using this account to you. It's theoretically possible, but I've never seen something like it...
Am very New to Web Page Development.
In my website i have the help page like Name, Email ID, Contact No, Comment and Submit Button Suppose i entered the data in the help page , then i press submit button means that entered data's should sent to the email account
For Example
Help Page
Name - Raja
Email - raja#gmail.com
Contact - 98763214
Comments - Good web page
Then I pressed Send button means, the above entered data should send to this email account
support#it.com with the same format.
Need Code Help or Script Help
use FormMail
you should set these variables in formmail.pl
#referers = ('yourdomain.com','ip_number');
look at this
and
#recipients = ('^support\#it\.com');
look at this
and
$mailprog = '/usr/lib/sendmail -i -t';
which is the path of sendmail in your system. if you're not sure where is your sendmail path, use the following command at your unix prompt:
which sendmail
This will tell you where sendmail is on your system. If it says it could not find it, just ask your system administrator.
after configured formmail.pl, put it in cgi-bin folder.
here is an example of a form using formmail.pl ( recipient field is required )
<form name="myform" action="http://yourdomain.com/cgi-bin/test.pl" method="GET">
<input type=hidden name="recipient" value="support#it.com">
<input type=textarea name="mytext"/>
<input type="submit" />
</form>
for more information about how to use formmail, go here.
Like Jørn Schou-Rode said, you really need to use a server-side programming language to catch and process the form data. The options are numerous - PHP, ASP, .NET, ColdFusion, Java, Ruby... the list goes on. See http://en.wikipedia.org/wiki/Server-side_scripting as a starting point.
I remember starting out in web development and wanting to do the same thing but you really are now getting into more advanced areas of development (beyond simple client-side HTML/CSS/JavaScript coding).