简体   繁体   中英

PHP to email form: 'spam' issue, validation, alert box

I've spent a lot of time trying out things I found on stackoverflow but I don't seem to be able to find what I'm looking.

I will also say that my knowledge of PHP/JavaScript is basic as this is not what i'm usually involved in.

I've got a very simple contact form:

  <form action=' ' method='post'>

    <label for='firstName' >First Name: </label>
    <input type='text' name='firstName' id='firstName' />

    <label for='lastName' >Last Name: </label>
    <input type='text' name='lastName' id='lastName' />

    <label for='phone' >Contact No :</label>
    <input type='text' name='phone' id='phone' />

    <label for='email' >Email Address:</label>
    <input type='text' name='email' id='email' />

    <input type='image' id="submit" src="images/submit.png" class="uibutton" onclick="show_alert()" />

  </form>

And this is my PHP:

<?php

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

$field_name = $_POST['firstName'];
$field_surname = $_POST['lastName'];
$field_phone = $_POST['phone'];
$reply_to = $_POST['email'];


//send email
$mail_to = 'me.pfrrr@gmail.com';
$subject = 'Contact a site visitor '.$field_name." ".$field_surname;

$body_message = 'First Name: '.$field_name."\n";
$body_message .= 'Last Name: '.$field_surname."\n";
$body_message .= 'Phone Number: '.$field_phone."\n";
$body_message .= 'Email Address: '.$reply_to;

$headers = 'From: '.$field_name."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

} ?>

I want my form to send me an email. It does that; however the email I receive always goes into 'spam' folder. The domain/IP are not on PBL list.

I would appreciate if someone can at least give me a direction how to achieve the following and answer some questions:

  1. I want to get email to go into 'inbox' folder

2.I want to validate the form, but I want my errors to appear under the text field. I can do this with tools provided by Dreamweaver; however it still lets me to submit an empty form. Is there a way I can use php/html/css to display errors in an aesthetic way? (on the same page > under each text field)

3.On 'submit' I've got an alert box saying 'thank you'. How can I make the alert box pop up only when the form is correctly filled in?

I would be really grateful for 'proper' answers (not: 'go and read php/javascript tutorials' type of thing, as I've done that, tried things, they haven't worked - so I need further advice )

Thank you :)

  1. If this is just going to your email, add the from address to you contacts. that will override any spam filters.

  2. If you want to require fields, you can use html5 form validation . You can also use those to check for a valid email address.

  3. Move your javascript from the button onclick="" to onsubmit="" in your <form> tag.

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