dqndc26628 2017-04-08 23:41
浏览 129

发送电子邮件到PHP中从数据库检索到的多个电子邮件地址

So i have had this issue which I’ve been trying to solve.. I have a list of customers emails retrieved from the database and once i click Process, it supposes to send an email to "the customer's email".. The screenshot below describes what i mean..

So once you press the "Process" button on the first row, it should send an email to the retrieved email in the first row image

    // The code below retrieves all the customer info from the database including customers email addresses. 
    <?php
while ($row = $result->fetch_assoc()){
    print "<tr>";
    print  "<td>" . $row['TransactionID'] . "</td>";
    print  "<td>" .$row['ItemName']."<br>" ."</td>";
    print  "<td>" .$row['ItemQTY']."<br>" ."</td>";
    print  "<td>" . $row['ItemAmount'] . "</td>";
    print  "<td>" . $row['BuyerEmail'] . "</td>";
    print "<td ><a href='#sendemail'>Process</a></td>"; //Once this link is clicked, it should take me to the next code send an email the retrieved email. 


    print  "</tr>";
}
$mysqli->close();


?>

//---------------------//

// The code below should trigger once i click "Process" and send an email to the customer. 
if (isset($_GET['sendemail'])){

require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();                                   
$mail->Host = 'smtp.gmail.com';                        
SMTP servers
$mail->SMTPAuth = true;                                       
authentication
$mail->Username = '****@gmail.com';          
$mail->Password = '********'; 
$mail->SMTPSecure = 'tls';                         
`ssl` also accepted
$mail->Port = 587;                                 

$mail->setFrom('****@gmail.com', 'RANDOMNAME');
$mail->addReplyTo('****@gmail.com', 'RANDOMNAME');
$mail->addAddress('BuyerEmail');  


$mail->isHTML(true); 

$bodyContent = '<h1>Our Valued Customer,</h1>';
$bodyContent .= '<p>Your Order is ready for pick up!</p>';

$mail->Subject = 'RANDOMNAME';
$mail->Body    = $bodyContent;

if(!$mail->send()) {
  echo 'Message could not be sent.';
  echo 'Mailer Error: ' . $mail->ErrorInfo;
}
runsendemail();



}
   ?>

The code works just fine when $mail->addAddress('email@example'); has a predefined value.. but not sure how to make it 'variable' meaning it keeps changing according to the list of emails retrieved from the database..

  • 写回答

2条回答 默认 最新

  • douzhang6646 2017-04-09 00:14
    关注

    Your process link should link to the page containing your mail logic and pass along the email to use <a href="/sendmail.php?sendemail=<?php echo urlencode($row['BuyerEmail']); ?>">Process</a>

    Your sendmail.php file then can receive the email with $_GET['sendemail'] and continue with your mailing logic $mail->addAddress(urldecode($_GET['sendemail']));.

    Note: Hopefully you have some kind of authentication in place and this isn't a publicly accessible page otherwise anybody can trigger this file and send your database information to an email of their choosing, or hit this file a ton of times and crash your system.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度