This should get you your authors:
jimport( 'joomla.access.access' ); // import access
$authors = JAccess::getUsersByGroup(3); // get all users by group
Simply replace the argument (3) with your authors group id and you're good to go.
This will give you an indexed array of user ids like:
array(
'0' => 71
'1' => 75
'2' => 79
);
Therefore, you can easily iterate it in a foreach loop to grab the user's email like:
$authorEmails = array(); // To store emails
foreach($authors as $userId){ // Loop through all user ID's we just got
$email = JFactory::getUser($userId)->email; // Get the user email from JUser Object
if($email !== '') // If it's not an empty string
$authorEmails[] = $email; // Push the value into the array
}
This will result in another indexed array like
array(
'0' => "user71@domain.com"
'1' => "user75@domain.com"
'2' => "user79@domain.com"
);
So now you can easily access all of the author emails to pipe in to your email.
PUTTING IT ALL TOGETHER
The whole thing should look something like:
// Get the authors
jimport( 'joomla.access.access' ); // import JAccess
$authors = JAccess::getUsersByGroup(2); // replace the argument (3) with your authors group id
$app = JFactory::getApplication(); // Get application
// iterate $authors to get all authors user ID's
if(!empty($authors)){
// Init mail send
$mail = JFactory::getMailer(); // New mailer object instance
$config = JFactory::getConfig(); // get the config (don't know why because this is not referenced anywhere below)
$mail->isHTML(true); // set the mode to HTML
// iterate $authors to get all authors user ID's
foreach($authors as $userId){ // Loop through all user ID's we just got
$email = JFactory::getUser($userId)->email; // Get the user email from JUser Object
if($email !== '') // If it's not an empty string
$mail->addRecipient($email); // Add the authors email as a recipient
}// End foreach
// Continue setting email properties
$body = "Here is my text"; // Build the Message Body string
$mailSubject = "This is my subject"; // Build the Subject string
$mail->setBody($body); // Set body in the mail object
$mail->setSubject($mailSubject); // Set subject in the mail object
$mail->Encoding = 'base64'; // Set mail encoding
// Send email(s)
if($mail->Send()) // If success
$app->enqueueMessage('Mail sent'); // Output a success message
else // Otherwise,
$app->enqueueMessage( // Output a failure message
"An error occurred. Mail was not sent.", // Add Message
"error" // Set message type to error
);
}else{
$app->enqueueMessage( // Output a failure message
"Error: No recipients. Mail cancelled. ", // Add Message
"error" // Set message type to error
);
}
N.B. When you put this into production, it would be a good idea to remove the last else statement as you wouldn't really want to output this message to your users. (
else{
$app->enqueueMessage( // Output a failure message
"Error: No recipients. Mail cancelled. ", // Add Message
"error" // Set message type to error
);
}
) The same could also be said about all other messages too since there's no real reason to let your users know that an email has been sent/not sent if it is simply for the purpose of informing publishers that new content is waiting for them to publish.
Hope it helps,
Gez