2016-04-16 19 views
-1

私のウェブサイトでは、データベースに保存されている電子メールアドレスに電子メールを送信できます。私のウェブサイトが生きているので、私はこのようなものをテストしています、そして、私のcontact.phpは動作していません。私は電子メールを送信しようとすると、常にSorry, there was an error sending your message.というメッセージが表示されます。データベースから取得した電子メールアドレスに電子メールを送信できないのはなぜですか?

私Contact.phpは、次のコード

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 
include 'dbcontroller.php'; 
if (isset($_POST['sendMessage'])) { 
    if(empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['message'])) { 
     echo "<script type='text/javascript'>alert('Please fill all the fields.');window.location.href='contact.php'</script>"; 
    } 
    else{ 
     $sql = "SELECT * from contact"; 
     $result = mysqli_query($conn, $sql); 
     $row = mysqli_fetch_array($result); 
     $to = $row['email']; 
     $firstname = $_POST['firstname']; 
     $lastname = $_POST['lastname']; 
     $mailfrom = $_POST['email']; 
     $message = $_POST['message']; 
     $headers = "From:" . $firstname . " " . $lastname . "\r\n" . $mailfrom; 
    if (mail($to, $message, $headers)) { 

    echo "<script type='text/javascript'>alert('Your email has been sent! Thank you for contacting us!');window.location.href='contact.php'</script>"; 
    } 
    else{ 
     echo "<script type='text/javascript'>alert('Sorry there was an error sending your message.');window.location.href='contact.php'</script>"; 
     } 
} 
} 
?> 

を持っており、これはPHPのコードと同じページにある私のコンタクトフォームです。

<form method="post" action""> 

    <label>First Name </label> 
<input type="text" class="form-control" placeholder="Firstname" name="firstname" required="required" /> 

    <label>Last Name</label> 
<input type="text" class="form-control" placeholder="Lastname" name="lastname" required="required"/> 

    <label>Email-address</label> 
<input type="email" class="form-control" placeholder="Email" name="email" required="required" maxlength="200"/> 

    <label>Message </label> 
<textarea name="message" class="form-control" required="required" rows="4" cols="50" maxlength="500"></textarea> 

    <input type="submit" name="sendMessage" value="Send Email" class="btn btn-primary pull-right"/> 
</form> 
+0

ハード[snipp]は言って: 次は、私がいつも使用しているものです。もっと理由があるかもしれません。あなたのパソコンやサーバーでテストしていますか? SMTPサーバーをインストールしていない可能性があり、メールを送信できない可能性があるためです。 – Ibrahim

+0

私は自分のPCにインストールされているSMTPサーバーでテストしています。 @Ibrahim – Felix

+0

私はあなたのコードでエラーを発見したと思います。 php mail関数はto、subject、message、headersの順にパラメータを取る。あなたのコードは、to、message、headersの順に送信されます。件名を省略しました。 – Ibrahim

答えて

0

最初は、$ fnと$ lnは空白の値であるため、$ fnと$ lnは埋められません。これは1つの原因になる可能性があります。 私が見る他のものはヘッダーです。私は彼らがいくつかの最小限のものを欠いていると思う。

$headers = "From: $botmail\n"; // Who the email was sent from 
$headers .= "MIME-Version: 1.0\n"; 
$headers .= "Content-Type: text/plain; charset=UTF-8\n"; 
$headers .= "X-Priority: $priority\n"; // The priority of the mail 

$success = mail($receivermail, $subject, $message, $headers); 
+0

fnを使用していますが、lnは私が使用した最初の変数でした。私はそれを指摘してくれてありがとう、私はあなたの答えを試してみます。ありがとうございます:) – Felix

+0

これを試しても結果は同じです。この状況で適切なクエリを使用する方法を理解する必要があると思います。とにかく教えてくれてありがとう。 – Felix

+0

問題を解決するには、まずデータベースから必要な情報をすべて取得することをお勧めします。次に、ヘッダーやものでメールを構築します。多分あなたは途中で問題を見つけるでしょう。 –

関連する問題