Python mysql-connector send to email


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


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',



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 =, 1, 1)

valid_until =, 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.sendmail('', [''], msg.as_string())




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.


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

1 Like