Python email verification?

377
2
08-20-2018 04:49 PM
by Anonymous User
Not applicable

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."
0 Kudos
2 Replies
deleted-user-qpvAI3Fo0MKR
Occasional Contributor III

Forgive me for answering your question with a question, but if your msg['From'] variable is your email address wouldn't the bad address return a non-deliverable error message to your email address?

0 Kudos
by Anonymous User
Not applicable

The ['From'] address pretty much just shows as a from address to the recipient. When scripting the email process it is sent directly from my email server and not really a specific email address on the server. No notification is sent to me with this script if an email is invalid. If I open outlook and manually send to a invalid email address I am notified. 

0 Kudos