Page 1 of 1

Sending email to multiple receipt

Posted: 2020-10-08 09:32
by espo
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.

Re: Sending email to multiple receipt

Posted: 2020-10-08 09:35
by jsetzer

Re: Sending email to multiple receipt

Posted: 2020-10-08 12:41
by espo
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.

Re: Sending email to multiple receipt

Posted: 2020-10-08 13:54
by espo
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;
}

Re: Sending email to multiple receipt

Posted: 2020-10-08 17:06
by jsetzer
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).

Re: Sending email to multiple receipt

Posted: 2020-10-09 05:54
by espo
I solved, thanks a lot for the support.