问题描述
搜索结果"将显示一个表格,并且在每一行的末尾都有一个电子邮件按钮".访客单击一个电子邮件按钮",系统将一封电子邮件发送给该注册的用户".
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".
PHPMailer和邮件系统工作正常,但仅适用于静态电子邮件!
The PHPMailer and the mailing system is working fine, but ONLY with static email!
问题:我需要更改代码以将电子邮件发送给访客选择的单个电子邮件.我在$query
中添加了"WHERE"条件,但似乎没有影响.
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("[email protected]", "John Doe");
$mail->AddAddress("[email protected]");
以下行不正在工作
$mail->AddAddress("$email", "John Doe");
错误消息说:
<?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 = "[email protected]"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = "[email protected]";
$mail->FromName ="Company Name";
$mail->AddAddress($email, "John Doe");
$mail->AddReplyTo("[email protected]", "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
?>
推荐答案
这似乎是错误的
$query = "SELECT email FROM db_name WHERE email=$row[email]";
从哪里获得$ row [email]?您需要在其中有一个有效的变量-例如在post数组中(假设您正在从表单传递变量.类似
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 . "';
然后,您从返回的$ row中获取电子邮件(假设您正在使用返回查询中的该变量
Then you get the email out of the returned $row (assuming you are using that variable from the returned query
$email = $Row['email'];
然后,您可以使用$ email(因为它是变量而不是字符串,所以不带引号):
Then you can use $email (without quotes since it is a variable rather than a string):
$mail->AddAddress($email, "John Doe");
这篇关于通过PHPMailer将电子邮件发送到MySQL上的单个电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!