Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29767

Python mysql connector query to send email

$
0
0

I have a script, that connect to mysql and print a query to send to email.

#!/bin/pythonimport smtplibimport datetimeimport mysql.connectorfrom email.mime.text import MIMETextcnx = mysql.connector.connect(user='admin', password='admin',                                host='127.0.0.1',                                database='database')cursor = cnx.cursor()query = ("SELECT name, description, valid_to, application, location, issuer FROM certs ""WHERE hidden = 0 and valid_to >= %s  and valid_to < %s")valid_from =  datetime.date(2020, 1, 1)valid_until = datetime.date(2020, 12, 31)cursor.execute(query, (valid_from, valid_until))for (name, description, valid_to, application, location, issuer) in cursor:  msg = MIMEText("The certificate {}, is expiring on {:%d %b %Y}".format(application, valid_until))  msg = MIMEText("Description: {}".format(description))  msg = MIMEText("Application: {}".format(application))  msg = MIMEText("Location: {}".format(location))  msg = MIMEText("Issuer: {}".format(issuer))  msg['Subject'] = 'Simple test message'  server = smtplib.SMTP('localhost')  server.set_debuglevel(True) # show communication with the server  server.sendmail('admin@gmail.com', ['admin@gmail.com'], msg.as_string())server.quit()cursor.close()cnx.close()

How can I add, this result, to the email body?

I will merge with this script


Viewing all articles
Browse latest Browse all 29767

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>