Skip to main content
Question

Sending attached file fails when sent to Box folder via Python app; works when sent to email address

  • May 22, 2025
  • 2 replies
  • 70 views

Forum|alt.badge.img

I am trying to use the SMTP functions within Python to send a file to a Box folder. This works when I use Outlook or AT&T web mail (both sending to my own Box account and the one I need to send to at another University), so I know I am authorized. When I use my app to send a file to my own email address, it works; the file appears in my in-box and is correct.  When I try to send using my Python code, I get:

Your attempt to email file(s) into 'a folder' was not successful. This could be due to the following reasons:
- Your collaboration privilege did not allow you to upload into the folder (I have checked this - I have the privilege and it works using an email client)
- A file of the same name already exists and the folder owner disabled file-overwriting for this folder (I have checked this, there is no such existing file).
- The file you attached exceeded file size limit (It is a small file, a few kB).
- The owner of this folder does not have sufficient space in their account (my University account has unlimited storage).

- You did not attach a file (yes, it is there; when I change nothing but the destination email and send it to my own account, the file goes through and is correct).

 

Is this related to the MIMEType or something like that? I would rather not use the Box API if I can avoid it, because this should be dirt-simple. What is the email handler in the Box system looking for that I am not providing?

CODE
import smtplib, tarfile
import sys, getopt, os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from mimetypes import guess_type
from email.encoders import encode_base64
 
 
def main(argv):
  username = ''
  password = ''
  thePath = ''
 
  try:
    opts, args = getopt.getopt(argv,"hu:p:s:d:",["uid=","pwd=","source=","dest="])
  except getopt.GetoptError:
    print("options error")
    print 'filesend.py -u <userId> -p <password> -s <sourcepath> -d <destination>'
    sys.exit(2)
  print("#args=" + str(len(opts)))
  if(len(opts) != 4):
   
    print("missing arg(s)")
    print 'filesend.py -u <userId> -p <password> -s <sourcepath> -d <destination>'
    sys.exit(2)
  print("opts: ",opts)
  for opt, arg in opts:
      if opt == '-h':
        print 'filesend.py -u <userId> -p <password> -s <sourcepath> -d <destination>'
        sys.exit()
      elif opt in ("-u", "--uid"):
        username = arg
      elif opt in ("-p", "--pwd"):
        password = arg
      elif opt in ("-s", "--source"):
        thePath = arg
      elif opt in ("-d","--dest"):
        dest = arg
  print("uid = " + username + " pwd =" + password + " sourcepath =" + thePath + " dest=" + dest)
 
  dirList = os.listdir(thePath)
  for theFile in dirList:
    if theFile.endswith('.csv'):
      print("now compress " + theFile)
      cFile = thePath + "/" + theFile + ".tar"
      tar = tarfile.open(cFile, mode="w:gz")
      tar.add(thePath + "/" + theFile)
      tar.close
    #  r = os.system("rm " + thePath + "/" + theFile)
    else:
      cFile = thePath + "/" + theFile
    print"Try to send file: " + cFile
   
 
    sender_email = username
    receiver_email = dest
 
    message = MIMEMultipart()
 
    message["From"] = sender_email
    message['To'] = receiver_email
    message['Subject'] = "Data File from Grape"
 
    body = "Grape file"
    body = MIMEText(body)
    message.attach(body)
 
    filename = cFile
 
    mimetype, encoding = guess_type(filename)
    mimetype = mimetype.split('/',1)
    fp = open(filename, 'rb')
    attachment =  MIMEBase(mimetype[0],mimetype[1])
    attachment.set_payload(fp.read())
    fp.close()
    encode_base64(attachment)
    attachment.add_header('Content-Dispoosition', 'attachment',
                          filename=os.path.basename(filename))
    print('filename basename = ' + filename)
 
    my_message = str(message)
  # email_session = smtplib.SMTP('smtp2go.com',2525)
    email_session = smtplib.SMTP('smtp.mail.yahoo.com',587)
    email_session.starttls()
 
    email_session.login(sender_email,password)
 
    email_session.sendmail(sender_email,receiver_email,my_message)
    email_session.quit()
    print("YOUR MAIL HAS BEEN SENT SUCCESSFULLY")
  # r = os.system("rm " + file)
 
if __name__ == "__main__":
  main(sys.argv[1:])

:

 

 

 

2 replies

Forum|alt.badge.img

Problem found: when sending a file to Box by using email, the email text must have an html header; a plain text text message will cause the attachment to be rejected.


Forum|alt.badge.img

@william, could you share an example of how you make it work? thanks in advance!