Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email attachment received as 'noname'

The following Python function results in the attachment being named "noname" when it should be "text_file.txt". As you can see I've tried a 2 different approaches with MIMEBase and MIMEApplication. I've also tried MIMEMultipart('alternative') to no avail.

def send_email(from_addr, to_addr_list,
              subject, html_body,plain_text_body,
              login,
              password,
              smtpserver='smtp.gmail.com:587',
              cc_addr_list=None,
              attachment=None,
              from_name=None):

    message=MIMEMultipart()

    plain=MIMEText(plain_text_body,'plain')
    html=MIMEText(html_body,'html') 

    message.add_header('from',from_name)
    message.add_header('to',','.join(to_addr_list))
    message.add_header('subject',subject)

    if attachment!=None:
        #attach_file=MIMEBase('application',"octet-stream")
        #attach_file.set_payload(open(attachment,"rb").read())
        #Encoders.encode_base64(attach_file)
        #f.close()
        attach_file=MIMEApplication(open(attachment,"rb").read())
        message.add_header('Content-Disposition','attachment; filename="%s"' % attachment)
        message.attach(attach_file)


    message.attach(plain)
    message.attach(html)

    server = smtplib.SMTP(smtpserver)
    server.starttls()
    server.login(login,password)
    server.sendmail(from_addr, to_addr_list, message.as_string())
    server.quit()

How I'm calling the function:

send_email(
           from_addr=from_email,
           to_addr_list=["[email protected]"],
           subject=subject,
           html_body=html,
           plain_text_body=plain,
           login=login,
           password=password,
           from_name=display_name,
           attachment="text_file.txt"
           )
like image 976
ChrisArmstrong Avatar asked Dec 30 '13 00:12

ChrisArmstrong


People also ask

What is Noname file in email?

A file with the extension NONAME can be used in Google Photos to hide media from the gallery. To hide photos stored in an Android phone from the Google Photos gallery, they must be placed in a folder with a file that has no file name and has just the extension NONAME or NOMEDIA.

Why are my emails with attachments not being received?

If the e-mail box is full of other e-mail messages and your storage space is limited to only a few megabytes, the attachment cannot be received. E-mail programs and e-mail service providers automatically reject incoming e-mails if there is not enough space available to store the e-mails.

Why is my email not opening attachments?

One of the most common reasons why you can't open an e-mail attachment is because your computer doesn't have the necessary program installed to recognize the file format.


1 Answers

Your header isn't correct. filename is the attribute not a string.

# Add header to variable with attachment file
attach_file.add_header('Content-Disposition', 'attachment', filename=attachment)
# Then attach to message attachment file    
message.attach(attach_file)
like image 112
Puffin GDI Avatar answered Oct 06 '22 19:10

Puffin GDI