I am testing this code, I got from Github, to render jinja template and send as an email.
When I run the code as below. I get the following error message. Need help to identify and resolve the issue. Thanks.
JayMac$ python3 "send_email.py"
ERROR:root:Error sending email
ERROR:root:object of type 'filter' has no len()
Traceback (most recent call last):
File "send_email.py", line 47, in send_email
server.sendmail(sender, to_list, msg.as_string())
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 878, in sendmail
if len(senderrs) == len(to_addrs):
TypeError: object of type 'filter' has no len()
Here is the code I am using -
*** send_email.py ****
import sys, os.path, logging
from jinja2 import Environment, PackageLoader
from os import path
def render_template(template, **kwargs):
''' renders a Jinja template into HTML '''
# check if template exists
if not os.path.exists(template):
print('No template file present: %s' % template)
sys.exit()
import jinja2
templateLoader = jinja2.FileSystemLoader(searchpath="/")
templateEnv = jinja2.Environment(loader=templateLoader)
templ = templateEnv.get_template(template)
return templ.render(**kwargs)
#-----------------------------------------------------------
def send_email(to, sender='jk<jk@gmail.com>', cc=None, bcc=None, subject=None, body=None):
''' sends email using a Jinja HTML template '''
import smtplib
# Import the email modules
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
from email.mime.base import MIMEBase
from email import encoders
# convert TO into list if string
if type(to) is not list:
to = to.split()
to_list = to + [cc] + [bcc]
to_list = filter(None, to_list) # remove null emails
msg = MIMEMultipart('alternative')
msg['From'] = sender
msg['Subject'] = subject
msg['To'] = ','.join(to)
msg['Cc'] = cc
msg['Bcc'] = bcc
msg.attach(MIMEText(body, 'html'))
server = smtplib.SMTP_SSL('smtp.gmail.com') # or your smtp server
try:
server.login('jk@gmail.com', 'password')
logging.info('sending email xxx')
server.sendmail(sender, to_list, msg.as_string())
except Exception as e:
logging.error('Error sending email')
logging.exception(str(e))
finally:
server.quit()
#-----------------------------------------------------------------------------------
# MAIN
item1 = 'kryptonite'
item2 = 'green clothing'
# generate HTML from template
html = render_template('default.j2', vars=locals())
to_list = ['xyz@gmail.com', 'vcb@gmail.com']
sender = 'jk<jk@gmail.com>'
cc = ''
bcc = ''
subject = 'Testing Jinja Email'
# send email to a list of email addresses
# send_email(to_list, sender, cc, bcc, subject, html.encode('utf-8'))
send_email(to_list, sender, cc, bcc, subject, html)
The error is in this line:
to_list = filter(None, to_list) # remove null emails
it should be
to_list = list(filter(None, to_list) # remove null emails
because sendmail expects an actual list
, not the iterator type returned by filter()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.