从php网站接收邮件

时间:2016-03-17 13:03:43

标签: php forms email contact

我想问一下我应该怎么做才能收到我的联系表格中的电子邮件。 当我尝试发送消息时,它说我的消息已发送,但我从未收到它。 请帮帮我。

这是我的PHP代码。我做错了什么?

<?php 
  $myemail = '#######@yahoo.co.uk';
//set validation error flag as false
$error = false;
//check if form is submitted
if (isset($_POST['submit']))
{
    $name = trim($_POST['txt_name']);
    $fromemail = trim($_POST['txt_email']);
    $subject = trim($_POST['txt_subject']);
    $message = trim($_POST['txt_msg']);

    //name can contain only alpha characters and space and greek letters
    if (!preg_match("/^[a-zA-Zα-ωΑ-Ωίϊΐόάέύϋΰήώςτσπρφχψ ]+$/",$name))
    {
        $error = true;
        $name_error = "Plsease insert name";
    }
    if(!filter_var($fromemail,FILTER_VALIDATE_EMAIL))
    {
        $error = true;
        $fromemail_error = "wrong mail";
    }
    if(empty($subject))
    {
        $error = true;
        $subject_error = "empty sub";
    }
    if(empty($message))
    {
        $error = true;
        $message_error = "message???";
    }
    if (!$error)
    {
        //send mail
        $toemail = $myemail;
        $subject = "Enquiry from Visitor " . $name;
        $body = "Here goes your Message Details: \n\n Name: $name \n From: $fromemail \n Message: \n $message";
        $headers = "From: $fromemail\n";
        $headers .= "Reply-To: $fromemail";

        if (mail ($toemail, $subject, $body, $headers))
        {
            $alertmsg  = '<div class="alert alert-success text-center">Cheers m8 for contact!</div>';
        }
            else
            {
            $alertmsg = '<div class="alert alert-danger text-center">something went wrong</div>';
            }
    }
}
?>

我的表格

<div class="container">
 <?php if (isset($alertmsg)) { echo $alertmsg; } ?>
<div class="row">
    <div class="col-md-6 col-md-offset-3 well">
        <form role="form" class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contactform">
        <fieldset>
            <legend>Φόρμα επικοινωνίας</legend>

            <div class="form-group">
                <div class="col-md-12">
                    <label for="txt_name" class="control-label">Όνομα</label>
                </div>
                <div class="col-md-12">
                    <input class="form-control" name="txt_name" placeholder="Όνομα" type="text" value="<?php if($error) echo $name; ?>" />
                    <span class="text-danger"><?php if (isset($name_error)) echo $name_error; ?></span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12">
                    <label for="txt_email" class="control-label">Email </label>
                </div>
                <div class="col-md-12">
                    <input class="form-control" name="txt_email" placeholder="Εισάγεται το email σας" type="text" value="<?php if($error) echo $fromemail; ?>" />
                    <span class="text-danger"><?php if (isset($fromemail_error)) echo $fromemail_error; ?></span> 
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12">
                    <label for="txt_subject" class="control-label">Θέμα</label>
                </div>
                <div class="col-md-12">
                    <input class="form-control" name="txt_subject" placeholder="Θέμα" type="text" value="<?php if($error) echo $subject; ?>" />
                    <span class="text-danger"><?php if (isset($subject_error)) echo $subject_error; ?></span>
                </div>
            </div>

1 个答案:

答案 0 :(得分:-1)

也许你必须先安装postfix(如果你的网站使用linux作为环境)。