0

So I'll try to explain this the best I can. The task I have at hand is to gather data from a database and to put that information into a custom coded/formatted HTML/CSS email.

The error I get when running this code is: TypeError: Can't convert 'list' object to 'str' implicitly. The data I am getting from the database bring back a result of 8 strings within a list in each section so I know why that error is coming back. I'm just not too sure how to get around it to be able to embed all the data within an email automatically and send it with proper formatting for email like this.

import smtplib
import sys
import sqlite3
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

DATABASE = 'Blocs.db'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

print("Getting blocs from server...")
conn = sqlite3.connect(DATABASE)
cur = conn.cursor()
cur.execute("SELECT weburl FROM Blocs")
weburl_data = cur.fetchall()
cur.execute("SELECT imgurl FROM Blocs")
imgurl_data = cur.fetchall()
cur.execute("SELECT title FROM Blocs")
title_data = cur.fetchall()
cur.execute("SELECT notes FROM Blocs")
notes_data = cur.fetchall()
conn.commit()
conn.close()

from_email = "test@email.com"
from_pwd = "Password"
to_email = "sendHere@email.com"

msg = MIMEMultipart('html')
msg['Subject'] = "Test SMTPlib Message"
msg['From'] = "test@email.com"
msg['To'] = "sendHere@email.com"

firstHTML = '<html> <head></head> <body><table> <tr>'
bloc_one = weburl_data
secondHTML = '</tr></table></body></html>'

new_html = firstHTML + bloc_one + secondHTML

msg.attach(MIMEText(new_html, 'html'))
print(msg)

mail = smtplib.SMTP('outlook.office365.com', 587)
mail.ehlo()
mail.starttls()
mail.login("test@email.com", "Password")
mail.sendmail("test@email.com", "sendHere@email.com", msg.as_string())
print("email sent")
mail.quit()
martineau
  • 119,623
  • 25
  • 170
  • 301
Aaria
  • 279
  • 3
  • 9
  • 19
  • 1
    Maybe you could say in what line the error occurred ?! – Mario Geuenich Dec 04 '16 at 00:32
  • im going to assume that the error is occurring at `new_html = firstHTML + bloc_one + secondHTML`. since bloc_one is still a list. you should convert that list to a string as in the answer below. – parsethis Dec 04 '16 at 01:10

1 Answers1

0

I am not really an expert in the mail sending department but if you only problem is converting list to str why not try and do it with join?

new_string = ''.join(name_of_the_list)

I hope it helps :)

WholesomeGhost
  • 1,101
  • 2
  • 17
  • 31