0

I have class to send emails. I'm trying to send an attachment of a zip file that is around 157 mb but I'M getting a memory error. When i send other smaller zip files the program works fine. Does anyone knows how to handle this issue so i can can send attachment similar to the ones that is causing the problem?

class Sender: 
#constructor
def __init__(self,filename):
    self.filename = filename    

#functions
def message(self):
    fromaddr = "myaddress@gm.com"
    toaddr = ["address"]        
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = ",".join(toaddr)
    msg['Subject'] = "New Base Year"

    # This is the binary part(The Attachment):
    part = MIMEApplication(open('t:\\base_year_06_06_2014.zip',"rb").read())
    part.add_header('Content-Disposition', 'attachment', filename='srping2013.zip')

    msg.attach(part)        
    body = "Hello,"
    msg.attach(MIMEText(body, 'plain'))
    server = smtplib.SMTP('c11', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login("myaddress@gm.com", "password")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

    return "Message Sent"

And this is the error i get when trying to send the zip file

Traceback (most recent call last):
File "C:\Python27\ArcGIS10.1\Lib\site- packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "T:\Python scripts\scripts\test\zipper.py", line 58, in <module>
success = send_email.message()
File "T:\Python scripts\scripts\test\zipper.py", line 36, in message
text = msg.as_string()
File "C:\Python27\ArcGIS10.1\lib\email\message.py", line 137, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 83, in flatten
self._write(msg)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 108, in _write
self._dispatch(msg)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 134, in _dispatch
meth(msg)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 203, in _handle_multipart
g.flatten(part, unixfrom=False)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 83, in flatten
self._write(msg)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 118, in _write
self._fp.write(sfp.getvalue())
MemoryError: out of memory
Daniel
  • 5,095
  • 5
  • 35
  • 48

1 Answers1

0

Unfortunately, it's all based on how much memory your code is allocated and what memory limits you have physically as well as on the email server.

You can try splitting the file into pieces and then having them put back together on the other end, but that's hardly an ideal solution. Compression techniques could also help, but only in a very small way.

Another, very involved solution is, if you have access to the server running the mailserver, you could temporarily write the attachment to the server, and leave a link to it in the email, allowing the user to download larger attachments that are above a certain size off of the server itself.

Aeolingamenfel
  • 2,399
  • 1
  • 15
  • 22
  • It's a 157mb file on a system with 32gb of ram. I find it unlikely that he's actually running into the memory limits of his system. – Magitrek Jun 06 '14 at 17:47
  • Ill try to write it to the server. Thanks – Daniel Jun 06 '14 at 17:47
  • I wasn't aware of how large his RAM is when I wrote this, but you're right. There are no physical restrictions to this. There may be some limits on your python runtime, however. Take a look at [this](http://stackoverflow.com/questions/16779497/how-to-set-memory-limit-for-thread-or-process-in-python) for more information – Aeolingamenfel Jun 06 '14 at 17:50
  • Then it's as I was suspecting. There are two problems causing that then: 1) (unlikely) the server's memory is very low or is mostly consumed by other running applications and cannot load its buffer, or 2) the mail server automatically throws an error if you hit its set limit on attachment size – Aeolingamenfel Jun 06 '14 at 17:59