JavaScript equivalent of PHP mail() - javascript

Is it possible to send an email using JS, like PHP's mail() function.
I know you can use mailto in the action, like this:
<form action="mailto:example#example.com">
<p>Type In Your Email</p>
<input type="email">
<input type="submit">
</form>
But that just opens up the default email program. I want to send an email to the email they entered, from a different address.
Is there a way to send an email in JS, from a custom email address, without opening the default mail client?

No. You can't send an email using HTML form action. You'll need to do it using server-side languages like PHP.

Nop., with the help of only HTML you we can't send mail() because for sending emails we need to set server-side script so that server can send or receive requests

You can do it it javascript or jquery.
<form id="frm" action="mailto:example#example.com">
<p>Type In Your Email</p>
<input id="email" type="email">
<input id="btn" type="button">
</form>
<script>
$(document).ready(function(){
$('#btn').click(function(){
$('#frm').attr('action', 'mailto:' + $('#email').val() ) ;
$('#frm').submit();
});
});
</script>

Yes, you can use python or ruby
...the title is misleading.
No, you'd have to use something in addition to JavaScript and html

Related

Submit form with HTML and JS to email

I am using a form for people to submit contact requests for a website. I'm using JS and HTML for this website. I've tried to use mailto but it doesn't actually send anything to my email when I press submit. I don't want to use PHP if I can avoid it since I don't know PHP that well.
Here is my HTML
<form method=POST action="mailto:naomikudren#gmail.com" enctype="text/plain">
Company<br>
<input type="text" name="companyname"><br>
Contact Person<br>
<input type="text" name="contactname"><br>
Phone number<br>
<input type="tel" name="phonenumber" min="6" max="15"><br>
Email<br>
<input type="email" name="email"><br>
Message<br>
<input type="textarea" name="message" style='white-space:pre-wrap; height:200px;width:500px;'><br>
<input type="submit" value="Submit">
</form>
I haven't done anything in the JS document for this to work. Do I have to use some sort of command for when submit is pressed in JS or should it be enough to do all this in HTML?
You cant send email directly using Javascript, leave aside HTML.
what you can do is open another window with Mail To option.
window.open('mailto:test#example.com');
//or with subject using below
window.open('mailto:test#example.com?subject=subject&body=body');
Otherwise you can do AJAX call to server which sends mail, but that will need PHP or other backend programming.
You have to use a server side language to send emails .. You cannot do it using client side language like js. So better use a simple php function to send email. It's not difficult ...
You cant send email directly using Javascript. For this you have to use php Mailing function, which has a very simple syntax.
<?php
// the message
$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
mail("someone#example.com","My subject",$msg);
?>
"NO! JavaScript can't email a form! but, there are alternatives to send the form data to an email address.
There is no direct method provided by JavaScript to send the data submitted in the form to an email address.
The main concern for not providing a ‘JavaScript email form’ feature is security."

Contact Form with Javascript

Hello,
i'm trying to create a Windows 10 App with Javascript for self education. Today i want to try, send a email with message, Name from a Contact form. I'm only beginner on Javascript. can someone write a short code with example? I have a Contact form with PHP, but how can i send a Mail with Javascript?
<form method="post" action="send.js">
<input type="text" placeholder="Name" name="name" id="name" required=" ">
<input type="email" placeholder="Email" name="email" id="email" required=" ">
<textarea placeholder="Message..." name="msg" id="msg" required=" "></textarea>
<input type="submit" value="Submit">
</form>
Thanks
If you want to send e-mails via JavaScript, use Node.js Nodemailer.
https://nodemailer.com/Nodemailer - Send e-mails with Node.JS
You could also use
Send email
And that will open the default mail application on the user's device, and automatically fill in the To: field with that e-mail address.
Here is an example of how it works.
It is also possible to specify initial values for headers (e.g. subject, cc, etc.) and message body in the URL. Blanks, carriage returns, and linefeeds cannot be embedded but must be percent-encoded.
Send email
As far as I know you can't send mail with javascript. Normally you'd use javascript to send your form to your php page. You could use something like jquery and ajax the form to you php script so:
$(document).ready(function(){
$('#your-form-id').on('submit', function(){
var formData = $(this).serialize
$.ajax({
type: 'post',
url: 'your_url.php',
data: formData,
dataType: 'json',
success: function(response){
//Anything you want to do after form is submitted
}
});
});
});
This isn't tested code but I think it should be roughly right.

Retrieving Tumblr Username From HTML Form

I'm new to Javascript, and am currently writing a custom HTML page for my tumblr.
I understand that you can toggle a button to call a function in JS. I'd like to know how to retrieve a user's username when the button is clicked, within this function. I would like to email this information back to myself but can find no documentation on this anywhere :(
I understand the Tumblr has its own stock Ask form. However, I would like to customize my own. My HTML form is below:
<form action="sendUsername()" >
<textarea rows="4" cols="50" id="message"></textarea>
<input type="submit" value="Submit">
</form>
JavaScript
function sendUsername(emailAddress, message){
//email username and message to emailAddress
}
For the getting the username, you need
HTML:
<input type="text" name="username" id="username" />
and
JS:
var username = document.getElementByID('username').value;
to get the input value stored in a variable
However, I don't think you can send an email to yourself straight from Javascript without a third-party API. It can be done using PHP's mail() function though
http://php.net/manual/en/function.mail.php
If you don't want to use PHP at all, then have a look at this:
How to send an email from JavaScript

Form data won't send using Javascript to email

I am new to HTML and Javascript, but I have tried to do research on how to get a form to send its information to an e-mail address when the submit button is selected. Most of my research showed that PHP is needed, but when I asked my professor, he said it can be done using only javascript and the assignment needs to be submitted that way. Below is what I am trying to get to work.
<SCRIPT LANGUAGE="JavaScript">
function mailMe(form){
Subject=document.Registry.name.value;
location = mailto:XXXXXX#yahoo.com?subject="+Subject;
return true;
}
</SCRIPT>
<FORM NAME=“Registry” onSubmit="return mailMe(this.form)" >
<h3><font size=6pt> Visitor Registration </font></h3>
</br>
Name <input type="text" name=“name”><br>
<br>
E-Mail Address <input type="text" name=“mail”><br>
<br>
<INPUT TYPE="submit"><br>
</FORM>
It can't be done using only javascript or client-side technologies.
But you can probably open a new window with a mailto link.
Please note this won't send an e-mail, but instead open your local e-mail application to send an e-mail.
This is a really bad idea. You should use AJAX and PHP, this is really the better method.
If you really want your code, you have a few errors. This is correct:
var Subject = document.getElementById("name").value;
window.location = "mailto:XXXXXX#yahoo.com?subject=" + Subject;
Then you have to add an ID to the name field:
<input type="text" name=“name” id="name"><br>
But let me say that you really should use PHP mail(), because then not the client email program is used to send the mail and all is done in background.
I just tried the example below which I found at Microsoft and it worked fine. Can even be done without Javascript by simply adding the "mailto:" to the form action attribute. Also note that the form element names correspond to mailto query string parameters, i.e., "subject" and "body".
References: Microsoft mailto Protocol and RFC2368:The mailto URL scheme
Micosoft SharePoint, Adobe LiveCycle, and other middleware are able to process the emailed forms on the back end. Or one could roll their own using Java, C#, PHP, etc. Yet, that wasn't part of the class assignment ... er ... I mean question.
<html>
<body>
<form action="mailto:user#example.com" method="get">
<input name="subject" type="hidden" value="Message Title">
Feedback:<br/>
<textarea name=body cols="40">
Please share your thoughts here
and then choose Send Feedback.
</textarea>
<input type="submit" value="Send Feedback">
</form>
</body>
</html>

Can I use "form post mailto" to send page content by email?

I need to implement a function on my page that when user click a "Send mail" button, it will call local email client and put a div's content in the page into the mail body just like user manually copy-paste to email.
I try to use the code below:
<form action="mailto:someone#host.com" method="POST">
<input type="text" name="subject" />
<textarea name="body"></textarea>
</form>
and use javascript to set subject and body's values. But it seems email body cannot understand html code. It treats those tags as plain text. Besides, there's seems to be a char limit for body.
Can this function be done in front end? thanks a lot.

Categories

Resources