AnsweredAssumed Answered

Python email verification?

Question asked by vonmoosa on Aug 20, 2018
Latest reply on Aug 21, 2018 by vonmoosa

Anybody know of a way with Python to get an alert if an email address is invalid?? Below is my current script but I would like to integrate a way to be notified if an email address is invalid and does not get sent. I've read of a way to use server.verify to do this but was unable to get it to work for me.

 

 

import arcpy, datetime, smtplib, os, email, email.mime.application 
arcpy.env.overwriteOutput = True

points = r"******************.shp" # feature class
today = datetime.date.today() # creating date time variable
dte = today.strftime('%m/%d/%Y') # converts date time variable to string
dtep = today.strftime('%m_%d_%Y')

fields = ['Applicant', 'Date_Final', 'Email', 'Permit_No'] # define fields for search cursor and print format to use

arcpy.MakeFeatureLayer_management(points, 'FinalizedApproaches') #makes approaches fc a feature layer for use in this script

with arcpy.da.SearchCursor(points, fields, "Date_Final = CURRENT_DATE") as cursor:
    for row in cursor:
        print "{}\t{}\t{}".format(row[0], " Finalized", row[2])
print " "
print "{} list generated".format(dte)
print " "
print "Finalized notifications are being generated"
print " "

with arcpy.da.SearchCursor(points, fields, "Date_Final = CURRENT_DATE") as email_cursor:
    for row in cursor:
        SERVER = "****************"
        msg = email.mime.Multipart.MIMEMultipart()
        msg['Subject'] = "Test"
        msg['From'] = "***********"
        msg['To'] = "*********"
        msg['Cc'] = "*********"
        msg['Cc2'] = "*********"
        body = email.mime.Text.MIMEText(""'\n {},\n'.format(row[0]) + "        " + "This is to notify you that your approach" + " (" + '{}'.format(row[3]) + ") " + "has been finalized.""")
        msg.attach(body)
        filename=r"*******.pdf"
        fp=open(filename,'rb')
        att = email.mime.application.MIMEApplication(fp.read(),_subtype="pdf")
        fp.close()
        att.add_header('Content-Disposition','attachment',filename="{}.pdf".format(row[3]))
        msg.attach(att)
        server = smtplib.SMTP(SERVER)
        server.sendmail('*********', [msg['To'],msg['cc'],msg['Cc2']], msg.as_string())

server.quit()
print "Finalized approach notifications sent."

Outcomes