Sending email to multiple receipt

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
espo
Veteran Member
Posts: 98
Joined: 2013-03-01 12:55

Sending email to multiple receipt

Post by espo » 2020-10-08 09:32

Good morning,

I wanted to send an email to multiple recipients via hook, but it doesn't work.

function tablename_after_insert($data, $memberInfo, &$args){

// to compose a message containing the submitted data,
// we need to iterate through the $data array
foreach($data as $field => $value){
$messageData .= "$field: $value \n";
}

sendmail(array(
'to' => '[email protected], [email protected]',
'name' => 'Recipient Name',

'subject' => 'A new record needs your attention',

'message' => "The following new record was submitted by {$memberInfo['username']}: \n\n" . $messageData
));

return TRUE;
}


Can you help me understand how to solve?

Many thanks in advance.

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Sending email to multiple receipt

Post by jsetzer » 2020-10-08 09:35

Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

espo
Veteran Member
Posts: 98
Joined: 2013-03-01 12:55

Re: Sending email to multiple receipt

Post by espo » 2020-10-08 12:41

Thanks a lot jsetzer.
I had already seen the post but not being so experienced I did some tests without success.
Could you help me understand with an example how to send an email to multiple recipients?

Thanks a lot.

espo
Veteran Member
Posts: 98
Joined: 2013-03-01 12:55

Re: Sending email to multiple receipt

Post by espo » 2020-10-08 13:54

Hello,

I did some tests and with the following change, send to more recipients.
But I have a problem.
Send 17 emails. One for each string in the table.

Where am I wrong?

function myone_sped_after_update($data, $memberInfo, &$args) {
// to compose a message containing the submitted data,
// we need to iterate through the $data array
$receipt=array('[email protected]','[email protected]');


foreach($data as $field => $value){
$messageData .= "$field: $value \n";

foreach ($receipt as $address){


sendmail(array(
'to' => $address,
'name' => 'Recipient Name',

'subject' => 'Spedizione Modificata',

'message' => "{$memberInfo['username']} ha modificato una spedizione \n\n" . $messageData
));

}
}
return TRUE;
}

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Sending email to multiple receipt

Post by jsetzer » 2020-10-08 17:06

I think you have mixed up the two foreach-loops.

Create your message first. Then use foreach to send it to each recipient:

not tested code:

Code: Select all

function myone_sped_after_update($data, $memberInfo, &$args) {
  
  $receipt=array('[email protected]','[email protected]');
  
  // compose message
  foreach($data as $field => $value) 
  {
    $messageData .= "$field: $value \n";
  } // I have moved this bracket from the bottom up here
  
  // send to each recipient
  foreach ($receipt as $address)
  {
    sendmail(array(
      'to' => $address,
      'name' => 'Recipient Name',
      'subject' => 'Spedizione Modificata',
      'message' => "{$memberInfo['username']} ha modificato una spedizione \n\n" . $messageData
    ));
  }
  return TRUE;
}
PS: Please, when posting code, format it using code-tool [</>] in "Full Editor" mode for better readability
(app.php/help/bbcode#f2r1).
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

espo
Veteran Member
Posts: 98
Joined: 2013-03-01 12:55

Re: Sending email to multiple receipt

Post by espo » 2020-10-09 05:54

I solved, thanks a lot for the support.

Post Reply