Sending email via Python

1269
5
11-13-2016 02:15 AM
ErnejunCorvera
New Contributor III

I tried one of the sample python code to send email to the active database user as part of the database maintenance procedure. But I have a problem in sending it because of the domain included in the username (e.g. xxx\username)...So, I want to remove the domain name and the backslash and retain only the username as the email list. (Please check the attached python code).

0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

Unzipping zips are a pain... so barring the pertinent code snippet... give this a look-see. Note in python the offending string is in 'raw' format, hence the 'r in front of it.  A backslash '\' is an escape designator, so to use one without raw format you have to put a double-backslash.  Then just split and if the username is the last, just grab the username from the end (-1), if it was the second last, you would use -2 etc

>>> a = r"somedomain\xxx\username"
>>> b = a.split("\\")[-1]
>>> b
'username'‍‍‍‍
JunCorvera
New Contributor

thanks for the reply...yes that will work but how can I apply it inside the tuple?

0 Kudos
DanPatterson_Retired
MVP Emeritus

is user.Name returning the string in the format you showed

why don't you show what the actual emailList looks like minus the "@xxx.com.sa" stuff

because it can't be returning a simple string for user.Name

I suspect that it is returning one of the two incarnations that worked in the sample that follows.  

If it does, then you just need to modify the split

>>> a = "somedomain\\xxx\\username1"
>>> b = "somedomain/xxx/username1"
>>> c = "somedomain\xxx\username1"
  File "<string>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 10-11: truncated \xXX escape
‍‍‍‍‍

It can't just be returning a string with a single backslashes in it eitherwise it would throw and error at some point.

Notice that 'a' and 'b' are fine... either 'raw' format, double backslashes or a single forward slash will pass the test, not a single backslash

a = ["somedomain\\xxx\\username1", "somedomain\\xxx\\username2", "somedomain\\xxx\\username3"]
>>> emails = [name.split("\\")[-1] for name in a]
>>> emails
['username1', 'username2', 'username3']

or...

>>> a = ["somedomain/xxx/username1", "somedomain/xxx/username2", "somedomain/xxx/username3"]
>>> emails = [name.split("/")[-1] for name in a]
>>> emails
['username1', 'username2', 'username3']
JunCorvera
New Contributor

Dan, when I ran the code userList = [user.Name for user in arcpy.ListUsers(workspace)], it returns a list of users with the format 123456\\xx.com.sa. when I ran your suggested code emails=[name.split("\\")[-1] for name in userList], it returns a list of username only but have a little problem there's a double quote at the end of the username....[u'123456"', u'6789"'].

0 Kudos
DanPatterson_Retired
MVP Emeritus

Which is it?

>>> a = [u"username1\\xx.com", u"username2\\xx.com", u"username3\\xx.com"]
>>> emails = [name.split('\\')[-1] for name in a]
>>> emails
['xx.com', 'xx.com', 'xx.com']
>>> emails = [name.split('\\')[0] for name in a]
>>> emails
['username1', 'username2', 'username3']‍‍‍‍‍‍‍‍‍‍‍‍‍‍

if the username (my list 'a') looks like line 1 above, then you would have had to split using line 5 above to get the first part of the split.

once you have the parts as in line 7 above, you can concatenate what you want to it.. You will notice that they appear as email links... 

>>> emails
['username1', 'username2', 'username3']
>>> new = [e + "@yy.com" for e in emails]
>>> new
['username1@yy.com', 'username2@yy.com', 'username3@yy.com']‍‍‍‍‍