Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an email form that can send email using html

Tags:

html

I know there are a lot of examples using the mailto: post action to send emails using just html forms.

But using this will actually popup the send email dialog box e.g. outlook dialog box. And it actually uses our own smtp server to send the email.

Is there any way to create html forms that will simply send email upon submission?

Is there a javascript api which can achieve this effect? Say for example node.js?

Can anyone provide some code samples?

like image 440
dfdfd Avatar asked Nov 23 '11 09:11

dfdfd


People also ask

Can we send email from HTML?

HTML <a> tag provides you option to specify an email address to send an email. While using <a> tag as an email tag, you will use mailto: email address along with href attribute.


2 Answers

As the others said, you can't. You can find good examples of HTML-php forms on the web, here's a very useful link that combines HTML with javascript for validation and php for sending the email.

Please check the full article (includes zip example) in the source: http://www.html-form-guide.com/contact-form/php-email-contact-form.html

HTML:

    <form method="post" name="contact_form"     action="contact-form-handler.php">         Your Name:         <input type="text" name="name">         Email Address:         <input type="text" name="email">         Message:         <textarea name="message"></textarea>         <input type="submit" value="Submit">     </form> 

JS:

    <script language="JavaScript">     var frmvalidator  = new Validator("contactform");     frmvalidator.addValidation("name","req","Please provide your name");     frmvalidator.addValidation("email","req","Please provide your email");     frmvalidator.addValidation("email","email",       "Please enter a valid email address");     </script> 

PHP:

    <?php     $errors = '';     $myemail = '[email protected]';//<-----Put Your email address here.     if(empty($_POST['name'])  ||        empty($_POST['email']) ||        empty($_POST['message']))     {         $errors .= "\n Error: all fields are required";     }     $name = $_POST['name'];     $email_address = $_POST['email'];     $message = $_POST['message'];     if (!preg_match(     "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",     $email_address))     {         $errors .= "\n Error: Invalid email address";     }          if( empty($errors))     {     $to = $myemail;     $email_subject = "Contact form submission: $name";     $email_body = "You have received a new message. ".     " Here are the details:\n Name: $name \n ".     "Email: $email_address\n Message \n $message";     $headers = "From: $myemail\n";     $headers .= "Reply-To: $email_address";     mail($to,$email_subject,$email_body,$headers);     //redirect to the 'thank you' page     header('Location: contact-form-thank-you.html');     }     ?> 
like image 87
Yisela Avatar answered Oct 19 '22 02:10

Yisela


you can use Simple Contact Form in HTML with PHP mailer. It's easy to implement in you website.

Otherwise you can watch the demo video in following link: Youtube: Simple Contact/Feedback Form in HTML-PHP mailer

When you are running in localhost, you may get following error:

Warning: mail(): Failed to connect to mailserver at "smtp.gmail.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in S:\wamp\www\sample\mailTo.php on line 10

And this is the screenshot of HTML form: Simple Form

And this is the main PHP coding:

<?php if($_POST["submit"]) {     $recipient="[email protected]"; //Enter your mail address     $subject="Contact from Website"; //Subject      $sender=$_POST["name"];     $senderEmail=$_POST["email"];     $message=$_POST["comments"];     $mailBody="Name: $sender\nEmail Address: $senderEmail\n\nMessage: $message";     mail($recipient, $subject, $mailBody);     sleep(1);     header("Location:http://blog.antonyraphel.in/sample/"); // Set here redirect page or destination page } ?> 
like image 36
Antony Raphel Avatar answered Oct 19 '22 03:10

Antony Raphel