简体   繁体   中英

PHP contact form returning sent but it isn't sending

I have tried to make sure others mistakes aren't here but I can't seem to fix this problem. When I run a test by entering a Name, email and message it returns the thanks for the message but never sends the email.

Hopefully this one isn't a stupid typo

HTML:

            <form method="post" action="contact.php" id="contactform">

                <div>
                <p>Send us your message</p>
                </div>

                <div>
                <label>Username <span class="required">*</span></label>
                <input name="name" type="text" id="name" value="" />
                </div>

                <div>
                <label>Email <span class="required">*</span></label>
                <input name="email" type="text" id="email" value="" />
                </div>

                <div>
                <label>Message <span class="required">*</span></label>
                <textarea name="message" rows="20" cols="50"  id="message" ></textarea><br /><br />
                </div>

                <div>
                <input type="submit"  value="Submit" class="button">
                <input type="reset" value="Reset" class="button">
                </div>

            </form>

PHP:

<?php
$field_name    = $_POST['name'];
$field_email   = $_POST['email'];
$field_message = $_POST['message'];

$mail_to = 'test@email.com';
$subject = 'Web message from '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

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

if ($mail_status) { ?>
  <script language="javascript" type="text/javascript">
    alert('Thank you for the message.');
    window.location = '/';
  </script>
<?php
}
else { ?>
  <script language="javascript" type="text/javascript">
    alert('Message failed.);
    window.location = '/';
  </script>
<?php
}
?>

please dont do things like that. You need to escape and clean the user input with something like mysql_real_escape_string and you can simply use

header('Location: http://example.org/');
exit();

For your question: In my opinion the mail function is not good to use in any given situation. Simply because there are many webhosting services that don't allow using it or with a limit. I´d use something like PHPMailer , which uses smtp for sending and retrieving email data. Keep in mind, that you need an email account for using libraries like that.

Manni

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