简体   繁体   中英

Html Form, when submitted, send straight to email

I am using Dreamweaver to create a website, my website has a form, but when I click on the submit button it opens my outlook application, with the information I want. I was just wondering if it was possible to send the email to my account without going through my email client. This is my code...

BTW I am using Dreamweaver to edit all the code.

<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fiction Filming</title>
<link rel="shortcut icon" href="images/favicon3.ico" type="image/x-icon" />
<style type="text/css">
body {
    background-color: #2c2c2c;
}
</style>
<link href="Css/singlePageTemplate.css" rel="stylesheet" type="text/css">
<!--The following script tag downloads a font from the Adobe Edge Web Fonts server for use within the web page. We recommend that you do not modify it.-->
<script>var __adobewebfontsappname__="dreamweaver"</script>
<script src="http://use.edgefonts.net/source-sans-pro:n2:default.js" type="text/javascript"></script>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<!-- Main Container -->
<div class="container"> 
  <!-- Navigation -->
  <!-- Hero Section -->
  <!-- About Section -->
  <!-- Stats Gallery Section -->
  <div class="gallery"><img src="Images/Newbannercomingsoon.png" alt="" width="1000" height="500" class="logo-pic"/> </div>
  <!-- Parallax Section -->
  <!-- More Info Section -->
  <!-- Footer Section -->
  <section class="footer_banner" id="contact">

<form class="subscribeForm form-fix" name="Subscription Form" method="post" action="mailform.php" enctype="text/plain">
            <div class="newform">
              <div> </div>
              <div>
                <input id="fname" type="text" placeholder="NAME" name="name" required>
              </div>
              <div>
                <input name="email" type="email" required id="email" placeholder="EMAIL">
              </div>
              <div>
                <select name="select" required id="myselect">
                  <option>HELP / SUPPORT</option>
                  <option>BUSINESS ENQUIRES</option>
                  <option>OTHER</option>
                </select>
              </div>
              <div class="text-form">
                <div>
                  <textarea name="textarea" required id="textarea" placeholder="TYPE YOUR TEXT HERE"></textarea>
                </div>
              </div>
              <br><input name="Send" type="submit" id="Send" value="Send">
</div>
</form>
<!-- Step 1: Add an email field here -->

<!-- Step 2: Add an address field here -->
            <!-- Step 3: add a submit button here -->

</section>
  <!-- Copyrights Section -->
<div class="copyright">&copy;2016 - <strong>Fiction filming</strong></div>
</div>
<!-- Main Container Ends -->
</body>
</html>

Also, here is my php file... If you can fix it, it would be really helpful.

<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset="UTF-8">
<META HTTP-EQUIV="refresh" content="0;URL=thankyou.html">
<title>Email Form</title>
</head>

<body>
<?php
if(isset($_POST['submit'])){
    $to = "example@example.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $sender_name = $_POST['name'];
    $select = $_POST['select'];
    $message = $sender_name . " wrote the following:" . "\n\n" . $_POST['textarea'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>
</body>
</html>

HERE IS THE NEW PHP

    <!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset="UTF-8">
<META HTTP-EQUIV="refresh" content="3;URL=thankyou.html">
<title>Email Form</title>
</head>

<body>
<?php
if(isset($_POST['submit'])){

// Or the below if using "name="Send" for the input. Uncomment and get rid of the above
// if(isset($_POST['Send'])){


    if(!empty($_POST['email']) 
    && !empty($_POST['name']) 
    && !empty($_POST['select'])
    && !empty($_POST['textarea'])) {

    $to = "mail@fictionfilming.com";
    $from = $_POST['email'];
    $sender_name = $_POST['name'];
    $select = $_POST['select'];
    $textarea = $_POST['textarea'];

    $message = $sender_name . " wrote the following:" . "\n\n" . $textarea;

    if(mail($to,$subject,$message,$headers)){
    echo "Mail was sent. Check both your inbox and spam, as mail as done its job.";
}
else{
    echo "There was a problem. Check your logs.";
}

    }

}

   // $headers = "From:" . $from;
   // $headers2 = "From:" . $to;
    //echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    //}
?>
</body>
</html>

There are a few issues with your code.

Firstly, remove enctype="text/plain" . That type isn't valid to send POST arrays with a form.

You should also remove <META HTTP-EQUIV="refresh" content="0;URL=thankyou.html"> as that may play some nasty tricks on you.

Then your input <input name="Send" type="submit" id="Send" value="Send"> bears the Send name attribute and you're using the following conditional statement which will never fire up:

if(isset($_POST['submit']))

Either rename your input to name="submit" , or your conditional statement to read as if(isset($_POST['Send'])) ; the choice is yours.

Error reporting would have thrown you something about it.

Now, I don't see why you have this in your code $headers2 = "From:" . $to; $headers2 = "From:" . $to; since it's not being used anywhere, therefore I can't say much about this, other than just to get rid of it.

You should also check for empty fields, should someone decide not to fill any or all of the inputs, resulting in an empty email.

Ie, and I replaced your $_POST['textarea'] with $textarea while assigning it for the POST array.

Sidenote: See the above in regards to "submit" and "Send"; modify accordingly for the if(isset($_POST['submit'])) below here.

if(isset($_POST['submit'])){

// Or the below if using "name="Send" for the input. Uncomment and get rid of the above
// if(isset($_POST['Send'])){

    if(!empty($_POST['email']) 
    && !empty($_POST['name']) 
    && !empty($_POST['select'])
    && !empty($_POST['textarea'])) {

    $from = $_POST['email'];
    $sender_name = $_POST['name'];
    $select = $_POST['select'];
    $textarea = $_POST['textarea'];

    $message = $sender_name . " wrote the following:" . "\n\n" . $textarea;

    // Put your other mail code here

    }

}

All this assuming that mail() is available on the server you are running this from.

I suggest you replace:

mail($to,$subject,$message,$headers);

with a conditional statement, in order to check if it was sent:

if(mail($to,$subject,$message,$headers)){
    echo "Mail was sent. Check both your inbox and spam, as mail as done its job.";
}
else{
    echo "There was a problem. Check your logs.";
}

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


Footnotes:

I noticed you are using ID's, so if you're using JS/Ajax/jQuery that you didn't include in your original question, would be beyond the scope of the question.

It is unknown as to how you are accessing your files, so if you are on a hosted site, then what I have given you should work.

If it is a server issue, then that also is beyond the scope of the question and you will need to contact your service provider for tech support.

If you're using this from your own PC, then PHP, a webserver and mail() need to be installed, properly configured and using http://localhost/file.xxx as opposed to file:///file.xxx .

Sidenote: If nothing is passed from <option> , then you may have to give them values.

Ie: <option value="HELP / SUPPORT"> while doing that for the others.

I believe I have given you enough to move ahead here.

Since in the form tag u mentioned the action as action="mailto:mail@fictonfilming.com" it is trying to access outlook

change the action to the name of your php file action="yourphpfilename.php"

Depending on your server configuration, you may not be able to send mails using the simple php mail function.

To test it, use:

print mail($to,$subject,$message,$headers);

If the result is not 1, something is not good, and fixing it maybe not be easy.

I suggest you use the PHPmailer or API based services like MailGun.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM