Отправка электронной почты с помощью phpMailer нескольким получателям из базы данных с помощью флажка на выбранный адрес электронной почты(es)



В настоящее время я работаю над базовой системой PHP mysql, которая будет отправлять электронную почту нескольким получателям из базы данных с функциональным флажком.



Как я могу отправить электронное письмо получателю, используя функцию проверить все функции?



Вот мой код 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";
}
?>


Еще один пример кода, который я сейчас использую в системе для удаления пользователя из базы данных с помощью функции checkbox:



<?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*/
?>
602   1  

1 ответ:

Вы можете использовать часть своей логики из кода удаления, который у вас есть, но вы можете улучшить и это, просто сделав один звонок в БД, чтобы получить информацию:

<?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';

// 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";
}

?>

Comments

    Ничего не найдено.