Python Forum
Trouble with Name not being defined - don't know why?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with Name not being defined - don't know why?
#1
Hi Folks -

I have the following code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
import sys
import json
import datetime
import smtplib
import time
from pprint import pprint
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


def sendemail(email, data):
    ret = ''
    try:
        me = '[email protected]'  # You can update the helpdesk mail address.
        my_password = 'Password'  # Mailbox Password
        you = email
        msg = MIMEMultipart('alternative')
        msg['Subject'] = 'IMMEDIATE ACTION REQUIRED: Unassigned Requests (Orphans)'
        msg['From'] = me
        msg['To'] = email
        msg['X-Priority'] = '2'

        html = data
        part2 = MIMEText(html, 'html')

        msg.attach(part2)

        s = smtplib.SMTP_SSL('server.net')  # Mail Server Host name
        s.login(me, my_password)

        s.sendmail(me, you, msg.as_string())
        s.quit()
        ret = 'success'
    except smtplib.SMTPException as err:
        print ('Unable to Send emails : ', err)
        ret = 'failure'
    return ret


reportJson = str(sys.argv[1])
json_data = open(reportJson).read()
data = json.loads(json_data)
counter = 0
techEmail = ''
message = \
    'Attention Pod Leads, <br><br>' + \
    'Below is a list of tickets that do not have a Site, Request Type, Technician and other required field values. <br><br>' + \
    'Please review, triage if you can and/or forward along to the necessary Pod member. <br><br>' + \
    'Thank you, <br>' + \
    'PSRS Administrator' + \
    '<br><br>' + \
    """<html><html><head><style>table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even) {background-color: #dddddd;}</style></head><body><table><tr><th>RequestID</th><th>Site</th><th>RequestType</th><th>Status</th><th>Requester</th><th>Subject</th></tr>"""
length = len(data)

for dObj in data:
    requestID = dObj.get('requestid')
    site = dObj.get('site')
    RequestType = dObj.get('RequestType')
    status = dObj.get('status')
    requester = dObj.get('requester')
    subject = dObj.get('subject')
    email = '[email protected]'
    if RequestType is None:
        RequestType = 'Not Assigned'
    if counter == 0:
        techEmail = email
    counter += 1
    if techEmail == email:
        message += \
            """<tr><td>""" \
            + requestID + """</td><td>""" + site + """</td><td>""" + RequestType + """</td><td>""" \
            + status + """</td><td>""" + requester + """</td><td>""" \
            + subject + """</td></tr>"""
        if counter == length:
            message += '</table></body></html>'
            r = sendemail(techEmail, message)
            print(techEmail)
    else:
        message += '</table></body></html>'
        r = sendemail(techEmail, message)
        print(techEmail)
        techEmail = email
        message = \
            """<html><head><style>table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even) {background-color: #dddddd;}</style></head><body><table><tr><th>RequestID</th><th>Site</th><th>RequestType</th><th>Status</th><th>Requester</th><th>Subject</th></tr><tr><td>""" \
            + requestID + """</td><td>""" + site + """</td><td>""" + RequestType + """</td><td>""" \
            + status + """</td><td>""" + requester + """</td><td>""" \
            + subject + """</td></tr>"""
        if counter == length:
            message = \
                """<html><head><style>table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even) {background-color: #dddddd;}</style></head><body><table><tr><th>RequestID</th><th>Site</th><th>RequestType</th><th>Status</th><th>Requester</th><th>Subject</th></tr><tr><td>""" \
                + requestID + """</td><td>""" + site + """</td><td>""" + RequestType + """</td><td>""" \
                + status + """</td><td>""" + requester + """</td><td>""" \
                + subject + """</td></tr></table></body></html>"""
            r = sendemail(techEmail, message)
            print(techEmail)

if(r=="success"):
	print("Reports sent successfully to the respective techs")
else:
	print("Unable to send reports")
I included an attachment which outlines my error. It's saying 'r' is not defined. I have other scripts setup in similar format with no issues. What could the problem be here?

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
Trouble with Name not being defined - don't know why? - by simms7400 - Jul-01-2020, 12:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 3,771 May-30-2020, 04:14 AM
Last Post: DT2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020