Send email via PHPMailer to single email address o

2019-09-10 07:47发布

问题:

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
?>

回答1:

This seems wrong

$query = "SELECT email FROM db_name WHERE email=$row[email]";

where is it getting the $row[email] from? you need to have a valid variable in there - such as from the post array (assuminig you are passing the variable from a form. Something like:

if(isset($_POST['requestedEmail'])){
$requestedEmail = $_POST['requestedEmail']}
// other code

$query = "SELECT email FROM db_name WHERE email='". $requestedEmail . "';

Then you get the email out of the returned $row (assuming you are using that variable from the returned query

$email = $Row['email'];

Then you can use $email (without quotes since it is a variable rather than a string):

$mail->AddAddress($email, "John Doe");


回答2:

This line:

$mail->AddAddress("$email", "John Doe");

Should be changed to this:

$mail->AddAddress($email, "John Doe");

When passing a variable as a parameter in PHP, you don't use quotation marks around the variable like you would when passing a string. So in your code, you aren't passing the variable $email, you're passing the string "$email".