Select to view content in your preferred language

Clip and Ship

593
4
08-05-2010 05:39 AM
CrystalCowardin
Regular Contributor
I have made a rather large model and exported it out to a python script which runs under the Scheduled Tasks part of Windows.  The end result is a .dbf file with all results.  I tried adding the Email script to my model from the Clip and Ship example.  I really no nothing about python but I would like to be able to modify the email script to send the .dbf file instead of a zip file.  Any help is appreciated.
0 Kudos
4 Replies
CrystalCowardin
Regular Contributor
Doesn't anyone have a suggestion?  Please!  Thanks!!
0 Kudos
RDHarles
Regular Contributor
I suggest posting the script so we can take a look...
0 Kudos
CrystalCowardin
Regular Contributor
This is a straightforward copy and paste from the ESRI Clip and Ship Script.  I would like to modify this script to attach a .dbf file and send an e-mail. 

import arcgisscripting, smtplib, os, sys, traceback

from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

gp = arcgisscripting.create(9.3)

#**********************************************************************
# Description:
#   Emails a file. File is assumed to be a zip file. Routine either attaches
#   the zip file to the email or sends the URL to the zip file.
#
# Parameters:
#   1 - File to send.
#   2 - Email address to send file.
#   3 - Name of outgoing email server.
#   4 - Output boolean success flag.
#**********************************************************************

def send_mail(send_from, send_to, subject, text, f, server, sendZip = False):

    # Construct the message
    #
    msg = MIMEMultipart()
    msg['jon33@co.henrico.va.us'] = send_from
    msg['jon33@co.henrico.va.us'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    if not sendZip:         # Send URL to zip file
        text = text + "\n\n" + f   
        msg.attach( MIMEText(text))  

    else:                   # Attach zip file
        msg.attach( MIMEText(text) )
        part = MIMEBase('application', "dbf")   # Change if different file type sent.
        part.set_payload( open(f,"rb").read() )
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
        msg.attach(part)
       
    smtp = smtplib.SMTP(server)
   
    # If your server requires user/password, uncomment the smtp.login code
    #  below and supply your server user/password.
    #
    # smtp.login(user, password)
   
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close()
  
if __name__ == '__main__':
    zipfile = gp.GetParameterAsText(0)
    sendto = gp.GetParameterAsText(1)
    eMailServer = gp.GetParameterAsText(2)

    # If sendZip is true, the zip file will be attached to the email.
    # If false, the URL to the zip will be in the body of the email.
    #
    sendZip = True
    subject = "Your ZIP file containing your area of interest"
   
    try:
        if sendZip:
            # Attach zip
            #
            text = "Attached is a zip file containing the data in the area of interest you specified."

            # Don't email large zip files.
            #       
            zipsize = os.path.getsize(zipfile)
            gp.AddMessage("Zip file size = " + str(zipsize))
            if  zipsize <= 5000000:
                send_mail("zip@yourserver.com", [sendto], subject, text, zipfile, eMailServer, sendZip)
                gp.AddMessage("Sent zipfile to " + sendto + " from zip@yourserver.com")
                gp.SetParameterAsText(3, "True")
            else:
                # Instead of raising an error, you could modify to send the URL instead.
                #
                gp.AddError("The resulting zip file is too large (%sMB).  Must be less than 5MB.  Please digitize a smaller Area of Interest." % \
                            (str(round(zipsize / 1000000.0, 2))))
                gp.SetParameterAsText(3, "False")
                raise Exception

        else:
            # Send URL instead of .zip file.  The jobs and virtual directory can be found
            #  in service's properties dialog box.  UNIX note: this code converts pathnames
            #  to lower case to  do the string replace.
            #
            text = "Click the link below to open zip file containing the area of interest you specified."

            jobsDir = r"d:\arcgisserver\arcgisjobs"
            virtualDir = "http://xyzzy/arcgisjobs"
           
            # Form the URL to this text file. Replace backslashes with url forward slash
            #  convention.
            #
            url = zipfile.lower().replace(jobsDir, virtualDir)
            url = url.replace("\\", "/")
            send_mail("zip@yourserver.com", [sendto], subject, text, url, eMailServer, sendZip)         
              
    except:
        # Return any python specific errors as well as any errors from the geoprocessor
        #
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n    " + \
                str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
        gp.AddError(pymsg)

        msgs = "GP ERRORS:\n" + gp.GetMessages(2) + "\n"
        gp.AddError(msgs)
        gp.AddError("Unable to send email")
0 Kudos
RDHarles
Regular Contributor
I got it to work, but I had to change and rearrange the script quite dramatically to get it to work.
Make sure you update the code to reflect you email address and the IP address of your mail server.

import arcgisscripting, smtplib, os, sys, traceback

from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

gp = arcgisscripting.create(9.3)

#**********************************************************************
# Description:
# Emails a file. File is assumed to be a zip file. Routine either attaches
# the zip file to the email or sends the URL to the zip file.
#
# Parameters:
# 1 - File to send.
# 2 - Email address to send file.
# 3 - Name of outgoing email server.
# 4 - Output boolean success flag.
#**********************************************************************

def send_mail(send_from, sendto, subject, text, f, server, sendZip = False):

    # Construct the message
    #
    msg = MIMEMultipart()
    msg['vince@xxx.com'] = send_from
    msg['grill@with.com'] = COMMASPACE.join(sendto)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )
    part = MIMEBase('application', "dbf") # Change if different file type sent.
    part.set_payload( open(f,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
    msg.attach(part)

    smtp = smtplib.SMTP(server)
    smtp.sendmail(send_from, sendto, msg.as_string())
    smtp.close()

## Arguments
# Path to dbf
dbffile = sys.argv[1]
# Email
sendto = sys.argv[2]
# IP address
eMailServer = sys.argv[3]

subject = "DBF file attached"  
text = "Attached is the dbf file you requested."    
sendZip = True
send_mail("200.110.100.1", sendto, subject, text, dbffile, eMailServer, sendZip)
print "Sent dbffile to "+sendto+" from 200.110.100.1"
0 Kudos