dsfo22654 2014-03-08 17:14
浏览 34

用PHP发送电子邮件,其中包含从数据库中获取的信

I want to pull an email from the database as the recipient. If I replace the line "SELECT student_user_email FROM students";' with an actual email address I get the desired outcome. However it would be handier to pull a particular email address from the 'students' table. Below is the current code. Wondering if anyone can help?

mail('"SELECT student_user_email FROM students";','Sample Form',$msg, 'From: johnsmith@gmail.com');
  • 写回答

2条回答 默认 最新

  • dtslobe4694 2014-03-08 17:21
    关注

    You can't send email to a multiple recipients in this way. Because query return array list so you must be looping and send email one by one recipients by this following way,

    Try this will work:

    <?php
        $con = mysqli_connect("localhost", "uname", "password");
        if (!$con){
          die('Could not connect: ' . mysqli_error());
        }
    
        $db_selected = mysqli_select_db("database",$con);
        $sql = "SELECT student_user_email FROM students";
        $result = mysqli_query($sql,$con);
    
        while ($row_data = mysqli_fetch_array($result)) {
    
            // Then you will set your variables for the e-mail using the data 
            // from the array.
    
            $from = 'you@yoursite.com';
            $to = $row_data['email']; // The column where your e-mail was stored.
            $subject = 'Sample Form';
            $msg = 'Hello world!';
            mail($to, $subject, $msg, $from);
        }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀