通过PHPMailer发送电子邮件到MySQL上的单个电子邮件地址

时间:2016-05-01 21:05:02

标签: php email phpmailer html-email

"搜索结果"打开一张桌子,在每一行的末尾都有一个"电子邮件按钮"。访问者点击一个"电子邮件按钮"并且系统向注册的"用户"。

发送电子邮件

PHPMailer和邮件系统工作正常,但只能使用静态电子邮件!

问题:我需要更改代码以将电子邮件发送到访问者选择的单个电子邮件。我添加了一个" WHERE"条件到$query,但似乎没有影响。

使用以下两条线的邮件功能

$mail->AddAddress("support@domain.co.uk", "John Doe");
$mail->AddAddress("admin@domain.co.uk");

以下行是工作

$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 = "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
?>

2 个答案:

答案 0 :(得分:0)

这似乎不对

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

从哪里获取$ row [email]?你需要在那里有一个有效的变量 - 例如从post数组(你正在从表单中传递变量)。类似的东西:

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

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

然后从返回的$行中获取电子邮件(假设您正在使用返回的查询中的该变量

$email = $Row['email'];

然后你可以使用$ email(没有引号,因为它是一个变量而不是一个字符串):

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

答案 1 :(得分:-1)

这一行:

getContentScrim().getAlpha()

应改为:

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

在PHP中将变量作为参数传递时,不要像传递字符串那样在变量周围使用引号。因此,在您的代码中,您没有传递变量$ email,而是传递字符串“$ email”。