联系表格不发送电子邮件到我的地址

时间:2014-03-19 14:38:28

标签: php contact-form

我的联系表单有问题,邮件未发送到我指定的电子邮件地址。谁能告诉我,如果我在这里做错了什么:

$emailTo = 'email@myemailwenthere.com';
$siteTitle = 'My sitename went here';

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

//If the form is submitted
if(isset($_POST['submitted'])) {

    // require a name from user
    if(trim($_POST['contactName']) === '') {
        $nameError =  'Forgot your name!'; 
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }

    // need valid email
    if(trim($_POST['email']) === '')  {
        $emailError = 'Forgot to enter in your e-mail address.';
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
        $emailError = 'You entered an invalid email address.';
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    // we need at least some content
    if(trim($_POST['comments']) === '') {
        $commentError = 'You forgot to enter a message!';
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['comments']));
        } else {
            $comments = trim($_POST['comments']);
        }
    }

    $subject = 'New message to ';
        $sendCopy = "";
        $body = "Name";
        $headers = 'From: ' .' <'.$email.'>';

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

    // 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);

        //Autorespond
        $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;
    }

2 个答案:

答案 0 :(得分:0)

尝试使用迷你脚本检查或发送邮件:

<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);
?>

答案 1 :(得分:0)

我建议你尝试配置SMTP的PHPMailer,它提供了很大的灵活性。

PHPMailer