0

私のウェブサイトの連絡先ページ用に以下のphpスクリプトを作成しました。スクリプトの問題は、ユーザーが送信ボタンを入力してクリックすると、成功メッセージが表示されますが、電子メールは送信されないことです。
誰かが私がどこで間違っていたのかを確認して教えてもらえますか?

<?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 -->
4

4 に答える 4

2

次の行を削除する必要があります。

$emailSent = true;

そして、メール送信行を次のように変更します

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

これにより、電子メールの配信が受け入れられた場合、$emailSent が true に設定されます。PHP メール

于 2013-02-23T21:16:28.860 に答える
1

それ以外の

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

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

行う

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

そうしないと、結果をハードコーディングすることになります。わからないのに、なぜメールが送信されたと言うのでしょうか? あなたは何ですか、ある種の嘘つきですか?

于 2013-02-23T21:17:09.623 に答える
0

私は、次のように変更しようとする必要があると思います。

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

調子。次のように入力する必要があります。

if(!$hasError)
{
    ...
}
于 2013-02-23T21:20:16.610 に答える
0

PHP メールの smtp ヘッダーが適切に構成されていることを確認してください。以下は私のために働くサンプルコードです。コピーして試してみてください。

以下のコードがうまくいかない場合は、Dropifi Contact Widget (www.dropifi.com) をインストールしてみてください。彼らはあなたのニーズに合わせてカスタマイズできる非常に直感的なお問い合わせフォームを持っています.

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(); 
          }
      }
  }                             
于 2013-02-24T20:46:46.807 に答える