Search code examples
pythonsmtplib

Sending email to multiple addresses in Python 3


Recently we upgraded our Python from 2.x to 3.x. The commented out "brian@email.com" on line 11 will send an email, but the list of emails on line 10 discontinued functioning with going to 3.x. The error reported is " unhashable type: 'list'". How could I upgrade the multiple emails line to be compatible with Python 3.x?

fc = "D:\\WorkSpace\\Water\\Workspace.gdb\\NewAccountsGeocoded"
    fields = ['USER_MunisAccount','IN_Single_Line_Input']
    qry = "Status NOT IN ( 'M', 'T')"
    with arcpy.da.SearchCursor(fc, fields, qry) as cursor: 
        for row in cursor:
            print(row [0], row [1])           
            txtFile.write("{0}{1}".format(arcpy.GetMessages(), '\n'))
            HOST = "server.email.com"
            SUBJECT = "Report: Unable to Add UB Accounts to GIS Accounts"
            TO = ["symon@email.com","dave@email.com","brian@email.com"]
            #TO = "brian@email.com"               
            FROM = "Water Accounts<GIS@email.com>"
            text = "The following UB Account was not able to be imported into GIS WtrAccounts. Please see if there is an error in UB or if the address exists in COH Address GIS feature class. \n " + "UB Account: "+str(row[0]) + " | UB Address: "+str(row[1])+"\n"
            BODY = "From: {1}{0}To: {2}{0}Subject: {3}{0}{0}{4}".format('\r\n', FROM, TO, SUBJECT, text)
            server = smtplib.SMTP(HOST)
            server.sendmail(FROM, [TO], BODY)
            server.quit()  

Solution

  • Python thinks you are trying to shape TO by using square brackets around it. In python you can simply call a list without the square brackets around it. You should only use the square brackets if you want a specific item in the list like list[position_number]. The second problem is the TO variable must be a string not a list even if there are multiple addresses they must just be separated by a comma

    The code:

    fc = "D:\\WorkSpace\\Water\\Workspace.gdb\\NewAccountsGeocoded"
        fields = ['USER_MunisAccount','IN_Single_Line_Input']
        qry = "Status NOT IN ( 'M', 'T')"
        with arcpy.da.SearchCursor(fc, fields, qry) as cursor: 
            for row in cursor:
                print(row [0], row [1])           
                txtFile.write("{0}{1}".format(arcpy.GetMessages(), '\n'))
                HOST = "server.email.com"
                SUBJECT = "Report: Unable to Add UB Accounts to GIS Accounts"
                TO = "symon@email.com, dave@email.com, brian@email.com"
                #TO = "brian@email.com"               
                FROM = "Water Accounts<GIS@email.com>"
                text = "The following UB Account was not able to be imported into GIS WtrAccounts. Please see if there is an error in UB or if the address exists in COH Address GIS feature class. \n " + "UB Account: "+str(row[0]) + " | UB Address: "+str(row[1])+"\n"
                BODY = "From: {1}{0}To: {2}{0}Subject: {3}{0}{0}{4}".format('\r\n', FROM, TO, SUBJECT, text)
                server = smtplib.SMTP(HOST)
                server.sendmail(FROM, TO, BODY)
                server.quit()