2012-09-24 72 views
2

我目前正在研究一個php mysql基本系統,將從功能上覆選框從數據庫發送電子郵件給多個收件人。使用phpMailer發送電子郵件到多個收件人從數據庫使用複選框使用複選框到選定的電子郵件地址

如何使用檢查所有功能向那些收件人發送電子郵件?

這裏是我的PHPMailer的代碼和工作正常

<?php 
require("class.phpmailer.php"); 
include("class.smtp.php"); 
$mailer = new PHPMailer(); 
$mailer->IsSMTP(); 
$mailer->Host = 'ssl://smtp.gmail.com:465'; 
$mailer->SMTPAuth = TRUE; 
$mailer->Username = '[email protected]'; 
$mailer->Password = 'mypassword'; 
$mailer->From = '[email protected]'; 
$mailer->FromName = 'Admin'; 
$mailer->Body = 'TEST EMAIL'; 
$mailer->Subject = 'This is the subject of the email'; 
$mailer->AddAddress('[email protected]'); 
if(!$mailer->Send()) 
{ 
echo "Message was not sent<br/ >"; 
echo "Mailer Error: " . $mailer->ErrorInfo; 
} 
else 
{ 
echo "Message has been sent"; 
} 
?> 

我目前使用的系統中刪除數據庫複選框功能的用戶的另一個示例代碼:

<?php 
/*Check Box Commands*/ 
$id=$row_notification['user_id']; 

if(isset($_POST['Delete'])) { 
$checkbox = $_POST['checkbox']; 
mysql_select_db($database_connection_ched, $connection_ched); 
$id=$row_notification['user_id']; 

for($i=0;$i<count($checkbox);$i++) 
     { 
      $delete = "DELETE FROM tb_user WHERE user_id=$checkbox[$i]"; 
      mysql_query($delete,$connection_ched); 
     } 
$result2 = mysql_query($delete); 

    if($result2) 
    { 
    echo "<script language='javascript'>alert ('Successfully Deleted');</script>"; 
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=notification.php?\">"; } 
} 
/*End of Checkbox Commands*/ 
?> 
+1

如果您能夠檢索到顯示在複選框列表中的電子郵件地址列表,那麼您已經擁有幾乎所有您需要的東西。表單提交所選電子郵件地址的id(或其他),並將郵件發送給他們... –

回答

3

你可以使用你在那裏的刪除代碼的一些邏輯,但你也可以通過撥打一個電話來獲得你的信息來改善:

<?php 
require("class.phpmailer.php"); 
include("class.smtp.php"); 
$mailer = new PHPMailer(); 
$mailer->IsSMTP(); 
$mailer->Host = 'ssl://smtp.gmail.com:465'; 
$mailer->SMTPAuth = TRUE; 
$mailer->Username = '[email protected]'; 
$mailer->Password = 'mypassword'; 
$mailer->From = '[email protected]com'; 
$mailer->FromName = 'Admin'; 
$mailer->Body = 'TEST EMAIL'; 
$mailer->Subject = 'This is the subject of the email'; 

// get checkbox ids posted to us 
$checkbox_ids = $_POST["checkbox"]; 

// get email addresses from db based on checkboxes posted to us 
$query = "select email from tb_user where user_id in(".join(",",$checkbox_ids).")"; 

// connect to db and run query 
mysql_select_db($database_connection_ched, $connection_ched); 
$result = mysql_query($query); 
while($data = mysql_fetch_assoc($result)) 
{ 
    // make SURE we are not sending this to each and every recipient incrementally. 
    // alternately, you can use $mailer->ClearAllRecipients(); <-- this will clear cc and bcc as well 
    $mailer->ClearAddresses(); 
    // send it to THIS user... 
    $mailer->AddAddress($data["email"]); 


    print ($mailer->Send()) ? "Message sent to: " : "Message <b>not</b> sent to: "; 
    print $data["email"]."<br />\n"; 
} 

?> 
+0

非常感謝你,先生。它正在工作。 –

+0

還有一個問題。 如何合併這兩個對象(郵件程序和刪除功能),以便我可以在一個按鈕中調用這兩個函數。就像如果我點擊刪除或其他功能激活(具有類似的刪除邏輯),它也會發送電子郵件給收件人? –

+0

您可以傳遞點擊按鈕的名稱(例如「郵件」或「刪除」),然後在繼續操作之前檢查該值。 或 您可以使用另一個名爲「delete」的複選框,在對該用戶採取行動之前檢查$ _POST陣列中是否存在該複選框。 – rkstar

相關問題