Good way to change the sender email address

I guess it is failing here (not sure): https://github.com/pkp/pkp-lib/blob/master/classes/mail/SubmissionMailTemplate.inc.php#L67

But that is the only way I can think of using the hook here https://github.com/pkp/pkp-lib/blob/master/classes/mail/Mail.inc.php#L463

@asmecher any idea why using that hook to send mail with a plugin like useGlobalSender/UseGlobalSenderPlugin.inc.php at master · ajnyga/useGlobalSender · GitHub would stop the email logging from working? That can not be intended?

We have used a modified version of the core with basically the same changes since December and the email log does work for us, so probably the problem is with the hook (or the way the plugin is using it). I suggest that you switch the plugin off and wait for the changes to the core. As I mentioned above, I have not tested it much.

edit: I will add the same disclaimer to the plugin readme, but will leave the plugin there, because I think it is the only plugin available that shows how to use you own mailer code.

Ok so @asmecher if I am reading the code right the logging here https://github.com/pkp/pkp-lib/blob/master/classes/mail/SubmissionMailTemplate.inc.php#L68 only happens when the parent returns false. But the hook is now always set to return true https://github.com/pkp/pkp-lib/blob/master/classes/mail/Mail.inc.php#L463. So even the send function in my plugin returns false, it will be overridden by the hook which will always return true and this will lead to a situation where no email logging is happening?

So basically here is no way to use the hook here https://github.com/pkp/pkp-lib/blob/master/classes/mail/Mail.inc.php#L463 without losing email logging. Unless of course you add also the email logging code to the plugin?

Hi @ajnyga,

The way the hook return value is intended to work is to permit either augmentation of the existing behavior, or replacement of the existing behavior. If you return false from a hook callback, then any additional hooks are also called, and OJS permits the normal behavior of the system to continue (in this case, sending the email and logging the results). If you return true [ed: fixed!] from a hook callback, then OJS stops further hook processing, and where relevant, halting the normal handling of the action (sending and logging).

If you’re just looking to change the sender email address, you should (off the top of my head) be able to use the hook to reconfigure the SubmissionEmailTemplate object, then return false to permit normal execution (including logging) to continue, no?

Regards,
Alec Smecher
Public Knowledge Project Team

You wrote " If you return false from a hook callback" twice, which one is true?

The plugin above was just a quick test to solve the issue that is being fixed to the core anyway at some point. I’ve not used it in production but have something similar added to the core code at the moment, so I do not have that much interest in completing the plugin.

The reason I asked was just to find out how you can use the hook it uses and still have the emails being logged.

Hi @ajnyga,

Oops! Corrected my post.

Thanks,
Alec

Ok, so basically if the hook here https://github.com/pkp/pkp-lib/blob/master/classes/mail/Mail.inc.php#L463 is called, it returns true and the email logging is stopped? So basically any plugin that uses that hook to send emails should have it’s own method of logging the sent emails to ojs db? (that is probably missed by most users of that hook).

Hi @ajnyga,

As long as your hook callbacks return false, the call to HookRegistry::call will return false too. If one returns true, the rest will be skipped and the HookRegistry::call will return true.

Regards,
Alec Smecher
Public Knowledge Project Team

ok, but the plugin I made is returning false. But still the logs are not created?

see useGlobalSender/UseGlobalSenderPlugin.inc.php at master · ajnyga/useGlobalSender · GitHub

My understanding of this line here https://github.com/pkp/pkp-lib/blob/master/classes/mail/Mail.inc.php#L463 is that whenever that hook is called, that line will return true.

And if that returns true, then the log is not created https://github.com/pkp/pkp-lib/blob/master/classes/mail/SubmissionMailTemplate.inc.php#L68 or am I understanding this totally wrong :smiley:

The bottom line: I can not see how you could create a plugin that would use that hook and succesfully let OJS log the sent emails? edit: unless you add the logging code to the plugin itself, that is.

Hi @ajnyga,

Have a look at HookRegistry::call; I think the return value of that function isn’t what you’re expecting. It’s not success/failure, but more “do I continue working or do I stop”.

If it’s resulting in a true return, then that should suggest that some hook callback didn’t return false – or that something isn’t working properly. It’ll need some debugging.

Regards,
Alec Smecher
Public Knowledge Project Team

The users of the plugin above have been succesful in using the plugin to send the emails. The emails are just not being logged. And if the plugin has been successful in sending the emails, I can not see how it would return anything else than false: useGlobalSender/UseGlobalSenderPlugin.inc.php at master · ajnyga/useGlobalSender · GitHub

If the line https://github.com/pkp/pkp-lib/blob/master/classes/mail/Mail.inc.php#L463 would look something like if (HookRegistry::call('Mail::send', array($this, &$result))) return $result; I would understand how it could return the false from the plugin using the hook. But I just can not see how it currently returns anything else than true.

I mean something like this: https://github.com/pkp/pkp-lib/blob/master/classes/form/Form.inc.php#L171-L173. That I understand.

But yeah, I give up :grinning: @bog I suggest that you stop using the plugin. I am not sure how I could fix it to support email logging if that seems to be a problem and unfortunately have my hands full of other work so can not look into the issue that much at the moment.

hah just missed that one ! when reading the code in a hurry. Seemed so apparent now that I reopened it…

So the return values of almost the same send function just probably need to be reversed when called from a plugin: Update UseGlobalSenderPlugin.inc.php · ajnyga/useGlobalSender@51af788 · GitHub

Basically now, when a sendmail is succesful, the plugi callback should return false and thus return to the hook call in the mail.inc.php which has the conditional return true for the hook call. And the code should then go on to log the email.

Feel free to try the modified code @bog, but note that I did not test it

1 Like

Thanks @ajnyga, we will try it out! I will report how it worked.

Regards,
Bogi

Dear @ajnyga,

we have tested the above code but it did not work as expected. What happens is that the system sends the Global Sender email and the usual email as well, which is logged in the email_log. This is not what we wanted, unfortunately. We will use it this way because it is very important to have Global Sender, but we would extremely appreciate if you could show us how to have a log with Global Sender.

We have also seen that there are PHP warnings from the Global Sender plugin, what may also be important, like the ones below.

Thanks in advance for your help,
Bogi

[13-Jan-2019 05:56:01 Europe/Budapest] PHP Warning:  Illegal string
offset 'email' in
/var/www/html/cyberojs3114/plugins/generic/useGlobalSender/UseGlobalSenderPlugin.inc.php
on line 114
[13-Jan-2019 05:56:01 Europe/Budapest] PHP Warning:  Illegal string
offset 'email' in
/var/www/html/cyberojs3114/plugins/generic/useGlobalSender/UseGlobalSenderPlugin.inc.php
on line 114
[13-Jan-2019 05:56:01 Europe/Budapest] PHP Warning:  Illegal string
offset 'email' in
/var/www/html/cyberojs3114/plugins/generic/useGlobalSender/UseGlobalSenderPlugin.inc.php
on line 135
[13-Jan-2019 05:56:01 Europe/Budapest] PHP Warning:  Illegal string
offset 'name' in
/var/www/html/cyberojs3114/plugins/generic/useGlobalSender/UseGlobalSenderPlugin.inc.php
on line 135
[13-Jan-2019 05:56:01 Europe/Budapest] PHP Warning:  Illegal string
offset 'email' in
/var/www/html/cyberojs3114/plugins/generic/useGlobalSender/UseGlobalSenderPlugin.inc.php
on line 135

so what exactly is in the log now? You mean the global sender address is there?

No, in email_log we can see the log of the “normal” email, with the sender as without the Global Sender plugin. In other words, there are two emails sent, the Global Sender one, which is still not logged, and the original email, which is logged.

are you sure there are two emails sent? Or did you want the system to log the global sender address instead of the actual user address?

Yes, I’m completely sure. I can show you the headers of the two emails, the only difference is the sender, otherwise identical.

About the log, I do not mind which one is logged, either with the sender true email address or with the global one, both versions are fine.

Date: Mon, 14 Jan 2019 09:22:09 +0100
To: =?utf-8?Q?Bogl=C3=A1rka_G=2E-T=C3=B3th?= boglarka@inf.szte.hu
From: =?utf-8?Q?Bogl=C3=A1rka_G=2E-T=C3=B3th_via?= cybernetica@bibl.u-szeged.hu
Reply-To: =?utf-8?Q?Bogl=C3=A1rka_G=2E-T=C3=B3th?= boglarka@inf.szte.hu
Subject: [actcybern] Submission Acknowledgement
Message-ID: 880add9cbe1f70245ee9d77573ec1909@cyber.bibl.u-szeged.hu

Date: Mon, 14 Jan 2019 09:22:10 +0100
To: =?utf-8?Q?Bogl=C3=A1rka_G=2E-T=C3=B3th?= boglarka@inf.szte.hu
From: =?utf-8?Q?Bogl=C3=A1rka_G=2E-T=C3=B3th?= boglarka@inf.szte.hu
Subject: [actcybern] Submission Acknowledgement
Message-ID: 5f31e74c5659b8b6dffd4d2d54eb2a84@cyber.bibl.u-szeged.hu

and does this line here look the same in your installation: https://github.com/pkp/pkp-lib/blob/master/classes/mail/Mail.inc.php#L463 (return true).