def send_email_to_qualified_users(query_id, email_subject, email_body, email_intent, max_recipients):
 query_model = user_models.UserQueryModel.get(query_id)
 query_model.query_status = feconf.USER_QUERY_STATUS_ARCHIVED
 recipient_ids = query_model.user_ids
 if max_recipients:
  recipient_ids = recipient_ids[:max_recipients]
 bulk_email_model_id = email_manager.send_user_query_email(query_model.submitter_id, recipient_ids, email_subject, email_body, email_intent)
 query_model.sent_email_model_id = bulk_email_model_id
 query_model.put()
 for recipient_id in recipient_ids:
  recipient_bulk_email_model = user_models.UserBulkEmailsModel.get(recipient_id, strict=False)
  if (recipient_bulk_email_model is None):
   recipient_bulk_email_model = user_models.UserBulkEmailsModel(id=recipient_id, sent_email_model_ids=[])
  recipient_bulk_email_model.sent_email_model_ids.append(bulk_email_model_id)
  recipient_bulk_email_model.put()

def send_email_to_qualified_users ( query_id , email_subject , email_body , email_intent , max_recipients ) : query_model = user_models . UserQueryModel . get ( query_id ) query_model . query_status = feconf . USER_QUERY_STATUS_ARCHIVED recipient_ids = query_model . user_ids if max_recipients : recipient_ids = recipient_ids [ : max_recipients ] bulk_email_model_id = email_manager . send_user_query_email ( query_model . submitter_id , recipient_ids , email_subject , email_body , email_intent ) query_model . sent_email_model_id = bulk_email_model_id query_model . put ( ) for recipient_id in recipient_ids : recipient_bulk_email_model = user_models . UserBulkEmailsModel . get ( recipient_id , strict = False ) if ( recipient_bulk_email_model is None ) : recipient_bulk_email_model = user_models . UserBulkEmailsModel ( id = recipient_id , sent_email_model_ids = [ ] ) recipient_bulk_email_model . sent_email_model_ids . append ( bulk_email_model_id ) recipient_bulk_email_model . put ( )
send email to maximum max_recipients qualified users .