2017-04-08 113 views
0

數據庫中檢索多個電子郵件地址,所以我有過這樣的問題,我一直在試圖解決.. 我從數據庫中檢索客戶電子郵件列表,一旦我點擊進程,它假設發送一封電子郵件到「客戶的電子郵件」..下面的屏幕截圖描述了我的意思..發送電子郵件至從PHP

因此,一旦你按下第一行的「進程」按鈕,它應該發送一封電子郵件到檢索到的電子郵件在第一行 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(); 



} 
    ?> 

當$ mail-> addAddress('email @ example');有一個預定義值..但不知道如何使它「變量」,意思是它不斷根據從數據庫中檢索電子郵件列表改變..

回答

0

你的流程鏈接應該鏈接到網頁包含您的郵件的邏輯和通沿着要使用的電子郵件<a href="/sendmail.php?sendemail=<?php echo urlencode($row['BuyerEmail']); ?>">Process</a>

然後您的sendmail.php文件可以收到電子郵件$_GET['sendemail']並繼續您的郵寄邏輯$mail->addAddress(urldecode($_GET['sendemail']));

注意:希望你有一些類型的身份驗證,這不是一個公共可訪問的頁面,否則任何人都可以觸發此文件並將您的數據庫信息發送到他們選擇的電子郵件,或打這個文件一噸並導致系統崩潰。

0

更改爲以下幾點:

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. 

這是一些事情,你應該將其更改爲:

echo " 
<td> 
<form method='get' action='CHANGE IT TO YOUR PHP SCRIPT NAME'? 
<input type='hidden' name='sendemail' value='$row['BuyerEmail']'> 
<input type='submit' value='Process'> 
</form> 
</td> 
"; 

這裏我用一個簡單的HTML表單。我使用get方法傳遞一個隱藏值,即您的買方電子郵件。如果它是一個不同的文件,請將操作更改爲您的php腳本名稱。現在,你可以在你的電子郵件發送代碼作爲買家的電子郵件地址使用$ _GET [「sendemail」]。