尝试使用 mail() 从数据库表发送记录

我正在使用 XAMPP,我想从数据库表中选择一个随机记录并通过电子邮件发送。我正在使用测试邮件服务器工具,但收到下一个错误:

警告:mail():第 24 行 C:\xampp\htdocs\dailyapp\sender2.php 中的错误消息返回路径 糟糕!出了点问题,我们无法发送您的消息。

这里可能发生什么?任何想法?

PHP代码:

<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "dapp";


// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);

}


$recipient = "mdrr5545@gmail.com";



$sql = "SELECT * FROM my_messages ORDER BY RAND() LIMIT 1";

$result = $conn->query($sql);


if ($result->num_rows > 0) {

    ($row = $result->fetch_assoc());

    $subject = $row["subject"];

    $message = $row["message"];

    

    if (mail($recipient, $subject, $message)) {

        http_response_code(200);

        echo "All good";

    }

    

    else {

        http_response_code(500);

        echo "Oops! Something went wrong and we couldn't send your message.";

        }

}


    else {

    echo 0;

};

?>


哆啦的时光机
浏览 78回答 1
1回答

九州编程

像这样添加和标题$header = array(&nbsp; &nbsp; 'From' => 'webmaster@example.com',&nbsp; &nbsp; 'Reply-To' => 'webmaster@example.com',&nbsp; &nbsp; 'X-Mailer' => 'PHP/' . phpversion());并输入您自己的信息并在邮件功能中使用它mail($recipient, $subject, $message,$header)&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP