I'm new to phpmailer and I'm trying to send an email to a group of users. I've searched on Google and used also the phpmailer reference guide and this is what i've created so far.
$oggetto_email = 'Kondo Manager: nuova comunicazione condominio';
// Creo il link per il login
$link_login = BASE_URL . '/index.php';
// Ottengo il tema html per il layout della email
$message = file_get_contents(BASE_URL .'/email-templates/client-new-group-post.html');
// Passo la variabile link a %linkripristino% che poi inserisco nel tema
$message = str_replace('%linklogin%', $link_login, $message);
$message = str_replace('%titolo%', $group_message_subject, $message);
$user_emails = mysqli_prepare($conn, "SELECT user_email FROM user_group_join LEFT OUTER JOIN users ON user_group_join . user_join_id = users . user_id WHERE group_join_id = ?");
mysqli_stmt_bind_param($user_emails, 'i', $group_id);
mysqli_stmt_execute($user_emails);
mysqli_stmt_store_result($user_emails);
mysqli_stmt_bind_result($user_emails, $tot_user_emails);
$tot_users_emails = mysqli_stmt_num_rows($user_emails);
// Inserisco le emails dentro ad un array
$emails = array();
if($tot_users_emails > 0){
while(mysqli_stmt_fetch($user_emails)){
$emails[] = $tot_user_emails;
}
}
foreach ($emails as $email) { //This iterator syntax only works in PHP 5.4+
// uso la funzione smtpmailer per inviare la mail passando i parametri richiesti
$send_mail = smtpmailer($email, $oggetto_email, $message);
// controllo la presenza di possibil errori nell'invio
if($send_mail === 'fail') {
echo "manage errors here, to be developed";
}
}
It seems to work fine but i've only tested with a group of three emails, i'm wondering if this is the right approach and if it will be still fine with a big group of emails 50-100. I believe it will take some time for the browser to load the page causing some issues to the final user. I read online I can put the emails in a queue and then send them later in background. I'm aware it can be done using RabbitMq but I'm thinking if I can use a mysql approach saving the messages into the database and then somehow running a background process to get all the emails from the database and process them using stmp phpmailer. If is it possible how can it be done? Also considering that the email will be the same for all the group's members do you think using addBcc will help to reduce the page loading time? Many thanks for your help