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"@myorganiztion.com. 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 + '@myorganization.com' for name in userNames]
EMAILSERVER = "myorganization.us"
FROM = "username@myorganization.com"
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
%s
""" % (FROM, ", ".join(TO), SUBJECT, MSG)
# Send the mail
server = smtplib.SMTP(EMAILSERVER)
Hey Jennifer,
Try this:
emailList = [name[1:-1] + '@myorganization.com' for name in userNames]
The [1:-1] should strip the first and last characters in the username.
-Josh
Thanks Joshua. That worked great! However, one more question...if I wanted to exclude a particular user name...say the user name that publishes REST services as an owner....would it be with an if...then statement or if....else? And where within my script would I write it? After the tuple or before? How would I go about doing it? These are the things that really get to me in a pickle.
Thanks once again.
Jennifer,
You could define a list of usernames you wish to exclude, and do something like this:
userNames = [u.Name for u in userList]
exclusion_list = ["Josh", "Jennifer"]
for name in userNames:
if name in exclusion_list:
userNames.remove(name)
emailList = [name + '@myorganization.com' for name in userNames]
Hope this helps!
-Josh
It worked great! Thanks so much.