Trying to send Email to a group of user's on selection by user .

Permalink
I have created a page attribute which is a multiple select list where the page creator gets to choose to which group he wants send mail to .

Call to a member function getGroupMembers() on a non-object ---> this is the error

Here is my code

$list =(string)$page->getAttribute('Groups whom email shd be sent');--->attribute---->value-->("Administrators Editors Publishers")
$grps = preg_split('/\s+/',$list);
foreach ($grps as $grp)
{
$group =Group::getByName($grp); /*getByName function is not accepting dynamic values instead of $grp if i give "Administrators" it works */
$users = array_unique($group->getGroupMembers());
foreach ($users as $member) {
$list.=$member->getUserEmail().',';
$mh->cc($member->getUserEmail());
}

 
hutman replied on at Permalink Reply
hutman
Have you tried to var_dump your $grp variable? Sometimes when you have a multi-select attribute the values are stored with a "\n" separator and that would cause the group to not be found by the getByName because it wouldn't be an exact text match.
mohamedkhalid replied on at Permalink Reply
@hutman tried removing all the spaces and new lines but i still am getting the error

Call to a member function getGroupMembers() on a non-object

I am getting the groups chosen by user into an array but when i try getting the group members using foreach i get the error
hutman replied on at Permalink Reply
hutman
If you do a var_dump on $group after the getByName, do you have an object or a null? If it's a null there is still something wrong with your $grp where it doesn't have an exact text match to the Group Name entered in the Dashboard.
mohamedkhalid replied on at Permalink Reply
i am getting the value of array grps as
Array ( [0] => Administrators [1] => Publishers [2] => Editors )

when i try
foreach($grps as $grp)
{
$group =Group::getByName($grp); ----> something going wrong here. it works fine when $grps is Array ( [0] => Administrators )
$group =var_dump($group);
$users =$group->getGroupMembers();
foreach ($users as $member) {
$list.=$member->getUserEmail().',';
$mh->cc($member->getUserEmail());
}
}
tried this but it shows same error
hutman replied on at Permalink Reply
hutman
What did this line give you - var_dump($group);? Also, you need to not try to assign that to a variable, it's just dumping values.
hutman replied on at Permalink Reply
hutman
Can you take a screenshot or copy/paste exactly what comes out when you do this

var_dump($grp);
echo '<br /><br />';
foreach($grps as $grp){
   var_dump($grp);
   echo '<br /><br />';
   $group = Group::getByName($grp);
   var_dump($group);
   echo '<br /><br />';
   /*
   $users = $group->getGroupMembers();
   foreach ($users as $member) {
      $list .= $member->getUserEmail().',';
      $mh->cc($member->getUserEmail());
   }
   */


I don't see anywhere in your code where you are cleaning up the newline characters and I still think that's what is causing your issues here.
mohamedkhalid replied on at Permalink Reply 1 Attachment
Please refer the screenshot for the result what i got when placing the code given by you .
The Problem is only the first time it runs through the foreach loop we get the result for this line .

$group = Group::getByName($grp);
hutman replied on at Permalink Reply
hutman
I know this is a silly question but, you do have two User Groups in your system called "Publishers" and "Editors" right? Same capitalization and no end spaces or anything
mohamedkhalid replied on at Permalink Reply 1 Attachment
You can see the groups i have in the screenshot attached below
hutman replied on at Permalink Best Answer Reply
hutman
In this screenshot your group is called "Editor" but in your previous array your group is called "Editors" those do not match and that's why it's not finding that group.
mohamedkhalid replied on at Permalink Reply
Oh thats such a silly mistake from my side . Thanks a lot for noticing and helping me out with this thing . You helped a lot.