python mysql 发邮件,通过python通过电子邮件发送mysql select语句

I am trying to insert a select statement into an email. The table

has 10 rows and 3 columns but I'm not able to do it. Code and errors below.

#!/usr/bin/env python

import csv

import MySQLdb

import pysftp

import smtplib

import sys

import os

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

conn = MySQLdb.connect(XXXX)

#Email Part1 Msg

part1 = """

To: To Person <>

MIME-Version: 1.0

Content-type: text/html

Subject: SMTP HTML e-mail test

"""

sender = ''

receivers =

x = conn.cursor()

query1= ("""select * from table""")

smtpObj=smtplib.SMTP('smtp', 25)

smtpObj.sendmail(sender, receivers, x.execute(query1))

This is the error messsage:

Traceback (most recent call last):

File "test3.py", line 33, in

smtpObj.sendmail(sender, receivers, x.execute(query1))

File "/usr/lib/python2.7/smtplib.py", line 729, in sendmail

esmtp_opts.append("size=%d" % len(msg))

TypeError: object of type 'long' has no len(

解决方案

You have to pass in a string as the 3rd argument to smtpObj.sendmail(). After you do x.execute(query1), you have to fetch the results and turn them into a string.

x.execute(query1)

msg = ""

for row in x.fetchall():

msg += str(row) # or however you turn a row into a string

smtpObj.sendmail(sender, receivers, msg)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值