Select to view content in your preferred language

Service Support Email for Citizen Problem Reporter not working after configured.

562
1
11-25-2020 09:31 AM
JeffreySteele
Emerging Contributor

When I run the script below I receive the following error any help you can give me would be greatly appreciated. I am very new to this portion of ESRI.  Thank you!

Traceback (most recent call last):
File "C:\ServiceSupport\send_email.py", line 99, in <module>
to_addresses[:] = (value for value in to_addresses if value != '' and value != '#')
TypeError: 'str' object does not support item assignment
>>>

 

 

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

import smtplib, sys

 

class EmailServer(object):

    def __init__(self, smtp_server, smtp_username=None, smtp_password=None, use_tls=False):

        self._server = smtplib.SMTP(smtp_server)

        if use_tls:

            self._server.starttls()

        self._server.ehlo()

        if smtp_username and smtp_password:

            self._server.esmtp_features['auth'] = 'LOGIN'

            self._server.login(smtp_username, smtp_password)

 

    def __enter__(self):

        return self

 

    def send(self, from_address="", reply_to="", to_addresses=[], cc_addresses=[], bcc_addresses=[], subject="", email_body=""):

        msg = MIMEMultipart()

        msg['from'] = from_address

        if reply_to != "":

            msg['reply-to'] = reply_to

        if len(to_addresses) > 0:

            msg['to'] = ", ".join(to_addresses)

        if len(cc_addresses) > 0:

            msg['cc'] = ", ".join(cc_addresses)

        msg['subject'] = subject

        msg.attach(MIMEText(email_body, 'html'))

 

        recipients = to_addresses + cc_addresses + bcc_addresses

        if ('') in recipients:

            recipients.remove('')

        if len(recipients) == 0:

            raise Exception("You must provide at least one e-mail recipient")

 

        self._server.sendmail(from_address, recipients, msg.as_string())

 

    def __exit__(self, exc_type, exc_value, traceback):

        self._server.quit()

 

def _add_warning(message):

    try:

        import arcpy

        arcpy.AddWarning(message)

    except ImportError:

        print(message)

 

def _set_result(index, value):

    try:

        import arcpy

        arcpy.SetParameter(index, value)

    except ImportError:

        pass

 

if __name__ == "__main__":

    smtp_server = 'smtp.****.*********.**.gov'

    smtp_username = '***@.****.*********.**.gov'

    smtp_password = '********'

    use_tls = 'smtp.****.*********.**.gov'

    from_address = '***@.****.*********.**.gov'

    reply_to = '***@.****.*********.**.gov'

    to_addresses = '***@.****.*********.**.gov'

    cc_addresses ='***@.****.*********.**.gov'

    subject = 'You have a New Inquiry'

    email_body = '*:\***********\internal_email_template2.html'

 

   smtp_server = 'smtp.****.*********.**.gov'

    smtp_username = '***@.****.*********.**.gov'

    smtp_password = '********'

    use_tls = 'smtp.****.*********.**.gov'

    from_address = '***@.****.*********.**.gov'

    reply_to = '***@.****.*********.**.gov'

    to_addresses = '***@.****.*********.**.gov'

    cc_addresses ='***@.****.*********.**.gov'

    subject = 'Thank you for your Report'

    email_body = '*:\***********\user_email_template2.html'

    

 

    # Remove empty strings from addresses

    to_addresses[:] = (value for value in to_addresses if value != '' and value != '#')

    cc_addresses[:] = (value for value in cc_addresses if value != '' and value != '#')

    bcc_addresses[:] = (value for value in bcc_addresses if value != '' and value != '#')

    all_addresses = to_addresses + cc_addresses + bcc_addresses

 

    try:

        with EmailServer(smtp_server, smtp_username, smtp_password, use_tls) as email_server:

            email_server.send(from_address, reply_to, to_addresses, cc_addresses, bcc_addresses, subject, email_body)

        _set_result(11, True)

    except Exception as e:

        _add_warning("Failed to send e-mail. {0}".format(str(e)))

       _set_result(11, False)

 

 

0 Kudos
1 Reply
RandyBurton
MVP Alum

The error indicates that to_addresses is a string value, but the line of code is expecting to process a list.

>>> to_addresses = ['one@somewhere.gov', 'two@somewhere.gov', '', '#' ]
>>> to_addresses[:] = (value for value in to_addresses if value != '' and value != '#')
>>> to_addresses
['one@somewhere.gov', 'two@somewhere.gov']

Per the line of code, empty values and '#' will be removed from the list.

It also looks like you are setting up to process two email messages in your lines of code after __name__ ==  __main__.   But you are just resetting the variables to new values, so only the last value will actually be used. Since bcc_addresses is not defined, this will also cause an error.

 

 

0 Kudos