【问题标题】:Why can't I send an email to the email-address taken from the database? [duplicate]为什么我不能向从数据库中获取的电子邮件地址发送电子邮件? [复制]
【发布时间】:2023-03-23 05:14:01
【问题描述】:

我的网站允许用户向我的数据库中保存的给定电子邮件地址发送电子邮件。现在我的网站已经上线,我正在测试这样的东西,结果发现我的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>

【问题讨论】:

  • 很难说。可能还有更多原因。您是在个人计算机上还是在服务器上进行测试?因为您可能没有安装 SMTP 服务器,因此您无法发送邮件。
  • 我正在我的电脑中安装的 SMTP 服务器上进行测试。 @易卜拉欣
  • 我想我在您的代码中发现了一个错误。 php 邮件函数按以下顺序接受参数:收件人、主题、消息、标题。您的代码按以下顺序发送:to、message、headers。您省略了主题。
  • 您的查询是 $sql = "SELECT * from contact";这将选择所有行,这意味着您将获得一个多维数组作为来自数据库的响应。
  • 哦,好的。那么发送邮件需要包含主题吗? @易卜拉欣

标签: php forms email mysqli


【解决方案1】:

起初,我看到的是,$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);

【讨论】:

  • fn 和 ln 是我使用的第一个变量,我忘记更改了,谢谢指出,我会试试你的答案。谢谢你:)
  • 试过了,结果还是一样。我想我需要弄清楚如何在这种情况下使用正确的查询。无论如何,谢谢先生。
  • 为了解决这个问题,我建议首先确保从数据库中获取所有需要的信息。然后,使用标题和内容构建邮件。也许您在途中发现了问题。
猜你喜欢
  • 1970-01-01
  • 2014-07-20
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
  • 2017-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多