Send e-mail via python with attachments within Blender 2.55

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

this may be dirty, but i think it will explain the issue.

you have a python 3.1x installation unrelated to blender, it contains a folder called lib/email
copy the content from lib/email into your equivalent of blender/2.5x/python/libs/… (and overwrite the supplied email folder)

worked for me. nice little script, i see this has usefulness for sending rendered files if you aren’t at home etc.

I use PHP mostly, I know that the mail() function does not work locally, it only works within the webhost environment (with mail servers, name servers registered).
The best way to send email from your PC, is to use POP3/SMTP style through an existing email account.

which is what he does :
smtp.gmail.com:587’)
passing it an email and password. it works. in blender :slight_smile:

Thanks for replying guys.

Hi Zeffi. I have replaced the lib/email in Blender with my current python’s version but I am still stuck with the issue. Now Blender prints the following message below. I’ve also tried to import smtplib in BLender python shell and it doesn’t even recognize the module…

:frowning:

File “/Applications/Blender/blender.app/Contents/MacOS/2.55/scripts/modules/bpy/utils.py”, line 45, in _test_import
mod = import(module_name)
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/scripts/io/netrender/init.py”, line 35, in <module>
from netrender import model
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/scripts/io/netrender/model.py”, line 20, in <module>
import http, http.client, http.server, urllib
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/http/client.py”, line 69, in <module>
import email.parser
ImportError: Bad magic number in /Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/email/init.pyc
Traceback (most recent call last):
File “Text”, line 1, in <module>
File “/Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/smtplib.py”, line 46, in <module>
import email.utils
ImportError: Bad magic number in /Applications/Blender/blender.app/Contents/MacOS/2.55/python/lib/python3.1/email/utils.pyc

location:<unknown location>:-1

location:<unknown location>:-1


Any idea?

:frowning:

Thanks.

double post

I’m running Python 3.1.3 (r313:86834, Nov 27 2010, 18:30:53) [MSC v.1500 32 bit (Intel)] on win32.
And i use blender revision 34221 of 2.56 from graphicall.org. initially i got that error you where having about


TypeError('string payload expected: %s' % type(payload))
TypeError: string payload expected: &lt;class 'bytes'&gt;

so i copied the email folder from my 3.1.3 python directory ontop of the email folder that resides in blender/2.56/python/libs/… and that works( so maybe install a graphical.org version for osx in a seperate directory to toy around…)
can’t help you any further regrettably… I’m sure some bright spark reading this knows what to do

From a short google i found you are not the only person to stumble upon that problem with 3.1, blender is distributed with a python version that still has that bug…maybe it’s worth a bug report…

http://img155.imageshack.us/img155/4706/likethism.png
click to view larger

Hi Zeffii

I didn’t realize that I needed to have a higher version to copy and paste in blender folder.
My python was 2.6 and now I have updated it to 3.1.3.
Now it’s worked fine. :slight_smile:
I will report this bug.
Thanks a lot for your help.