繁体   English   中英

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

[英]Trying to send records from database table with mail()

我正在使用 XAMPP 并且我想 select 从我的数据库表中随机记录并将其发送到 email 中。 我正在使用测试邮件服务器工具,但收到下一个错误:

警告:mail():第 24 行 Oops 上 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;
};
?>

像这样添加和 header

$header = array(
    'From' => 'webmaster@example.com',
    'Reply-To' => 'webmaster@example.com',
    'X-Mailer' => 'PHP/' . phpversion()
);

并输入您自己的信息

并在邮件中使用它 function

mail($recipient, $subject, $message,$header)  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM