Python mysql-connector send to email

Hi

I would just like to ask for help how to redirect python mysql-connector to email using smtplib

#!/bin/python

import smtplib

import datetime

import mysql.connector

import email.utils

from email.mime.text import MIMEText

def send(self):

while True:

msg = self.msg_queue.get()

cnx = mysql.connector.connect(user='admin', password='pass',

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(body, 'text', 'utf-8')

print("The certificate {} is expiring on {:%d %b %Y}".format(application, valid_until).format(str, msg))

print("******************************************************************************* ".format(str, msg)

print("Description: {}".format(description, msg))

print("Application: {}".format(application, msg))

print("Location: {}".format(location, msg))

print("Issuer: {}".format(issuer, msg))

print("*******************************************************************************".format(str, msg))

server = smtplib.SMTP('localhost')

server.set_debuglevel(True)

server.sendmail('admin@example.com', ['admin@example.com'], msg.as_string())

server.quit()

cursor.close()

cnx.close()

Welcome, apohadin.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Note: Please go over your code again to edit in the correct indentation.

1 Like