0

I have some trouble with this, Anyone know why it won't work? I have this homepage where it uses this script for sending an email, but it won't work. When I call that I should get an email sent, but it just runs without any error.

<?php 
$emailTo = 'youremail';
$siteTitle = 'SiteTitle';

//error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP

//If the form is submitted
if(isset($_POST['submitted'])) {
    $hasError = false;
    // require a name from user
    if(trim($_POST['contactName']) === '') {
        $nameError =  'name plz!'; 
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }

    // need valid email
    if(trim($_POST['email']) === '')  {
        $emailError = 'Forgot Email?';
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
        $emailError = 'It's not right fool';
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    // we need at least some content
    if(trim($_POST['comments']) === '') {
        $commentError = 'Forgot something=';
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['comments']));
        } else {
            $comments = trim($_POST['comments']);
        }
    }

    // upon no failure errors let's email now!
    if(!isset($hasError)) {

        $subject = 'New message to '.$siteTitle.' from '.$name;
        $sendCopy = trim($_POST['sendCopy']);
        $body = "Name: $name \n\nEmail: $email \n\nMessage: $comments";
        $headers = 'From: ' .' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);

        //Autoresponse
        $respondSubject = 'Thank you for contacting '.$siteTitle;
        $respondBody = "Your message to $siteTitle has been delivered! \n\nWe will answer back as soon as possible.";
        $respondHeaders = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $emailTo;

        mail($email, $respondSubject, $respondBody, $respondHeaders);

        // set our boolean completion value to TRUE
        $emailSent = true;
    }
}
?>

3 Answers 3

3

Add \ in ', in all occurences

$emailError = 'It\'s not right fool';

instead of

$emailError = 'It's not right fool';
Sign up to request clarification or add additional context in comments.

Comments

0

You need to escape any 's with \ in strings that are enclosed in 's. For example, you need to change $emailError = 'It's not right fool' to $emailError = 'It\'s not right fool'.

The same goes for "s when enclosed in "s.

Comments

0

You said it runs without any error, so it may not be a PHP error. Have you made sure that SMTP is set up properly on your sever? With the new code you just posted, are there any new errors?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.