Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

How to create an email form that can send email using 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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 .= "
 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 .= "
 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:
 Name: $name 
 ".
    "Email: $email_address
 Message 
 $message";
    $headers = "From: $myemail
";
    $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');
    }
    ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...