The "search result" brings up a table, and at the end of each row there is an "email-button". Visitor clicks one "email-button" and the system sends an email to that registered "user".
The PHPMailer and the mailing system is working fine, but ONLY with static email!
QUESTION: what do I need to change on the code to send email to the single email what the visitor chooses. I have added a "WHERE" condition to the $query
but it seems to have no impact.
MAILING FUNCTION WITH THE FOLLOWING TWO LINES IS WORKING
$mail->AddAddress("support@domain.co.uk", "John Doe");
$mail->AddAddress("admin@domain.co.uk");
WITH THE FOLLOWING LINE IS NOT WORKING
$mail->AddAddress("$email", "John Doe");
ERROR MESSAGE SAYS:
"Invalid address: Message could not be sent."
"Mailer Error: You must provide at least one recipient email address."
<?php
//mailer.php
$dbhost = 'xxx.xxx.xxx.xxx';
$dbuser = 'user_name';
$dbpass = 'password';
$dbtable = 'tablename';
$db = 'db_name';
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db) or
die("Could not connect: " . mysql_error());
mysqli_select_db($conn,$db);
if(isset($_POST['requestedEmail'])){
$requestedEmail = $_POST['requestedEmail'];
}
//Connecting to database
$link = mysqli_connect($dbhost, $dbuser, $dbpass, $db) OR DIE ("Unable to connect to database! Please try again later.");
mysqli_select_db($conn,$db);
//Fetching from database table.
$query = "SELECT email FROM db_name WHERE email='". $requestedEmail . "'";
if ($result)
{
while ($row = mysqli_fetch_array($result)) {
send_mail($row['email']);
}
}
//$email = $row['email'];
?>
<?php
require 'PHPMailer_5.2.0/class.phpmailer.php';
$email = $row['email'];
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "domainname.co.uk;domainname.co.uk"; // specify main and back`enter code here`up server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "support@domain.co.uk"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = "support@domain.co.uk";
$mail->FromName ="Company Name";
$mail->AddAddress($email, "John Doe");
$mail->AddReplyTo("info@domain.co.uk", "Information");
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject 3.4";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent to: " . $email; // $email is not printed out
?>