Page 1 of 1

Sending email to multiple receipt with sendmail()

Posted: 2020-07-29 10:54
by epano
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

Re: Sending email to multiple receipt with sendmail()

Posted: 2020-07-29 11:13
by jsetzer
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

Re: Sending email to multiple receipt with sendmail()

Posted: 2020-07-29 12:02
by pbottcher
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

Re: Sending email to multiple receipt with sendmail()

Posted: 2020-07-30 22:30
by epano
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']
));
}
}

Re: Sending email to multiple receipt with sendmail()

Posted: 2020-08-02 17:36
by pfrumkin
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.

Re: Sending email to multiple receipt with sendmail()

Posted: 2021-02-08 22:09
by rpierce
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.

Re: Sending email to multiple receipt with sendmail()

Posted: 2023-02-01 18:49
by jsetzer
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);