Post email component is sending emails multiple times

bryce alvord Posted in Component Development 1 month ago

I have created a component that sends an email to each user letting them know that a new post has been created. Side note: this is for a site with 11 users and thats it. The problem I am facing is that it sends emails to users multiple times in a pattern like this based on my foreach loop:

Email 1st user loop:
[email protected]

Email 2nd user loop:
[email protected]
[email protected]

Email 3rd user loop:
[email protected]
[email protected]
[email protected]

And so on where the email user loop in my code below is not for only that user guid, it is an inclusive list of all the previous emails + the new email from the next user guid in line if that makes sense. Here is my com code with my attempt at fixing said problem:

<?php
define("__POST_EMAIL__", ossn_route()->com . "PostEmail/");

function post_email() {
    ossn_register_callback("wall", "post:created", "postEmailHandler");
}

function postEmailHandler($callback, $type, $params) {
    $mail = new OssnMail;
    $mail->isHTML(true);
    $postuser = ossn_user_by_guid($params["poster_guid"]);
    $subject = "A new post has been created on myjournal.family";
    $message = $postuser->fullname . " has created a new post. " . ossn_site_url("post/view/") . $params["subject_guid"];

    $user = new OssnUser;
    $users = $user->getSiteUsers(["page_limit" => false]);
    $done = array();
    foreach ($users as $key => $value) {
        // send the message to all users except the posting user
        // error_log("NEW NOTI: " . ossn_dump($params));
        if ($value->guid != $params["poster_guid"]) {
            if(in_array($value->guid, $done, true) != true) {
                $mail->NotifiyUser($value->email, $subject, $message);
                array_push($done, $value->guid);
            }
        }
    }
}

ossn_register_callback("ossn", "init", "post_email");
Replies
German Arsalan Shah Replied 1 month ago

You have to initialize OssnMail in loop, you may see https://www.softlab24.com/product/view/78/notifications-via-email

German ~Z~ Man Replied 1 month ago

Honestly I don't understand the idea behind that $done array at all.
Did you dump $users, and some members appeared more than once in the result?
If so, we had a real issue in Ossn.

pk Malik Jokes Replied 1 month ago
us Bryce alvord Replied 1 month ago

This has to be programmatic because it wouldn't exactly be essentially email = list + newEmail every time someone posts. Should my $done array be loaded with the fullname of the user instead? Could the "strict" overload of true on the in_array method be causing it never to match? Is it a bug in the notify user code or possibly a misuse on my part? Ugh, frustrating