0

PHP の連絡先フォームを作成しましたが、メールが送信されない理由がわかりません。フォームは正常に送信されますが、実際のメールは送信されません。

以下は私のPHPコードです。

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

     //Check to make sure that the name field is not empty  
     if(trim($_POST['contactname']) == '') {  
         $hasError = true;  
     } else {  
        $name = trim($_POST['contactname']);  
     }  
     //Check to make sure that the subject field is not empty  
    if(trim($_POST['subject']) == '') {  
         $hasError = true;  
     } else {  
         $subject = trim($_POST['subject']);  
     }  

    //Check to make sure sure that a valid email address is submitted  
    if(trim($_POST['email']) == '')  {  
         $hasError = true;  
     } else if (!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/i", trim($_POST['email']))) {  
         $hasError = true;
     } else {  
         $email = trim($_POST['email']);  
     }  

    //Check to make sure comments were entered  
     if(trim($_POST['message']) == '') {  
         $hasError = true;  
    } else {  
        if(function_exists('stripslashes')) {  
             $comments = stripslashes(trim($_POST['message']));  
       } else {  
            $comments = trim($_POST['message']);  
        }  
     }  

    //If there is no error, send the email  
     if(!isset($hasError)) {  
        $emailTo = 'xyz@xyz.com';  
        $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";  
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;  

        mail($emailTo, $subject, $body, $headers);  
       $emailSent = true;  
 }  
}  
?> 

以下の HTML コードは、お問い合わせフォームの HTML です。

 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="contactform">
                <fieldset class="contact-fieldset">
                    <h2>Send us a message</h2>
                    <ul>
                        <li>
                            <label for="contactname">Your Name:</label>
                            <div class="ctinput-bg"><input type="text" name="contactname" id="contactname" value="" class="contact-input required" /></div>
                        </li>
                        <li>
                            <label for="email">Email:</label>
                            <div class="ctinput-bg"><input type="text" id="email" name="email" class="contact-input required email" /></div>
                        </li>     
                        <li>
                            <label for="subject">Subject:</label>
                            <div class="ctinput-bg"><input type="text" name="subject" id="subject" class="contact-input required" /></div>
                        </li>
                        <li>
                            <label for="message">Your message:</label>
                            <div class="cttxtarea-bg"><textarea rows="6" cols="40" id="message" name="message" class="contact-textarea required"></textarea></div>
                        </li>
                        <li>
                            <div class="form-button contact-submit">
                                <span><input type="submit" value="&nbsp;&nbsp;send message&nbsp;&nbsp;" name="submit" /></span>
                            </div>
                        </li>                      
                    </ul>
                </fieldset>
            </form>
4

4 に答える 4

1

他の人が提供したソリューションに加えて、SwiftMailer ライブラリを使用することを強くお勧めします。

自動メール機能を作成する必要があり、ヘッダーなどを手動で設定するのは悪夢でした。SwiftMailer のおかげで、かなりの時間を節約できました。例については、こちらを参照してください。

于 2012-09-11T13:14:43.557 に答える
0

私は2つのことを見ます:

1-コードの先頭で$hasError= falseの宣言(shureを存在させる)

2-次のようにヘッダーを生成します:

$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

これでうまくいくことを願っています、

于 2012-09-11T12:34:00.957 に答える
0

メールが実際に送信されているかどうかを確認してみてください。

if (mail($to, $subject, $body)) {
   echo("<p>Message successfully sent!</p>");
} else {
   echo("<p>Message delivery failed...</p>");
}

また、ヘッダーのvar_dumpを実行して、ヘッダーが正しいかどうかを確認することもできます。

var_dump($headers);

これらのことは問題を解決しませんが、確かにそれをデバッグするのに役立ちます。

于 2012-09-11T12:34:58.723 に答える
0

mail関数が正しく設定されていないようです。ファイルをチェックしphp.iniて、SMTP サーバーの設定が正しいことを確認してください。

また、元の質問とは関係ありませんが<ul>、フォームをマークアップするために使用することが意味的に正しいかどうかはわかりません。フィールドのセットは実際にはソートされていないリストではありません。意味的に正しい<table>、または一連の<div>s を使用することをお勧めします。

于 2012-09-11T12:30:48.190 に答える