向 Python 中的多个地址发送电子邮件 3

Sending email to multiple addresses in Python 3

最近我们将 Python 从 2.x 升级到 3.x。第 11 行注释掉的“brian@email.com”将发送一封电子邮件,但第 10 行的电子邮件列表停止运行并转到 3.x。报告的错误是“unhashable type: 'list'”。我如何升级多电子邮件行以与 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()  

Python 认为您试图通过在其周围使用方括号来塑造 TO。在 python 中,您可以简单地调用一个没有方括号的列表。如果您想要列表中的特定项目,例如 list[position_number],您应该只使用方括号。第二个问题是 TO 变量必须是字符串而不是 list 即使有多个地址,它们也必须用逗号分隔

代码:

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()