Select to view content in your preferred language

Clip and Ship

06-08-2012 07:24 AM
Occasional Contributor
I need to modify the from the Clip and Ship geoprocessing service in order to work with a MS Exchange Server email account. Is that possible? I want to add the srcipt as a schedule task not as a GP service. I know that the original script is using an SMTP server but I cannot find the libraries required to apply these changes. Any help will be appreciated.

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['From'] = send_from
    msg['To'] = 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', "zip")   # Change if different file type sent.
        part.set_payload( open(f,"rb").read() )
        part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
    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 here", "password here")
    # smtp.login(user, password)
    smtp.sendmail(send_from, send_to, msg.as_string())
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"
        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("", [sendto], subject, text, zipfile, eMailServer, sendZip)
                gp.AddMessage("Sent zipfile to " + sendto + " from")
                gp.SetParameterAsText(3, "True")
                # 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

            # 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("", [sendto], subject, text, url, eMailServer, sendZip)         
        # 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"

        msgs = "GP ERRORS:\n" + gp.GetMessages(2) + "\n"
        gp.AddError("Unable to send email")
0 Kudos
2 Replies
Esri Contributor
You can set up Microsoft Exchange account as an email server host. I did not try it, but I configure my Google and Yahoo email account as an email host and it works fine. Just make sure:

1. If your company has fire wall, it may be needed to be disabled.
2. Or use external network instead of internal network, which has fire wall set on.
3. You need to know your smtp server name and possibly port number.

Here is a link that may help.

As for schedule work. It is possible too. What kind of schedule work you are referring to?

0 Kudos
Occasional Contributor
Thanks, I will take a look at these suggestions and post the results for the benefit of everyone.
0 Kudos