Future Date Email or SMS Notification Trigger

246
1
05-18-2021 04:49 AM
JosephDindiok
New Contributor

Please, is it possible to get a future email or SMS notification for a date question type (e.g. ${tenancy_expiry_date}) when the said date is due? I am working on a project work which aims to improve rent tax mobilization by the Ghana Revenue Authority (GRA). I want the Authority to be notified when a rent tenancy agreement expires. For instance, a 3-year rent agreement entered into force on March 5, 2021 will expire on March 5, 2024. On March 5, 2024, the application which is designed in Survey123 Connect and published to AGOL should trigger an email or SMS notification to GRA informing it of the tenancy agreement expiration. The Debts and Compliance Unit of the Authority can then follow up on a possibly renewed tenancy agreement to administer rent tax. I hope to find a solution to this challenge. Thanks in advance! 

0 Kudos
1 Reply
DougBrowning
MVP Notable Contributor

Fancy was is using webhooks if you have access.  https://doc.arcgis.com/en/survey123/browser/create-surveys/webhooks.htm 

https://community.esri.com/t5/arcgis-survey123-blog/a-simple-e-mail-notification-system-for-survey12...

I think maybe Microsoft is an option now.

Or old school.  When I had to do this I wrote a Python script that runs every night using task scheduler.

This is Python 2.7 code but I would use Python 3 with ArcPro if possible (this will require some code change).  That way hitting a hosted feature service is way easier - just have Pro logged in and it uses its credentials.  

Create a layer file that has a query in it like LeaseExpire + 5 > today or something like that.  Then use that layer file in your script.

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

Author:     Doug Browning

Created:    12/7/2015
Updated:    05/19/2017

Purpose:    Shows how to send a email out with attachement

Changed:    Changed the smtp server due to move to outlook.
            Changed to work with GMail now


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"
# Will work
#emailSMTP = "yoursmtp.com"
smtpusername = "something@gmail.com"
smtppassword = "pass"


# Set To and From
emailfrom = "something@gmail.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 = ["soemthing@yahoo.com","asecond@dd.com"]      # or  emailRecList = ["dbrowning@blm.gov"]
# 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)
# for gmail
server = smtplib.SMTP_SSL(emailSMTP,465)
# for gmail
server.ehlo()
#server.starttls()
server.login(smtpusername,smtppassword)   # password not needed for here
# 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()

Hope that helps

0 Kudos