can we assign to one user 2 different mail contact?

can we assign to one user 2 different mail contact?

by Raja ba -
Number of replies: 26

hi,

I want to know if it is possible to assign at an user in moodle an general and alternate email adress on his contact in which we can contact him. I try to modify the validate_email function , so that when we create an user we can pass him 2 different email seperated by an comma but it doesn't work, that return me invalid syntax error.


smile

Average of ratings: -
In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by David North -

If all you want it for is for the user to be able to store an extra email address on their profile, you can add custom profile fields https://docs.moodle.org/29/en/User_profile_fields


In reply to David North

Re: can we assign to one user 2 different mail contact?

by Raja ba -

Thanks, i have already added the second adress to the user, but i don't know how to do in order that the user can receive information on his main adress and on his personal adress at the same time.

Please help smile

In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I don't think you'll find that's supported in core Moodle.  A user has an email address set, and that's the address they receive email to.  If you wrote a plugin, you could have it find the address in a custom field and send an email to that, but beyond that you're probably out of luck. 

You'd be better off ensuring that your users set their email address to the one they want to receive emails to.

In reply to Mark Johnson

Re: can we assign to one user 2 different mail contact?

by Raja ba -
do you know how can i do that at the level of my development code?
In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hi Raja,
I'm assuming you're asking about the first option I suggested, creating a plugin that gets the two email addresses from separate fields and sends an email to both.  I wasn't explicit, but I wouldn't actually recommend doing this.  All this would be is a form where you write the email and pick the user, then it gets the email address from the standard field and from your custom field and sends two emails.  If this is really what you'd like, there have got to be better tools to do that then writing a Moodle plugin.

I suspect what you're trying to achieve is making sure students receive emails that are sent from Moodle (notifications etc), and are hoping that sending to both addresses doubles the chance that they see it.  If this is the case, I'd suggest that your time is better spent communicating to students to either change the email address in their profile to their preferred one, or to indicate at registration which email address they'd prefer to be contacted on so you can set that in Moodle.
If this isn't the case, please share your motivation for wanting to send to two email addresses and we may be able to find a different solution that satisfies your need.
In reply to Mark Johnson

Re: can we assign to one user 2 different mail contact?

by Raja ba -

Hi ,

First thank you for your answer, yes i have added on my platform moodle and second field which is called personnal email in the general settings of the user, but i don't know how to do for when i send an message to an user that he can receive him on his personnal email he have edited and also at the university email that we have created him automatically. But for the moment my is he receive the message on his university address and not on her graping personel address. So do you know how can i edit my code to allow that.

Thanks

smile

In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Raja,

I'm not sure you've understood my meaning.  Moodle won't send a notification email to 2 different addresses, the only way to achieve this would be a horrible hack to the core code which I'm not even going to think about.  Even so, Moodle behaves this way with good reason - it's up to the user where (and if) they receive email notifications, so they should set their email address to the one they want to receive emails on, rather than the system sending emails to every possible address.

If there's a more complex reason why you want to send emails to two addresses (you need to archive them, or process them somehow), if you share the reason we may be able to help you via another method.

In reply to Mark Johnson

Re: can we assign to one user 2 different mail contact?

by Raja ba -

yes, it's mainly due to the fact that student doesn't frecuently consult their email address that's why i have created the personal email address but i don't know how to make that alternate, so that when we send a message to the user he can also receive a copy on this personnal address, he habitually use

Average of ratings: Useful (1)
In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Ok, if the user checks their personal email and not their university email, why not set the email field in their profile to their personal email rather than sending two emails?
In reply to Mark Johnson

Re: can we assign to one user 2 different mail contact?

by Raja ba -

Sure, that's what i want to do but the entreprise want that he receive that on his university and personnal email. So after, some research i found that moodle have into the profile settings an field name email which allow to send a copy of the message into the user platform and on his alternate email, but do you know how can i apply this same action to my personnal email address in the profile edit of the user?

In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I think I understand what you're saying, you'll have to forgive me if not.  I believe you're talking about the ability to specify an alternative email address in a user's messaging preferences, in which case messages are sent there instead of to their profile email, but you want it to sent to their profile email *and* their alternative email?

It may be possible to do this by creating a custom Messaging Output plugin.  Take a look at /message/output/email, you could create a new plugin based on that (say called customemail), and change the code of message_output_customemail.php around line 56 and line 93 to create a two recipient objects and send an email to both.

Note that you'll need to disable to core email output plugin when enabling yours, or they're recieve 3 emails.  You should also note that this would make your users' preferences misleading - the field in the messaging preferences screen says "Send email notifications to" "Leave empty to send notifications to xxx@yyy.zzz".  You might want to override the lang strings to change this to "Copy email notifications to" (or an equivalent translation) and make the second string blank.
In reply to Mark Johnson

Re: can we assign to one user 2 different mail contact?

by Raja ba -

yes i want that the message will be send on their alternate email address and on their email given to it automatically by the university at the same time, so what do you think of that?


In reply to Mark Johnson

Re: can we assign to one user 2 different mail contact?

by Raja ba -

i have created the custom email folder but you tell that i need to change the line to allow that so could you be more clear, because i haven't understand how to do that

In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Just so I know what level we're communicating at, what's your level of PHP experience? Have you done much Moodle development before?

In reply to Mark Johnson

Re: can we assign to one user 2 different mail contact?

by Raja ba -

No this is the first time, i just done development with php, my version of moodle actually is the 2.7

In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Ok, you might be jumping in the deep end a bit here if you've not developed with Moodle before.  You'll want to read some of the API documentation to understand what you're dealing with, in particular the Messaging API, Events API and Message Outputs (also here).  You should also read and try to understand the email_to_user() function that's used in the file you're trying to edit, since this does the business of actually sending emails from your system and you don't want to get that wrong!

If you read the comments in the message_output_customemail.php file you copied, you'll see that around line 56 it's creating the $recipient object based on whether the user has set an alternative email address or not.  You want to create 2 recipient objects, 1 for each address, and then call email_to_user() once for each.

In reply to Mark Johnson

Re: can we assign to one user 2 different mail contact?

by Raja ba -

Hi, 

Thanks, i have already read that documentation you give me, here is the message_output_customemail which i created, could you tell how can i test that without changing my sending message into the platform, could you also tell me if what i do seem correct to you.

Thanks 

In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Raja,

Not quite,  what you've done there is:

If the user has set a custom email, create $recipient1 and $recipient2 with the custom email.
else, create $recipient1 and $recipient2 with the regular email.

To do what you want, you need to do away with the if statement altogether, have $recipient1 use the custom email (using the code originally in the if block), and $recipient2 use the regular email (using the code originally in the else block).

I'm not sure what you mean by "could you tell how can i test that without changing my sending message into the platform".  Could you put it another way?

In reply to Mark Johnson

Re: can we assign to one user 2 different mail contact?

by Raja ba -

yes, i want to know after taking this change how can i test that inside the moodle platform.

So, could you please give me more details i haven't really understand the problem with this config file?

In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Raja,

To manually test it, you'd just set up an instance of Moodle with your custom messaging output plugin enabled, and a user that has both email addresses configured (using two addresses you have access to), then send them a message.  You could also look at writing automated tests for your code, in PHPUnit and Behat. It's a bit of a learning curve but it's worth it.

I think I've explained it in quite a bit of detail. If you don't understand what I've told you, I suggest you spend a bit of time understanding what the original file is doing by running it in a test environment like I described above, sending messages to a user with and without a second email to see how it works.  You can then try your modified version and see how that works differently, and hopefully then you should be able to see what I mean in my last message.

In reply to Mark Johnson

Re: can we assign to one user 2 different mail contact?

by Raja ba -

Hi,

i have edit the message_output_customemail, could you please check that and tell me what do you think about the change ?

I have to deliver it today night.

Thanks.


In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Raja,

If you have to 'deliver' today, then the most important factor is not whether someone else in the community is happy with the standard of your code (however expert and helpful they are, like Mark), but whether you have set up an environment and tested your code and that it works!

A code review is no substitute for proper testing.

Follow Mark's previous guidance and carry out some testing and ensure your code does what you expect it to, have debugging on to identify any errors and address them.

If it works (first priority), run it through the local_codechecker plugin to ensure it meets Moodle coding standards. OK there may be better ways to do some things sometimes, but if you have to deliver today, first priority has to be test it to make sure it does what it is supposed to. If it does, deliver it.

Average of ratings: Useful (2)
In reply to Richard Oelmann

Re: can we assign to one user 2 different mail contact?

by Raja ba -

Hi,

could you help me please, mark i have done the changing you advice me to do but it doesn't work, could you help me please.

Raja.

In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I think you either misread or misunderstood what I suggested, whereas before you had :

If the user has set a custom email, create $recipient1 and $recipient2 with the custom email.
else, create $recipient1 and $recipient2 with the regular email.

You now have:

Create $recipient1 and $recipient2 with the custom email.
Create $recipient1 and $recipient2 with the regular email.
And you want:
Create $recipient1 with the custom email.
Create $recipient2 with the regular email.
I'd suggest running your code with a debugger such as xdebug, or at the very least with some print_object() statements to output the $recipient1 and $recipient2 objects, that will help you better understand what the code is doing.  Without properly understanding the code you're trying to edit, you're unlikely to end up with something working or reliable.  You should also take seriously all of Richard's comments above.

In reply to Raja ba

Re: can we assign to one user 2 different mail contact?

by Olumuyiwa Taiwo -
Picture of Plugin developers

Will 'the enterprise' accept a solution in which students set their enterprise email such that any email coming from the Moodle site is copied to their personal email?

In reply to Olumuyiwa Taiwo

Re: can we assign to one user 2 different mail contact?

by Raja ba -
Hi,


First, you can find enclose some picture of my editing interface for an user, so as you can see on this picture, i want that when i grap an automatic email adress for an user that he can edit his profile when he want to communicate his pernnal email in the suitable field and after that he can receive the message of the university on his two affected email address.


Attachment mood1.PNG
Attachment mood2.PNG