Hi everyone,i have this mail notification whereby the receiver is selected from combobox. Upon combobox selection, the sender will click the submit button and the mail suppose to be sent to the receiver. But it seems that the combobox selection cannot be read.There is error message ie "Message could not be sent.Mailer Error: You must provide at least one recipient email address." Appreciate for your advise on this matter. Thanks a lot.

responsible.php

<html>
<head>
<title>Send Notification</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<form name="<?php echo $_SERVER['PHP_SELF']?>" method="post" action="ipdnotify2.php">
<?php

$cn=mysql_connect("localhost","pqa","") or die(mysql_error());
mysql_select_db("pq",$cn) or die(mysql_error());
$sql = "SELECT * FROM user";
$rs = mysql_query($sql) or die(mysql_error());
echo "<select>";


    while($row = mysql_fetch_array($rs)){


echo "<option value=".$row["Email"].">".$row["Position"]."</option>";

$_POST['Email']='".$row["Email"]."';
echo $email=$_POST['Email'];



    }


mysql_free_result($rs);
echo "</select>";
?>
<button type="submit" name="submit" value="Submit">Notify</button>
</form>
</body>
</html>

ipdnotify2.php

<?php
echo $_SESSION['Email'];
error_reporting(E_ALL);
$cn=mysql_connect("localhost","pqa","") or die(mysql_error());
mysql_select_db("pq",$cn) or die(mysql_error());
$sql = "SELECT Email FROM user where Userid>0 order by Username desc limit 1";
$rs = mysql_query($sql) or die(mysql_error());
if($row=mysql_fetch_array($rs))


    {


echo "Email:<input type='text' name='Email' value='".$row['Email']."'><br>";

mysql_free_result($rs);


    }  



if( isset($_POST["submit"]) )


     {



    echo $email=$row['Email'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;


    $mail->IsSMTP();                                      // Set mailer to use SMTP


$mail->Host = '2';   // Specify main and backup server


    //$mail->Port = 587;                                    // Set the SMTP port


$mail->SMTPAuth = false;                               // Enable SMTP authentication
//$mail->Username = '';                              // SMTP username
//$mail->Password = '';                              // SMTP password
//$mail->SMTPSecure = 'ssl';                            // Enable encryption, 'ssl' also accepted
 $mail->From = $_SESSION['Email'];
 $mail->FromName = $_SESSION['Username'];


    echo $mail->AddAddress('$email');                   // Add a recipient


$mail->IsHTML(true);                                  // Set email format to HTML
$mail->Subject = 'IPDS system Notification';


    $mail->Body    = 'Please do your <strong>Improvement Plan</strong>';


$mail->AltBody = 'Appreciate if you could process Improvement Plan. Thank you.';



    if(!$mail->Send()) {


   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;

   exit;


    }




    }


echo 'Message has been sent';
?>

Hello,

I see a couple of things:
You dont have an entry for who the mail is going to just who it is from:

$mail->From = $_SESSION['Email'];

And you are not authenticating with the mail server properly when trying to send. You have to have a legitimate user and password to send mail from most mail servers now a days and the entries in your script are commented out. You can't just send mail as if you were the person you want to receive it. Unless your a spammer and your IP will get blocked by the mail spam lists pretty quick then.
Also your mail server is named "2" which is odd unless you have that host predefined somewhere in your suystem.

>while($row = mysql_fetch_array($rs)){
>echo "<option value=".$row["Email"].">".$row["Position"]."</option>";
>
>$_POST['Email']='".$row["Email"]."';
>echo $email=$_POST['Email'];
>}

no idea what this is supposed to be doing

this however

echo $mail->AddAddress('$email'); `

assuming $email has a value
should read $mail->AddAddress($email);

Hi...thanks alot for your reply...really appreciate it..fyi the while($row=mysql_fetch_array($rs)){....} suppose to get the receiver email address from the database, which will be displayed as combobox. Please advise if this is the correct way to get the email address? Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.