如何通过php邮件发送电子邮件到从数据库中获取的地址

时间:2015-05-27 05:52:37

标签: php mysql

我创建了用户输入数据的页面&发送电子邮件。首先,我想将数据插入db&然后触发邮件到注册的电子邮件ID。现在我手动输入电子邮件ID。如何从数据库中获取电子邮件ID&发送。

    <?php 
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="testmra"; // Database name 

    // Connect to server and select databse.
    $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 

    mysqli_select_db($conn,$db_name);
    $sname=$_SESSION['usr_name'];


    $room = mysqli_real_escape_string($conn, $_POST['txtrname']);
    $name = mysqli_real_escape_string($conn, $_POST['txtname']);
    $dept = mysqli_real_escape_string($conn, $_POST['txtdept']);
    $purpose = mysqli_real_escape_string($conn, $_POST['txtpurpose']);
    $attendee = mysqli_real_escape_string($conn, $_POST['attendee']);
    $date = mysqli_real_escape_string($conn, $_POST['txtdate']);
    $btime = mysqli_real_escape_string($conn, $_POST['btime']);
    $etime = mysqli_real_escape_string($conn, $_POST['etime']);

    $sql="INSERT INTO bookingdetails (room,name,department,purpose,attendee,date,starttime,endtime,status_id)VALUES('$room','$name','$dept','$purpose','$attendee','$date','$btime','$etime','2')";

    $res  = mysqli_query($conn,"SELECT emailid FROM newuser WHERE username='$sname'");

    $row = mysqli_fetch_assoc($res);
    $to = $row["emailid"];

    require('phpmailer/PHPMailerAutoload.php');

    $mail = new PHPMailer();

    $subject = "Bookig Details";
    $content = "<b>Hello $name. Your Booking Details are as follow. Room : $room Date : $date Start Time : $btime End Time : $etime</b>";
    $mail->IsSMTP();
    $mail->SMTPDebug = 0;   
    $mail->SMTPAuth = TRUE; 
    $mail->SMTPSecure = "no";
    $mail->Port     = 26;  
    $mail->Username = "admin";
    $mail->Password = "@@@";
    $mail->Host     = "59.68.1.101";
    $mail->Mailer   = "smtp";
    $mail->SetFrom("admin@hitechplast.in", "Admin");
    $mail->AddReplyTo("admin@hitechplast.in", "Admin");
    $mail->AddAddress($to);
    $mail->Subject = $subject;
    $mail->WordWrap   = 80;
    $mail->MsgHTML($content);
    $mail->IsHTML(true);

    if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";

    if (mysqli_query($conn,$sql))
      {
        echo "Record added";

        }
    else
    {
        die('Error: ' . mysqli_error());
     }
?>

3 个答案:

答案 0 :(得分:2)

我认为问题在于会话开始。   你使用这个会话

$sname=$_SESSION['usr_name'];

你忘了开始会话

使用

session_start(); 

位于页面顶部

答案 1 :(得分:1)

$res  = mysqli_query($conn,"SELECT emailid FROM newuser WHERE username='$sname'") or die(mysqli_error($conn));
if($res && mysql_num_rows($res)>0)
{
    $data = mysql_fetch_assoc($res);
    $userEmail = $data['emailid']; // now this is your email id variable for user's email address.
    require('phpmailer/PHPMailerAutoload.php');

    $mail = new PHPMailer();

    $subject = "Bookig Details";
    $content = "<b>Hello $name. Your Booking Details are as follow. Room : $room Date : $date Start Time : $btime End Time : $etime</b>";
    $mail->IsSMTP();
    $mail->SMTPDebug = 0;   
    $mail->SMTPAuth = TRUE; 
    $mail->SMTPSecure = "no";
    $mail->Port     = 26;  
    $mail->Username = "meetingroom.admin";
    $mail->Password = "@@@";
    $mail->Host     = "59.68.1.101";
    $mail->Mailer   = "smtp";
    $mail->SetFrom("admin@hitechplast.in", "Admin");
    $mail->AddReplyTo("admin@hitechplast.in", "Admin");
    $mail->AddAddress($userEmail); // you can't pass php variables in single goutes like '$userEmail'. 
    $mail->Subject = $subject;
    $mail->WordWrap   = 80;
    $mail->MsgHTML($content);
    $mail->IsHTML(true);
}

试试这个。您的更改似乎正确。只有您在单引号中传递电子邮件变量$to。希望它有所帮助。

答案 2 :(得分:1)

您需要获取assoc数组,而不是数组。

$row = mysqli_fetch_assoc($res);