Sending email on feature service edit

5221
23
Jump to solution
07-17-2018 06:55 AM
joerodmey
MVP Alum

I'm looking to have an email sent out each time my hosted feature service (on AGOL) has my status field altered. This status field is set to "not complete" by default, and as its completed it changes to "complete". When this happens I want an email sent out.

I've been playing around with webhooks and services likes Microsoft Flow and Integromat but not having too much success.

Any ideas?

Tags (1)
0 Kudos
23 Replies
joerodmey
MVP Alum

Get further. Able to see that feature 1034 was added. Get error when trying to send email via gmail:

Generating Token
[1034]
Traceback (most recent call last):
  File "H:/Custom_WebApp_Code/Send_Email.py", line 64, in <module>
    smtpObj = smtplib.SMTP(host=smtpServer, port=portNumber)
  File "C:\Python27\ArcGIS10.6\lib\smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python27\ArcGIS10.6\lib\smtplib.py", line 318, in connect
    (code, msg) = self.getreply()
  File "C:\Python27\ArcGIS10.6\lib\smtplib.py", line 369, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed
0 Kudos
JakeSkinner
Esri Esteemed Contributor

This issue appears to be with GMAIL's SMTP server.  I haven't had much luck using GMAIL.  Are you able to use your Organizations SMTP?

0 Kudos
joerodmey
MVP Alum

Don't think I'll be able to get my hands on the email server details

0 Kudos
DougBrowning
MVP Esteemed Contributor

Looks like your code is assuming port 25 but gmail usese a different port.  First hit on google so it may be old.

Gmail Free SMTP Server Information

You can use Gmail’s SMTP server information in a number of different places. You can use this in your local email client like Microsoft Outlook or within your WordPress site with a plugin like Gmail SMTP or SendGrid. Check out our tutorial on how to send emails from within WordPress.

Outgoing Mail (SMTP) Server: smtp.gmail.com
Use Authentication:
 Yes
Use Secure Connection:
 Yes (this can be TLS or SSL depending on your mail client)
Username: GMail account (email@gmail.com)
Password: GMail password
Port:
 465 or 587

0 Kudos
joerodmey
MVP Alum

Still nothing

0 Kudos
DougBrowning
MVP Esteemed Contributor

Your email code looks different than mine.  This one worked for years for me.

'''-------------------------------------------------------------------------------
Name:       How to Send Email with Attachment.py


HTML and Attachemnt are turned off in this sample
-------------------------------------------------------------------------------'''

import arcpy
import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText

# May work
#emailSMTP = "smtp.gmail.com:587"
# Will work
emailSMTP = "yoursmtp.com"
smtpusername = "username@onmicrosoft.com"
smtppassword = "pass"


# Set To and From
emailfrom = "username@something.com"
# Weird but the msg["To"] must be a string but the call to the smtp server must be a list see line 71.  not easy to figrue out
# List can be a single text also but easier to just always be a list so that split code works
# This is who it will send to.  Always make this a list so that the split and join below work
emailRecList = ["someone@yahoo.com","someotherguy@test.com"]      # or  emailRecList = ["justoneguy@test.com"]
# this is a text list it needs for the to
emailto = ','.join(emailRecList)

# give a path to a file or set a flag by setting = no
#fileToSend = r"c:\temp\temp.pdf"
fileToSend = "no"

msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
# Can set any Subject here
msg["Subject"] = "Form Attached"
# No Idea where this goes
msg.preamble = "Where is this"

# For text - plain or HTML
# Plain Sample
text = "Hi!\nHow are you?\nHere is the text you wanted."

# HTML sample
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="https://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""


part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
# Uncomment out to send HTML
#msg.attach(part2)

# For attachement
if fileToSend != "no":
    ctype, encoding = mimetypes.guess_type(fileToSend)
    if ctype is None or encoding is not None:
        ctype = "application/octet-stream"
    maintype, subtype = ctype.split("/", 1)
    fp = open(fileToSend, "rb")
    attachment = MIMEBase(maintype, subtype)
    attachment.set_payload(fp.read())
    fp.close()
    encoders.encode_base64(attachment)
    attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
    msg.attach(attachment)

# Send section
server = smtplib.SMTP(emailSMTP)
server.starttls()
server.login(smtpusername,smtppassword)   # password not needed for me but you may
# Weird but the msg["To"] must be a string but the call to the smtp server must be a list
server.sendmail(emailfrom, emailRecList, msg.as_string())
server.quit()
0 Kudos
joerodmey
MVP Alum

Got some errors (attached)

0 Kudos
DougBrowning
MVP Esteemed Contributor

This looks like it is failing on import arcpy?  Is this running on a machine with arc installed?  Are you maybe trying to run it on a server?  What are you running scripts in?  Just Idle or command line?  Get PyScripter i you can.  Then just do a import arcpy to see if that works.  Your python may not be setup correctly.

Sorry not sure how familiar you are with python but this seems to show it is not even trying the script.  Does a 1 line script with just import arcpy in it work?

0 Kudos
DougBrowning
MVP Esteemed Contributor

Wait looking at your script you don't use any arcpy stuff so you can just get rid of the import arcpy line.

0 Kudos
joerodmey
MVP Alum

I used your script and got those errors. Removed import arcpy and this time its a connection error. Probably something to do with smptp settings in Gmail

0 Kudos