Apr-27-2020, 07:07 PM
Hello, I'm stuck at this point and looking for some help. I am trying to get the text file that my script creates, then email that out as an attached file. I keep getting an error "ERROR (421, b'4.3.2 Service not available')" my server is whitelisted to the mail relay server which I verified already.
Here is my code, please let me know what and where I am doing wrong this has been driving me crazy for 2 days now. ( i removed my email from and to list along with any info that would identify my mail server for security reasons)
Here is my code, please let me know what and where I am doing wrong this has been driving me crazy for 2 days now. ( i removed my email from and to list along with any info that would identify my mail server for security reasons)
#! /usr/bin/python3.6
import pymysql
import datetime
from pncnetworkdata import *
from datetime import datetime
import logging
import smtplib
from email.message import EmailMessage
logger = logging.getLogger('pncnetwork')
hdlr = logging.FileHandler('/opt/scripts/errors/pncnetworkerrors.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.WARNING)
#logging.info('Started')
try:
markets = ["Beltway", "Keystone", "Freedom", "New England"]
count = 0
cursorRowCount = 0
recordPrint = ""
for HOST in HOSTS:
db_con = pymysql.connect(host=HOST, port=3306, user=USERNAME, passwd=PASSWORD, db=DATABASE)
sql_select_Query = "SELECT * FROM pncnetwork where id=(SELECT MAX(id) FROM pncnetwork)"
cursor = db_con.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
for row in records:
recordPrint += markets[count] + ', ' + str(row[2]) + "\n"
cursorRowCount += cursor.rowcount
count += 1
db_con.close()
# print ("Total number of rows in pncnetwork is: ", cursorRowCount)
print ("\nPrinting each pncnetwork record\n", recordPrint)
# This section will print the script to file using the current date and save it to /opt/scripts/output
def get_filename_datetime():
# Use current date to get a text file name.
return "PNCChecker-" + str(datetime.today().replace(second=0, microsecond=0)) + ".txt"
#Get full path for writing
name = get_filename_datetime()
print("NAME", name)
path = "/opt/scripts/output/" + name
print("PATH", path);
with open(path, "w") as f:
# Write Data to file
f.write (recordPrint)
msg = EmailMessage()
msg["From"] = 'removed my email'
msg["Subject"] = "PNC Checker Results"
msg["To"] = 'removed to list'
msg.set_content("Hello, Please see attached results from today's PNC Checker script.")
msg.add_attachment(open(path, "r").read(), filename="NAME")
s = smtplib.SMTP('mailrelay.someone.com')
s.send_message(msg)
except Exception as e:
print("ERROR ", str(e))
logger.error('message')
