Hi Guys,
I’ve been working on a script that sends an email through python within Blender.
I could get one simple version that works well without the feature of sending attachments. But now, I need to be able to send attachments as well.
Outside Blender, the script works well. But when I am inside Blender and run the script it gives me some errors but I can’t figure out how to fix it.
Im using a 33775 branch for mac os x.
Would anybody have any idea how to make it work?
Thanks for your attention in advance.
Rodrigo R. Guimaraes
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os
gmail_user = "[email protected]" # Change this email for you gmail
gmail_pwd = "blabla" # Your email's password
def mail(to, subject, text, attach):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP('smtp.gmail.com:587')
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
#Sending Email
print ("sending email")
mail("[email protected]", "Hello from python!", "This is a email sent with python", "/image.jpg") ### Replace [email protected] for the person that you want to send an email and the last arg: "/image.jpg" for a image in your HD to attach image.
The error that I get:
sending email
Traceback (most recent call last):
File “Text”, line 38, in <module>
File “Text”, line 31, in mail
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/email/message.py”, line 136, in as_string
g.flatten(self, unixfrom=unixfrom)
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/email/generator.py”, line 76, in flatten
self._write(msg)
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/email/generator.py”, line 101, in _write
self._dispatch(msg)
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/email/generator.py”, line 127, in _dispatch
meth(msg)
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/email/generator.py”, line 181, in _handle_multipart
g.flatten(part, unixfrom=False)
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/email/generator.py”, line 76, in flatten
self._write(msg)
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/email/generator.py”, line 101, in _write
self._dispatch(msg)
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/email/generator.py”, line 127, in _dispatch
meth(msg)
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/email/generator.py”, line 155, in _handle_text
raise TypeError(‘string payload expected: %s’ % type(payload))
TypeError: string payload expected: <class ‘bytes’>
location:<unknown location>:-1
location:<unknown location>:-1