Reputation: 975
I write one Script for add members to a Google Group. The data is write into my website, saved into Google Sheets and this sheet launch the script for add the member to the group.
The code is working fine, but only in some cases. Generally, the adresses with @hotmail
are rejected with this message:
errorException: The email is invalid
I think there is one problem with @hotmail and another adresses. I migrated one list from Mailchimp to Google Groups with the same process, and multiple email adresses from @hotmail were rejected.
These email adresses not are invalid, the reports at Mailchimp indicated that the messages to those addresses were delivered.
In the past week alone, about five @hotmail addresses were rejected. They were real people trying to subscribe to my list, including people I have had communication with using those @hotmail email addresses. So I'm sure there is a problem in Google Workspace regarding @hotmail addresses. The same happens with a lot of addresses with @outlook, and with adresses of some custom domains.
How I can solve this.
The code I'm using is here:
function addUsertoGroup(userEmail) {
var userEmail = userEmail.toLowerCase();
var groupId = "[email protected]";
var group = GroupsApp.getGroupByEmail(groupId);
try {
var hasMember = group.hasUser(userEmail);
if (!hasMember){
var newMember = {email: userEmail, role: "MEMBER"};
var mStatus=AdminDirectory.Members.insert(newMember, groupId);
Logger.log(" inserted: "+mStatus.email+"\n");
return true;
}else{
Logger.log(userEmail+" exists\n");
return false;
}
} catch(e) {
Logger.log(userEmail + " error"+e+"\n");
return false;
}
}
This is one error example in the Log:
I asked into Issue Tracker without success.
Upvotes: 1
Views: 223
Reputation: 975
I found the solution to this problem thanks to this answer.
It seems that when the getFrom()
method is used on a card, the name appears but not the email address, that is, something like this: Firstname Lastname <[email protected]>
. It was for this reason that in some cases it gave an error, since instead of [email protected]
it took Firstname Lastname
as email address.
The solution, while they fix it in the Gmail API1, is to look for the correct address, which would be found between the <
and >
characters.
By putting the functions like this, the error has disappeared:
function triggerForEmail() {
const threads = GmailApp.search('is:unread AND {label:"Confirmado" subject:"Re: Suscripción al boletín semanal" subject:"Confirmando"}');
let newMembers = []
for (const thread of threads) {
const messages = thread.getMessages()
for (const message of messages) {
var from = message.getFrom().split("<")[1].split(">")[0];
newMembers.push({
'email': from.toLowerCase(),
role: "MEMBER"
})
message.markRead();
}
}
if (typeof newMembers !== 'undefined' && newMembers.length > 0) {
addUserstoGroup(newMembers);
}
}
function addUsertoGroup(userEmail) {
var groupId = "[email protected]";
var group = GroupsApp.getGroupByEmail(groupId);
try {
var hasMember = group.hasUser(userEmail);
if (!hasMember){
var newMember = {email: userEmail, role: "MEMBER"};
var mStatus=AdminDirectory.Members.insert(newMember, groupId);
Logger.log("Inserted: %s \n", userEmail);
return true;
}else{
Logger.log("Duplicated: %s \n", userEmail);
return false;
}
} catch(e) {
Logger.log("Error: %s Email: %s \n", e, userEmail);
return false;
}
}
Upvotes: 0