Reputation: 55
I'm trying to attach html into emails with a python script. I've been able to send them successfully however wanted to have more organized code. I created a function that has the HTML inside it as a string. However there seems to be an encoding issue when I attach it onto an email. I need help to figure where I need to set the encoding.
I read some other posts regarding this same error message and they all seem to add this line of code onto their string.
encode('utf8')
So I have tried attaching this onto where I think it needs to go, but have not been able to find any success.
This is what I have
def EmailTemplate():
test = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
def SendEmail(me, you):
# me == my email address
# you == recipient's email address
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "You've gone over your credit limit"
msg['From'] = me
msg['To'] = you
# Create the body of the message (a plain-text and an HTML version).
text = ''
html = EmailTemplate()
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
This code worked when I had the HTML as a string. However I now added a function to try and do the same thing.
For Example this is what i had before and worked
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
So I tried to replicate that with the following.
html = EmailTemplate()
Then the Function is
def EmailTemplate():
test = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
I Expected the email to send as normal, since the function is the same thing. However i get this for the error message.
File "H:\Files\Projects\Python\Test\Htmlemail.py", line 34, in SendEmail
part2 = MIMEText(html, 'html')
File "C:\Users\vanle\AppData\Local\Programs\Python\Python37-32\lib\email\mime\text.py", line 34, in __init__
_text.encode('us-ascii')
AttributeError: 'NoneType' object has no attribute 'encode'
So I tried to add the encoding to the following line of code
part2 = MIMEText(html.encode('utf8'), 'html')
However i still get this error message
File "H:\Files\Projects\Python\Test\Htmlemail.py", line 34, in SendEmail
part2 = MIMEText(html.encode('utf8'), 'html')
AttributeError: 'NoneType' object has no attribute 'encode'
Upvotes: 0
Views: 2037
Reputation: 317
Your EmailTemplate
function has no return statement and so is assigning None
to your variable html
. Adding return test
to the end of the EmailTemplate
definition should solve this.
def EmailTemplate():
test = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
return test
Upvotes: 1