Search code examples
phpmysqlemailmodx

Send email to users that checked 'yes' - php and modx


I have a database of 'user comments' that holds the comment, the userId, true/false for if they want to be emailed.

My issue is that if a user comments on a post 5 times, they are in that database 5 times and thus, they get 5 of the same emails. Here is my setup:

Find users in the database that want to be emailed about this comment

$ab = $modx->newQuery('Comments');
$ab->leftJoin('modUserProfile','Profile',array('Profile.internalKey = Comments.author'));
$ab->leftJoin('Bulletin','Bulletin',array('Bulletin.id = Comments.bulletin_id'));
$ab->where(array('Comments.bulletin_id' => $bullId));
$ab->where(array('Comments.email' => 1));
$ab->select(array('Comments.*','Profile.fullname as fullname' , 'Profile.email as email' , 'Bulletin.title as title'));
$emailList = $modx->getCollection('Comments',$ab);

Now, email them

foreach ($emailList as $e){
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->address('to',$e->email);
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_SUBJECT,'A new comment has been posted!');}

What should I use to email the users so that they only get 1 email regardless of the amount of comments they have made? Please let me know if the above is not clear enough. Thank you!!


Solution

  • I think you can add a groupby....

    $ab->groupby('email'); 
    

    after your select