在MySQL中查询电子邮件地址并向该地址发送电子邮件

时间:2015-03-06 10:43:45

标签: php mysql email variables sendmail

我正在尝试从用户名为已知值的表中检索电子邮件地址。该值将从上一页传递,但我在此示例中将其设为常量。

我不知道如何给结果一个变量名称,并稍后为" To"地址。

感谢任何帮助。

<?php
    $servername = "xxxx";
    $username = "xxxx";
    $password = "xxxx";
    $dbname = "xxxx";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $SLT_YeerLead = "B.Baggins";

    $sql = "SELECT YeerLeadMail FROM t_staffyeerlead WHERE YeerLead='$SLT_YeerLead'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    echo "" . $row["YeerLeadMail"]. "";
    }
    } else {
    echo "0 results";
    }
    $conn->close();

    echo "<br><br>";

    $to = "??????????????";
    $subject = "Subject";
    $message = "blah blah blah<br><br>";
    $header = "From: administrator@mail.com \r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type: text/html\r\n";
    $retval = mail ($to,$subject,$message,$header);
    if( $retval == true )
    {
    echo "An E-Mail was sent to the Year Lead <b>$SLT_YeerLead</b><br><br>";
    }
    else
    {
    echo "An E-Mail could not be sent to the Year Lead <b>$SLT_YeerLead</b><br><br>";
    echo "<br><br><br><br><br><br>";
    echo "<b>Please contact ICT Support</b><br><br>";
    echo "Call XXXX<br><br>";
    echo "E-Mail: help@mail.com<br><br>";
    }

    mysql_close($con); ?>

由于

1 个答案:

答案 0 :(得分:0)

在while循环中声明$ email,因为你只有一行结果并将其分配给$ to或声明并将其分配给更干净的代码。

if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "" . $row["YeerLeadMail"]. ""; $email = $row["YeerLeadMail"]; }

$to = $email;

if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "" . $row["YeerLeadMail"]. ""; $to = $row["YeerLeadMail"]; }