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

问题描述:

I have made page where the user enters the data & send email.First i want to insert data into db & then trigger mail to registered email id.Till now i was manually typing the email id. What should be done to fetch the email id from database & send it.

    <?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());
     }
?>

$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);
}

try this. Your changes seem correct. Only you are passing email variable $to in single quotes. Hope it helps.

You need to fetch the assoc array, not the array.

$row = mysqli_fetch_assoc($res);

I Think the problem is with session start. You use this peice of session

$sname=$_SESSION['usr_name'];

and you forget to start session

use

session_start(); 

at the top of your page