I'm working on a python script that would read from a table then send a single message to the users including the column information on each row the query returns, the script looks like this:
#!/usr/bin/python
import smtplib
import psycopg2
connection = psycopg2.connect(user="postgres",
password="postgres",
host="localhost",
port="5432",
database="myDB")
db_cursor = connection.cursor()
s = "SELECT error_message FROM temp.data_upload_errors"
db_cursor.execute(s)
try:
array_rows = db_cursor.fetchall()
except psycopg2.Error as e:
t_message = "Postgres Database error: " + e + "/n SQL: " + s
#return render_template("error.html", t_message = t_message)
db_cursor.close()
sender = 'email@provider.com'
receivers = ['email@provider.com']
for t_item in array_rows:
msg = "Error Message: " , t_item , "<br>"
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, msg.as_string())
print "Successfully sent email"
except smtplib.SMTPException:
print "Error: unable to send email"
However, I'm getting this error:
AttributeError: 'tuple' object has no attribute 'as_string'
I'd like to return the whole content under msg
, not just the [0]
or [1]
array item.
I'm new to Python so not sure what may be wrong.