【问题标题】:Trying to send records from database table with mail()尝试使用 mail() 从数据库表发送记录
【发布时间】:2020-12-06 02:35:37
【问题描述】:

我正在使用 XAMPP,我想从我的数据库表中选择一条随机记录并通过电子邮件发送。我正在使用Test Mail Server Tool,但收到下一个错误:

警告: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;
};
?>

【问题讨论】:

    标签: php mysql email


    【解决方案1】:

    像这样添加和标题

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

    并输入您自己的信息

    并在邮件功能中使用它

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

    【讨论】:

    • 我只是想知道为什么这是必要的?如果您能稍微解释一下,我将不胜感激......
    • 需要设置repliy-to等字段并且不能为空,这是标准的。请不要忘记accept the answer
    猜你喜欢
    • 2015-09-16
    • 2014-02-24
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2017-12-14
    相关资源
    最近更新 更多