为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
哆啦的时光机 2023-10-15 15:23:40
我正在使用 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 connectionif ($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;};?>
查看完整描述

1 回答

?
九州编程

TA贡献1785条经验 获得超4个赞

像这样添加和标题


$header = array(

    'From' => 'webmaster@example.com',

    'Reply-To' => 'webmaster@example.com',

    'X-Mailer' => 'PHP/' . phpversion()

);

并输入您自己的信息


并在邮件功能中使用它


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


查看完整回答
反对 回复 2023-10-15
  • 1 回答
  • 0 关注
  • 50 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信