AnsweredAssumed Answered

Email list of connected users needs character stripping

Question asked by jzumbadoCFG on Oct 10, 2019
Latest reply on Oct 15, 2019 by jzumbadoCFG

Full disclaimer: I'm not a programmer and understand the bare minimum of python just to get by. I want to learn more but don't have anyone to help with bouncing ideas or finding solutions. Hence, I'm here humbling requesting your help. 


I've been working off of  #Use Python scripting to batch reconcile and post versions. However, I have this problem were the list of user names comes back with double quotes and the domain plus user name. Ex. "domain\user name". 


So when it adds the full email address it comes out as: "domain\user name" Then of course it can't send an email because of the quotes and domain.


How do I eliminate the double quotes and domain from the userNames list? I tried .strip, as well as, .format and to no avail. 


Thanks in advance. 



import arcpy, os, csv, smtplib, ssl

SDE_Workspace = r"\\....DEFAULT.sde"


#Build list of connected users
userList = arcpy.ListUsers(SDE_Workspace)

for user in userList:
   print ("Username: {0}, Connected at: {1}, ID: {2}, IsDirectConnection: {3}".format(user.Name, user.ConnectionTime, user.ID, user.IsDirectConnection))


# get a list of user names from the list of named tuples returned from ListUsers
userNames = [u.Name for u in userList]


# take the userNames list and make email addresses by appending the appropriate suffix.
emailList = [name + '' for name in userNames]


FROM = ""

TO = emailList
SUBJECT = "Maintenance is about to be performed"
MSG = "Auto generated Message.\n\rServer maintenance will be performed in 15 minutes. Please log off."


# Prepare actual message
MESSAGE = """\
From: %s
To: %s
Subject: %s

""" % (FROM, ", ".join(TO), SUBJECT, MSG)


# Send the mail
server = smtplib.SMTP(EMAILSERVER)