0

The code below sends an email to my outlook. But, the email is empty and the column names in the query are printed in subject line and the email body is empty and I don't see the query results.

import smtplib

import pyodbc

import pandas as pd

myconn = pyodbc.connect('Driver='{SQL Server}',host='', database='',user='',password='', Trusted connection='yes')

query = """SELECT *;"""

df = pd.read_sql(query, myconn)

remail = "sendto@gmail.com"

smtpObj = smtplib.SMTP('smtp-outlook.com', 587)

smtpObj.ehlo()

smtpObj.starttls()

smtpObj.login('from@gmail.com','PASSWORD')

smtpObj.sendmail('from@gmail.com', remail, 'Subject:Query results \n' +df.to_string())

smtpObj.quit()
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
smritisree
  • 1
  • 1
  • 3
  • 3
    Possible duplicate of [Python email body is empty](https://stackoverflow.com/questions/50214990/python-email-body-is-empty) – Henry Yik Oct 08 '19 at 07:24

1 Answers1

2

use yagmail lib. to send email

import smtplib
import yagmail
import pandas as pd

import mysql.connector


host= '*****'
user= '*****'
passwd= '******'
db= '********'

mydb = mysql.connector.connect(
     host=host,
     user=user,
     passwd=passwd,
     database=db
     )
myconn =mydb

query = """SELECT * from sa_user;"""

df = pd.read_sql(query, myconn)

receiver = "*******@gmail.com"

print(df)
yag = yagmail.SMTP("*******@gmail.com")
yag.send(
        to=receiver,
        subject="Verify your email",
        contents=df.to_string(), 
        )

print("done")

this is my inbox screenshot

enter image description here

Akshay Karande
  • 559
  • 4
  • 6