Sending email to multiple receipt with sendmail()

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
epano
Posts: 19
Joined: 2020-07-09 11:56

Sending email to multiple receipt with sendmail()

Post by epano » 2020-07-29 10:54

Hello,

I have a problem on sending email notification to multiple receipt using sendmail() when a recod is updated.

The code below works great for sending email to one receipt:

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

if($data['alert'] != $GLOBALS['old_data']['alert'])

{
sendmail(array(
'to' => '[email protected]',
'name' => 'Contract Alert',
'subject' => 'Alert status changed for Contract ' . $data['id_kontrata'],
'message' =>'old data'. $GLOBALS['old_data']['alert'].'new data'.$data['alert']
));

}
return TRUE;
}

I have tried to to use multiple emails separating with comma like:
'to' => '[email protected], [email protected]', but it doesnt work.
Please any solution?
Regards

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 with sendmail()

Post by jsetzer » 2020-07-29 11:13

In AppGini's sendmail implementation there is a validation of "to" parameter based on a regular expression. If I'm right, this regEx-check returns FALSE for your comma-concatenated email addresses. It should return TRUE for a single (!) email address.

Code: Select all

function isEmail($email) {
  if(preg_match('/^([*+!.&#$�\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,45})$/i', $email)) {
    return $email;
  }
  return false;
}
This means for AppGini the given string (multiple email addresses) is NOT a valid (single) email address. Therefore the message will not be sent.

Alternatives as a starting point
  • iterate through the recipients (foreach) and send the same email to exactly one recipient per iteration
  • implement a custom sendmail function based on PHPMailer, which is already included in your sourcecode (./resources/PHPMailer/) by default
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

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Sending email to multiple receipt with sendmail()

Post by pbottcher » 2020-07-29 12:02

Hi,

maybe not exactly what you need, but you could also use the hooks/__global.php -> sendmail_handler

You would need to add possibly some logic if you send from different forms, but basically add

Code: Select all

$pm->addCC('MAILADDRESS', 'NAME');
That will send the mail in addition to MAILADDRESS
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

epano
Posts: 19
Joined: 2020-07-09 11:56

Re: Sending email to multiple receipt with sendmail()

Post by epano » 2020-07-30 22:30

Thank you both for your reply jsetzer and pböttcher,
I solved it with the code below by adding emails address on array and then sending email notification with foreach statement:

$receipt=array('[email protected]','[email protected]','[email protected]');

if($data['alert'] != $GLOBALS['old_data']['alert']){

foreach ($receipt as $address){

sendmail(array(
'to' => $address,
'name' => 'Contract Alert',
'subject' => 'Alert status changed for Contract ' . $data['id_kontrata'],
'message' =>'old data'. $GLOBALS['old_data']['alert'].'new data'.$data['alert']
));
}
}

pfrumkin
Veteran Member
Posts: 157
Joined: 2020-02-18 17:58
Location: Albuquerque, New Mexico USA

Re: Sending email to multiple receipt with sendmail()

Post by pfrumkin » 2020-08-02 17:36

I used the php mailer, comma delimiter worked. I would be fearful that iterating through a large list impacts performance on this page and adds extra work on your outbound email router.

Did you try a semi-colon? Seems like an AG bug if we can't send to a list.

rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Re: Sending email to multiple receipt with sendmail()

Post by rpierce » 2021-02-08 22:09

Would anyone be able to help me use this solution? I'm no coder!! Please help, I want to use sendmail also to send to multiple recipients.

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 with sendmail()

Post by jsetzer » 2023-02-01 18:49

I know, this is an old thread but today I had to solve this issue and wanted to find a solution right inside the calling method without using sendmail_handler in __global.php.

One idea came into my mind, honestly speaking, there is one backdraw, but maybe this idea can help others. With built in options of AppGini's sendmail function I did the following:
  1. Get the list of TO-recipients as an array like ["[email protected]","[email protected]"]
  2. Take the first array-entry as "TO"-recipient
  3. Take the rest and pass them as "CC"-recipients-array
AppGini's sendmail function accepts an array for CC but not for TO, unfortunately.

Backdraw
Only the first recipient will be addressed as "TO", all the others will be addresses in "CC".

Some code

Code: Select all

// prepare your $subject and $body contents
// get your recipients' email addresses as an array AND REVERSE IT
$recipients = array_reverse(["[email protected]","[email protected]","[email protected]"]);

// nothing to do
if (sizeof($recipients) < 1) return false;

$to = array_pop($recipients);
$cc = array_reverse($recipients);
$mail = [
    "to" => $to,
    "cc" => $cc,
    "subject" => $subject,    
    "message" => $body
];
$result = sendmail($mail);
if ($result !== true) die($result);
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

Post Reply