-2

I have looked at so many tutorials but I still cant seem to get my contact form working. It is really getting me frustrated. MY HTML code for the contact form is :

form action="server.php" method="post">
    <p>
        <label for="name">Name:</label>
        <input name="name" id="name" type="text" class="required">
        <span>Please enter your name</span>
    </p>
    <p>
        <label for="email">Email:</label>
        <input name="email" id="email" type="text" class="required">
        <span>Please enter a valid email address</span>
    </p>
    <p>
        <label for="subject">Subject:</label>
        <input name="subject" id="subject" type="text">
        <span>Please enter your subject</span>
    </p>
    <p>
        <label for="message">Message</label>
        <textarea name="message" id="message" class="required"></textarea>
        <span>Please enter your message</span>
    </p>
    <p class="submit">
        <input type="submit" value="Submit" class="btn-submit">
    </p>
</form>

My PHP Code Is:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: F-Sharp Media'; 
$to = 'f.ajibade@f-sharpmedia.com'; 
$subject = 'Hello';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

mail( "khanyasser@live.co.uk" , "New Project" , $body);
header("Location:email_success.php");


if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
                echo '<p>Your message has been sent!</p>';
            } else { 
                echo '<p>Something went wrong, go back and try again!</p>'; 
            } 
        } else if ($_POST['submit'] && $human != '4') {
            echo '<p>You answered the anti-spam question incorrectly!</p>';
        }
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}
?>

This is the second php file :

<!doctype html>
<html class="no-js" lang="en">
    <head>
        <meta charset="utf-8" />
        <title>F-Sharp Media | Welcome</title>
        <link rel="stylesheet" href="css/foundation.css" />
        <link rel="stylesheet" href="css/main.css" />
        <script src="js/vendor/modernizr.js"></script>
    </head>

    <body>
        <h1> Email Sent</h1>
        <p>Your e-mail has been sent<p/>
        <p><a href="JavaScript:history.go(-1);">Back</a></p>
    </body>
</html>
GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59
Eddy
  • 11
  • 1
  • 8
  • You should add `<` before your form starts – Benjamin Aug 13 '14 at 10:43
  • Sometimes your web server may be running a little slow, it happened to me, and then all of a sudden I had tons of emails in my inbox, I'll take a quick look at my old code now and see if you've missed anything, it appears to look fine though. – Alex Aug 13 '14 at 10:49
  • @LinkinTED there's a call to "mail" function followed by the header function call. Nothing below the header() call will be executed. Are you aware of this? – PauloASilva Aug 13 '14 at 10:59
  • @PauloASilva, not my question, only edited it, for better readability. – GreyRoofPigeon Aug 13 '14 at 11:01
  • Does the mail function go at the end of the PHP file after the else statements. I Just want to know what I have done wrong. Found anything @Alex – Eddy Aug 13 '14 at 11:03
  • @user3932806 I'm just testing out some things with your code now, I'll let you know if I find the problem :) – Alex Aug 13 '14 at 11:07
  • I can't seem to spot any mistakes with your code, I've tested to make sure the information from the form is being obtained, which was positive, and then the mail function should ofcourse be straight forward and do it's job.. But it's appearing to do nothing, just a thought, are you testing this using XAMPP or a localhost? – Alex Aug 13 '14 at 11:24
  • I am using a localhost. I am uploading this using FTP and my domain name and host is from Go Daddy. May I have a look at your code solution . How about the order of my coding is that affecting anything @Alex – Eddy Aug 13 '14 at 11:27
  • Ahh okay, I've just got a load of delayed emails now, so I'm unsure which version is fixed, 2 mins, Ill post updated code that works for me once I've figured out what was causing the problem. – Alex Aug 13 '14 at 11:35
  • Ok Alex. Where will it be posted @Alex – Eddy Aug 13 '14 at 11:41
  • I've posted an answer before, just a temp solution, I'm not on my laptop at the minute, I'm at work so can't improve it ATM, see below :) I've tested it and it worked, but it has no validation etc, I don't finish work till 11, but I'll take another look later when I get chance to see what I can do :) – Alex Aug 13 '14 at 13:27
  • Thanks a lot Alex. Do let me know if you can find a solution. Its been bugging me a lot for the time being i will try finding out what is wrong with it. @Alex – Eddy Aug 13 '14 at 14:50
  • @user3932806 Np, I'm going to work on it now, see if I can get it more improved for you, try out what I've supplied though? See if it works your end – Alex Aug 13 '14 at 15:15
  • Okay I've finally solved it, give me 10 mins to sort the rest out and I'll upload the full answer :) – Alex Aug 13 '14 at 15:34
  • okay all sorted, it should work fully now, and I've validated the field inputs, so if the user doesn't enter one of the fields in the form, they'll get an error message, Also, I've fully updated it for your code, take a look, and let me know if it works. – Alex Aug 13 '14 at 15:46
  • Alex the code is fine when i press submit it does say messeage sent but i dont get an email at all ? @Alex – Eddy Aug 13 '14 at 17:51
  • Hmm, you should do? Maybe it's being blocked or something by your host? I tested it on xampp and my univerisities server, and it worked fine – Alex Aug 13 '14 at 20:28
  • Do you reckon I should download xampp and test it on there? Can I get your email or can you email me its on my profile – Eddy Aug 13 '14 at 20:31

4 Answers4

1

UPDATE:

Final answer (Fully tested and works with field validation)

Okay so, For your form page..

<form action="server.php" method="post">

    <h1> Contact us </h1>
    <label for="name">Name:</label>
    <span>Please enter your name</span>
    <p> <input type="text" name="name"> </p>

    <label for="email">Email:</label>
    <span>Please enter a valid email address</span>
    <p> <input type="text" name="email"> </p>

    <label for="subject">Subject:</label>
    <span>Please enter your subject</span>
    <p> <input type="text" name="subject"> </p>

    <label for="message">Message</label>
    <span>Please enter your message</span>
    <p> Message: </p> <p> <textarea rows="4" cols="50" name="message"> </textarea> </p>

    <p> <input type="submit" value="submit"> </p>

</form>

server.php page:

If you decide to change the name of this page, remember to update the name within the 'form action' section on the form on the first page

PHP:

$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message= $_POST["message"];

//Sending information to an email
$to = "khanyasser@live.co.uk";

$from = $email;
$headers = "From: " . $from;

if ($name != '' && $email != '' && $subject != '' && $message != '')
{
    mail($to,$subject,$message,$headers);
    echo "mail sent";
} else {
    echo "Please fill in all of the required fields";
}

If you have any issues with it, let me know, and I'll further assist you, but I think this should solve your problem.

Alex
  • 1,208
  • 16
  • 26
  • Alex Like you told me I have coped the code exactly but i still dont seem to be getting an email sent to me. Should I contact my hosting provider or do you have any other solutions. Is this something you have used that works. @Alex – Eddy Aug 14 '14 at 12:43
  • @user3932806 Yeah I used this exact code, but ofcourse just changing your email address to my own in the $to variable, and it worked, Maybe check your junk folder in your email? It sometimes gets sent to that, also I'm not sure what version of PHP you are using? Idk if you need a certain version for the mail function to work, other than that, I'm kind of dry of ideas now, Sorry – Alex Aug 14 '14 at 14:04
0

Your are doing very silly mistake , Actually you haven't put < at the starting of form. If you are getting any error in the php file than please post it so that we can help you. I didn't find any error in php but i found the error only in the html syntax. I think that your server is getting crushed under pressure, give your server some time and than check your email i am sure that you will get the message soon.

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38
  • That was corrected. I did a mistake when typing it up. The error is that it doesnt send to my e-mail which is the problem. Any useful links or tutorials that you would recommend @user3857918 – Eddy Aug 13 '14 at 10:43
  • I think your server is getting crushed under an heavy load. You will get the message soon on your email – Utkarsh Dixit Aug 13 '14 at 10:55
  • Seems to be given as comment. Not as an answer – Gunaseelan Aug 13 '14 at 11:14
  • @Gunaseelan user doesn't have enough reputation to comment – Rakesh Shetty Aug 13 '14 at 11:25
  • Ya i have not that much reputation to comment. Please don't give me negative marking @Gunaseelan – Utkarsh Dixit Aug 13 '14 at 11:32
  • Sorry friend. As per stackoverflow rules, I cannot remove down vote after 5 minutes. I can do only when you edit your answer which should give the answer to the question – Gunaseelan Aug 13 '14 at 11:35
  • I have edited the answer i think that's the answer to the question which was asked. Please remove the negative rating to my answer please... – Utkarsh Dixit Aug 13 '14 at 11:38
  • @Gunaseelan its ok bro. You can flag the answer as "It is not answer" and can you tell me did you also downvote my answer too? – Rakesh Shetty Aug 13 '14 at 11:40
0
<form action="server.php" method="post">
    <p> 
        <label for="name">Name:</label>
        <input name="name" id="name" type="text" class="required">
        <span>Please enter your name</span>
    </p>
    <p> 
        <label for="email">Email:</label>
        <input name="email" id="email" type="text" class="required">
        <span>Please enter a valid email address</span>
    </p>
    <p>
        <label for="subject">Subject:</label>
        <input name="subject" id="subject" type="text">
        <span>Please enter your subject</span>
    </p>
    <p>
        <label for="message">Message</label>
        <textarea name="message" id="message" class="required"></textarea>
        <span>Please enter your message</span>
    </p>
    <p class="submit">
        <input type="submit" value="Submit" class="btn-submit" name="send" />
    </p>
</form>

Your server.php script

<?php
define('FROM', 'F-Sharp Media');
define('RECIPIENT', 'f.ajibade@f-sharpmedia.com');
define('SUBJECT', 'Hello');

if ( ! isset($_POST['send']))
{
    header('Location: form.html');
    die;
}

$errors = array();

if ( ! isset($_POST['name']) || empty($_POST['name']))
    $errors[] = '"name" can not be empty';

if ( ! isset($_POST['email']) || empty($_POST['email']))
    $errors[] = '"email" can not be empty';

if ( ! isset($_POST['message']) || empty($_POST['email']))
    $errors[] = '"message" can not be empty';

if ( ! isset($_POST['human']) || ((int) $_POST['human'] !== 4))
    $errors[] = 'are you human?';

if (count($errors) > 1)
{
    foreach($errors as $error)
        echo $error,"\n";

    die('Aborting due to form errors');
}

$body =<<<MAIL
    From: {$_POST['name']}
    E-Mail: {$_POST['email']}
    Message:
        {$_POST['message']}
MAIL;

$headers = 'From: '.FROM;

if (mail(RECIPIENT, SUBJECT, $body, $headers))
    echo 'Email sent!';
else
    echo 'Failed sending email';

Pay attention that mail() requires some configurations on php.ini

You'll be better going with SwiftMailer, PHPMailer or other mail library

PauloASilva
  • 1,000
  • 1
  • 7
  • 19
-1

If you are trying to send a mail from your localhost then you have to see How to send a mail from localhost

OR

You can use SwiftMailer to send a mail. It is very simply and easy to use.

Also try to send a mail using SMTP authentication it may happen that to send a mail on your server it require authentication.

May be this will work for you try this :

require_once('swift/swift_required.php');

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: F-Sharp Media'; 
$to = 'f.ajibade@f-sharpmedia.com'; 
$subject = 'Hello';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

$transport = Swift_SmtpTransport::newInstance('SMTP_HOST', SMTP_PORT)
                  ->setUsername('SMTP_USERNAME')
                  ->setPassword('SMTP_PASSWORD')
                  ;

$mailer = Swift_Mailer::newInstance($transport);

    $message = Swift_Message::newInstance()
                       ->setSubject($subject) // Message subject
                       ->setTo(array($to => 'TO_NAME')) // Array of people to send to
                       ->setFrom(array($email => $name)) // From:
                       ->setBody($body , 'text/html');

if($mailer->send($message)){
    header("Location:email_success.php");
}
else{
    echo 'Some error occurred while sending mail';
    exit;
}
Community
  • 1
  • 1
Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79