News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Can u explain this code, pls?

Started by Jotade29, November 27, 2022, 12:17:45 AM

Previous topic - Next topic

Jotade29

My version is 2.0.19
I need a little explanation of this piece of code, to be able to manipulate the one that sends the mail

$notifications = array();

if (!empty($row['email_address']) && ($row['pm_email_notify'] == 1 || ($row['pm_email_notify'] > 1 && (!empty($modSettings['enable_buddylist']) && $row['is_buddy']))) && $row['is_activated'] == 1)
$notifications[empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']][] = $row['email_address'];

foreach ($notifications as $lang => $notification_list)
{
// Make sure to use the right language.
loadLanguage('index+PersonalMessage', $lang, false);

// Replace the right things in the message strings.
$mailsubject = str_replace(array('SUBJECT', 'SENDER'), array($subject, un_htmlspecialchars($from['name'])), $txt['new_pm_subject']);
$mailmessage = str_replace(array('SUBJECT', 'MESSAGE', 'SENDER'), array($subject, $message, un_htmlspecialchars($from['name'])), $txt['pm_email']);
$mailmessage .= "\n\n" . $txt['instant_reply'] . ' ' . $scripturl . '?action=pm;sa=send;f=inbox;pmsg=' . $id_pm . ';quote;u=' . $from['id'];

// Off the notification email goes!
sendmail($notification_list, $mailsubject, $mailmessage, null, 'p' . $id_pm, false, 2, null);
}

This function is sendpm() in sub_post.

What the code does is that if you have the option "notify by mail if you receive a new private message" activated. I explain that row[email-adress] is from a sql query of the user's mail (the one who receives the mail)

I don't understand what the lang file has to do with that point one.
Finally, and most importantly, it is the last foreach, which I don't know what it has to show me ($notifications as $lang => $notification_list), I put print_r to the notification_list variable but it doesn't show me anything..., and that variable is the one that contains the email to which the notification is sent:

sendmail($notification_list, $mailsubject, $mailmessage, null, 'p' .$id_pm, false, 2, null);

If I change $notification_list in the sendmail function to my email, example: sendmail('[email protected], $mailsubject, $mailmessage, null, 'p' . $id_pm, false, 2, null);

  It sends the notification to me by mail, so I understand that this variable contains the mail, among other things, but what exactly is that variable returning to me? Because if I leave it as it is, that is, $notification_list doesn't get it, and I don't receive the email.

Thank you very much
Quote from: Diego Andrés on August 12, 2023, 02:20:18 AMI'm afraid convincing Jotade to upgrade to SMF 2.1 will require bigger effort than your work sanitizing Unicode characters  :laugh:

shawnb61

There is a good description of the sendmail() parameters at the top of Subs-Post.php.

That last chunk of code is attempting to avoid loading the language strings many times.  Earlier in the logic, the recipients were grouped by language.  So, for each language, it loads the language strings once ($lang), then sends it to that subset of recipients emails ($notification_list). 
Address the process rather than the outcome.  Then, the outcome becomes more likely.   - Fripp

Jotade29

Quote from: shawnb61 on November 27, 2022, 12:27:37 PMThere is a good description of the sendmail() parameters at the top of Subs-Post.php.

That last chunk of code is attempting to avoid loading the language strings many times.  Earlier in the logic, the recipients were grouped by language.  So, for each language, it loads the language strings once ($lang), then sends it to that subset of recipients emails ($notification_list). 

I appreciate the explanation. In the next part, assuming that my language is español_es, and $row['email_address'] is [email protected], which should show me the following code: $row['lngfile']][] = $row[ ' Email address'];
I don't understand the row[lngfile][] last two square brackets. Finally, how can I do a print_r there so that it shows me the content of that array. I do it but it doesn't show me anything
Quote from: Diego Andrés on August 12, 2023, 02:20:18 AMI'm afraid convincing Jotade to upgrade to SMF 2.1 will require bigger effort than your work sanitizing Unicode characters  :laugh:

Sesquipedalian

Quote from: Jotade29 on November 27, 2022, 09:30:45 PMI don't understand the row[lngfile][] last two square brackets.

To understand what's happening there, I suggest this.
I promise you nothing.

Sesqu... Sesqui... what?
Sesquipedalian, the best word in the English language.

mgcAna

Quote from: Jotade29 on November 27, 2022, 09:30:45 PMI don't understand the row[lngfile][] last two square brackets.

I assume you are still learning PHP, no problem with that. Just a note, SMF loves to embrace arrays, you need to be good in that. To make simple, last square bracket is saying that assign a value to current available key in array row['lngfile'].

Give few minutes to this link :
https://www.php.net/manual/en/language.types.array.php [nofollow]

Advertisement: