17

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)?

Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
rint
  • 255
  • 1
  • 3
  • 10
  • 1
    if you need to send the e-mail I think you need some server side scripting language. PHP is one of them and the mail function would help: http://www.php.net/manual/de/function.mail.php – caramba Apr 16 '14 at 13:45
  • Do you want to send a text wrote by the user? or send something static?. for this kind of work, you need the server side to handle mailings, unless you want just want it to open some email program like outlook.. – Oscar Reyes Apr 17 '14 at 05:42
  • @nosthertus I want to send a text wrote by user. User types login and password, and I want to have it. Actually this user's login and password is in the my unversity library's computer, and I want to hack all passwords typed by different users for fun :D What can I do? – rint Apr 17 '14 at 16:53
  • @rint its a bad idea to be hacking other users info.. anyways, this can only be done by server side, it might be PHP hosting or another kind of server hosting. You would have to get into server's logic files and rewrite the code in order to make it do what you want. – Oscar Reyes Apr 17 '14 at 18:46
  • Please share more details. Why is this question tagged with Javascript, PHP, jQuery, but does not contain any such code? – Nico Haase Oct 20 '21 at 11:40

3 Answers3

81

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

Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
  • formspree seens to be a good solution, but you will depend on their services and you can't control how it work. – Gwenc37 Apr 16 '14 at 14:02
  • Yes, if you want your full control, then you have to implement your server code. – Fizer Khan Apr 16 '14 at 14:03
  • 3
    it seems that neither [mailgun](http://stackoverflow.com/questions/29175006/ajax-function-w-mailgun-getting-error-request-header-field-authorization-is-n#comment-46758163) nor [sendgrid](http://stackoverflow.com/questions/17827253/sendgrid-api-json-call#answer-17846507) accept Ajax/JS requests because of CORS, intentionally unimplemented. – masciugo May 19 '15 at 12:31
  • 1
    mailgun does allow POSTs from webpages, see http://stackoverflow.com/a/18370329/1205281 – David Avikasis Jun 05 '15 at 09:18
  • Is it possible that you write a PHP script and that alone would do email sending thing? Because I tried it, and dint succeed. I followed instructions given here: http://www.html-form-guide.com/email-form/php-form-to-email.html But then may be i missed out something and couldnt receive mails.On further research I found out, that you have to change SPF records in order for this 'PHP Script solution to Send Email' thing work. Check here: http://www.html-form-guide.com/email-form/php-script-not-sending-email.html All PHP experts, Please do help me solve this issue note: I am unaware about PHP – Ashish Goyal Sep 26 '15 at 14:34
  • Thank you for the update, The Google script tutorial you linked is the best and besides sending the email to me from my website, it also logs all the responses to a google spreadsheet which is amazing! Thanks again! – Chilli Sep 23 '17 at 08:32
  • If you were to use mailgun or sendgrid client-side, you'd be exposing credentials. Even if you had a separate set for each site it would be really insecure. – quickshiftin Sep 18 '18 at 16:15
9

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.

Gwenc37
  • 2,064
  • 7
  • 18
  • 22
  • No, I want html page to do it automatically without any outlook etc. Can it be made without mailto? – rint Apr 16 '14 at 13:46
  • 1
    nope.. you can't. For that you have to add a simple mail script using PHP. No need to learn full PHP for that, http://www.w3schools.com/Php/php_mail.asp here is small tutorial. – saikiran Apr 16 '14 at 13:49
  • 1
    html can't send emails, It's not a programming language, it's a markup language. – Jonast92 Apr 16 '14 at 14:01
  • @saikiran Will it post email if the page is in local storage and that local computer is not a server? it is just a computer in the library in my university – rint Apr 16 '14 at 14:31
  • install wamp in your system and follow this tutorial http://roshanbh.com.np/2007/12/sending-e-mail-from-localhost-in-php-in-windows-environment.html – saikiran Apr 16 '14 at 14:45
  • I like this solution however is there a way to do this by ? – Sara Nikta Yousefi Mar 12 '15 at 12:58
  • @sara.y If you want to use an input, then you need to use a language like php in back-end to send your mail (which is obviously possible, but doesn't answer this specific question) – Gwenc37 Mar 12 '15 at 14:37
0

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.

tomodachi
  • 251
  • 3
  • 9
Shahid Hussain
  • 1,599
  • 1
  • 20
  • 24