Php电子邮件脚本失败

时间:2014-01-25 15:04:53

标签: php email

我遇到了一些麻烦,任何人都知道为什么它不起作用? 我有这个主页,它使用这个脚本发送电子邮件,但它不起作用。 当我打电话给我时,我应该收到一封电子邮件,但它只是运行没有任何错误。

<?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 个答案:

答案 0 :(得分:3)

在所有出现的\中添加'

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

而不是

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

答案 1 :(得分:0)

您需要在' s中包含的字符串中使用\转义任何'个。例如,您需要将$emailError = 'It's not right fool'更改为$emailError = 'It\'s not right fool'

" s中包含"时也是如此。

答案 2 :(得分:0)

你说它运行没有任何错误,所以它可能不是PHP错误。您是否确保在服务器上正确设置了SMTP?使用您刚刚发布的新代码,是否有任何新错误?