简体   繁体   English

联系人表格显示成功消息,但不发送电子邮件

[英]Contact form shows the success message but doesn't send email

I have created the below php script for the contact page of my website. 我已经为我的网站的联系页面创建了以下php脚本。 The problem with the script is that when a user fills and clicks the submit button, it show a success message but the email is not sent. 脚本的问题在于,当用户填写并单击“提交”按钮时,它会显示成功消息,但不会发送电子邮件。
Can someone please check and tell me where I went wrong. 有人可以检查一下并告诉我我哪里做错了。

<?php 
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']);
    }
}

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

    $emailTo = 'info@example.com';
    $subject = 'Submitted message from '.$name;
    $sendCopy = trim($_POST['sendCopy']);
    $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
    $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

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

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

<!-- @begin contact -->
<div id="contact" class="section">
    <div class="container content">

        <?php if(isset($emailSent) && $emailSent == true) { ?>
            <p class="info">Your email was sent. Sir!</p>
        <?php } else { ?>



            <div id="contact-form">
                <?php if(isset($hasError) || isset($captchaError) ) { ?>
                    <p class="alert">Error submitting the form</p>
                <?php } ?>

                <form id="contact-us" action="contacts.php" method="post">
            <div class="columns two contact_label alpha">
                Your Name 
            </div>
            <div class="columns five contact_input omega">
                <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="txt requiredField" placeholder="Name:" />
                        <?php if($nameError != '') { ?>
                            <br /><span class="error"><?php echo $nameError;?></span> 
                        <?php } ?>
                    </div>

                    <div class="clear"><!-- ClearFix --></div>
            <div class="columns two contact_label alpha">
                E-Mail Address
            </div>
            <div class="columns five contact_input omega">
                <input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="txt requiredField email" placeholder="Email:" />
                        <?php if($emailError != '') { ?>
                            <br /><span class="error"><?php echo $emailError;?></span>
                        <?php } ?>
                    </div>
            <div class="clear"><!-- ClearFix --></div>
                <div class="columns two contact_label alpha">
                Your Message
            </div>
            <div class="columns five contact_input omega">
                <textarea name="comments" id="commentsText" cols="45" rows="5" class="txtarea requiredField" placeholder="Message:"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
                        <?php if($commentError != '') { ?>
                            <br /><span class="error"><?php echo $commentError;?></span> 
                        <?php } ?>
                    </div>
            <div class="clear"><!-- ClearFix --></div>

                <div class="columns five contact_button alpha omega offset-by-two">
                <input type="submit" name="submitted" id="submitted" value="Send Email" />
            </div>
            <div class="clear"><!-- ClearFix --></div> 


                </form>         

             </div>
        <?php } ?>
    </div>
</div><!-- End #contact -->

You need to remove the line: 您需要删除以下行:

$emailSent = true;

And change the mail send line to 并将邮件发送行更改为

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

This sets $emailSent to true if the email is accepted for delivery. 如果接受电子邮件发送,则将$ emailSent设置为true。 PHP Mail PHP邮件

Instead of 代替

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

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

do

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

otherwise you'll be hardcoding the result, why would you say that the email has been sent if you don't know? 否则,您将对结果进行硬编码,如果您不知道,为什么还要说电子邮件已发送? what are you, some kind of liar? 你是什​​么骗子?

I guess, you should try to change the following: 我想,您应该尝试更改以下内容:

if(!isset($hasError)) {
    ...
}

condition. 条件。 You should type: 您应该输入:

if(!$hasError)
{
    ...
}

Ensure that your php mail smtp headers is properly configured. 确保正确配置了php邮件smtp标头。 below are sample code which works for me. 以下是对我有用的示例代码。 Copy and try it. 复制并尝试。

If the code below does not work for you then try to install Dropifi Contact Widget (www.dropifi.com). 如果下面的代码对您不起作用,请尝试安装Dropifi Contact Widget(www.dropifi.com)。 They have a very intuitive contact form which you can customize to meet your need. 他们有一个非常直观的联系表,您可以自定义满足您的需求。

require_once("class.phpmailer.php");
require_once("class.smtp.php");

  class Email extends PHPMailer
  {       
      public function __construct(){
          parent::__construct(true);
          $this->IsSMTP();
          $this->SMTPAuth = true;
          $this->SMTPSecure = "ssl";
          $this->Host = "smtp.gmail.com";
          $this->Port = 465;
          $this->Username = "someemail@gmail.com";
          $this->Password = "your password";
          $this->from = "no-reply@localhost.com";
          $this->FromName = "Webmaster";
          $this->WordWrap = 50;
          $this->IsHTML(true);
          $this->AddReplyTo("no-reply@localhost.com", "Webmaster");
          $this->Mailer = "smtp";
      }

      public function setReceiver($to){ 
          $this->AddAddress($to,"Administrator");
      }

      public function setSubject($subject){ 
          $this->Subject = $subject;
      }

      public function setMessage($param){

          $this->AltBody = "your alternative message";

          $message = "your main  message";
          $this->MsgHTML($message);
      }

      public function sendmail(){
          try{
              $flag = $this->Send();
              return $flag;
          }catch(phpmailerException $e){
              return $e->getMessage(); 
          }
      }
  }                             

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM