Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我不能向数据库中的电子邮件地址发送电子邮件?_Php_Forms_Email_Mysqli - Fatal编程技术网

Php 为什么我不能向数据库中的电子邮件地址发送电子邮件?

Php 为什么我不能向数据库中的电子邮件地址发送电子邮件?,php,forms,email,mysqli,Php,Forms,Email,Mysqli,我的网站允许用户向保存在我的数据库中的给定电子邮件地址发送电子邮件。现在我的网站已经上线了,我正在测试类似的东西,结果发现我的contact.php不起作用。我总是收到消息抱歉,发送您的消息时出错。当我试图发送电子邮件时 mycontact.php有以下代码 <?php error_reporting(E_ALL); ini_set('display_errors', 1); include 'dbcontroller.php'; if (isset($_POST['sendMessage

我的网站允许用户向保存在我的数据库中的给定电子邮件地址发送电子邮件。现在我的网站已经上线了,我正在测试类似的东西,结果发现我的contact.php不起作用。我总是收到消息抱歉,发送您的消息时出错。当我试图发送电子邮件时

mycontact.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>

首先,我可以看到,$fn和$ln没有填入任何地方,所以它们是空白值。这可能是一个原因。 我看到的另一件事是标题。我认为他们遗漏了一些最基本的东西。 以下是我经常使用的:

[狙击声]

$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);

很难说。可能还有更多的原因。您是在个人计算机上测试,还是在服务器上测试?因为您可能没有安装SMTP服务器,因此无法发送邮件。我正在电脑中安装的SMTP服务器上进行测试。@IbrahimI我想我发现了您代码中的错误。php邮件函数按以下顺序获取参数:to、subject、message、header。您的代码按以下顺序发送:收件人、消息、标题。您省略了主题。您的查询是$sql=SELECT*from contact;这将选择所有行,这意味着您将从数据库获得一个多维数组作为响应。哦,好的。所以在发送邮件时需要包含一个主题@Ibrahimfn和ln是我使用的第一个变量,我忘了更改它,谢谢你指出,我会尝试你的答案。谢谢:试过了,但结果还是一样。我想我需要弄清楚如何在这种情况下使用正确的查询。无论如何,谢谢,先生。为了解决这个问题,我建议您首先确保从数据库中获得所有需要的信息。然后,用标题和其他东西构建邮件。也许你会在路上发现问题。