Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6911647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:59:03+00:00 2026-05-27T08:59:03+00:00

I have the following code which works fine, but it doesn’t send the attachment

  • 0

I have the following code which works fine, but it doesn’t send the attachment files.

import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.MIMEBase import MIMEBase
from email import Encoders

msg=MIMEMultipart()

def mymail(address,body,format,mylist=None):

    msg['To']=address
    msg['From']='ggous1@gmail.com'
    if format=='txt':
        text_msg=MIMEText(body,'plain')
    elif format=='html':
        text_msg=MIMEText(body,'html')
    msg.attach(text_msg)
    if mylist is not None:
        mylist=[]
        fn=[]
        for f in range(len(mylist)):
            direct=os.getcwd()
            os.chdir(direct)
            part=MIMEBase('application','octet-stream')
            part.set_payload(open(mylist[f],'rb').read())
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(mylist[f])) 
            fn.append(part)
            msg.attach(fn)

    srv=smtplib.SMTP('smtp.gmail.com')
    srv.set_debuglevel(1)
    srv.ehlo()
    srv.starttls()
    srv.ehlo()
    srv.login('username','pass')
    srv.sendmail(msg['From'],msg['To'],msg.as_string())
    srv.quit()

if __name__=="__main__":
    address=raw_input('Enter an address to send email in the form "name@host.com" ')
    body=raw_input('Enter the contents of the email')
    format=raw_input('The format is txt or html?')
    question=raw_input('Do you have any files to attach?Yes or No?')
    mylist=[]
    if question=='Yes' or question=='yes':
        fn=raw_input('Enter filename')
        mylist.append(fn)

    mymail(address,body,format,mylist)

Am I not using MIMEBase right, or do I have an error in my code?

UPDATE————————

 if mylist is not None:
        mylist=[]
        fn=[]
        for f in range(len(mylist)):
            direct=os.getcwd()
            os.chdir(direct)
            fn[f]=open(mylist[f],'r')             
            part=msg.attach(MIMEApplication(fn[f]))
            mylist.append(part)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:59:04+00:00Added an answer on May 27, 2026 at 8:59 am

    I would recommend to use MIMEApplication instead for the attachment. You also do not need to do all the payload encoding manually since that is already done automatically. This example works for me:

    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    from email.utils import formataddr
    from email.utils import make_msgid
    from email.utils import formatdate
    
    email = MIMEMultipart()
    email['From'] = formataddr(('Jane Doe', 'jane@example.com'))
    email['Subject'] = u'Test email'
    email['Message-Id'] = make_msgid()
    email['Date'] = formatdate(localtime=True)
    email.attach(MIMEText(u'This is your email contents.'))
    email.attach(MIMEApplication('your binary data'))
    print email.as_string()
    

    Note that I’m also taking care to set a proper Date and Message-Id header here.

    Applying that to your code (and doing a few small cleanups) I get the following working code:

    import smtplib
    import os
    from email.mime.multipart import MIMEMultipart
    from email.mime.application import MIMEApplication
    from email.mime.text import MIMEText
    from email.utils import make_msgid
    from email.utils import formatdate
    
    
    def make_mail(address,body,format,mylist=[]):
        msg = MIMEMultipart()
        msg['To'] = address
        msg['From'] = 'ggous1@gmail.com'
        msg['Message-Id'] = make_msgid()
        msg['Date'] = formatdate(localtime=True)
        msg.attach(MIMEText(body, 'plain' if format == 'txt' else 'html'))
        for filename in mylist:
            part = MIMEApplication(open(filename).read())
            part.add_header('Content-Disposition',
                    'attachment; filename="%s"' % os.path.basename(filename))
            msg.attach(part)
        return msg    
    
    def send_mail(msg):
        srv = smtplib.SMTP('smtp.gmail.com')
        srv.set_debuglevel(1)
        srv.ehlo()
        srv.starttls()
        srv.ehlo()
        srv.login('username','pass')
        srv.sendmail(msg['From'], msg['To'], msg.as_string())
        srv.quit()
    
    if __name__=="__main__":
        address=raw_input('Enter an address to send email in the form "name@host.com" ')
        body=raw_input('Enter the contents of the email')
        format=raw_input('The format is txt or html?')
        question=raw_input('Do you have any files to attach?Yes or No?')
        mylist=[]
        if question=='Yes' or question=='yes':
            fn=raw_input('Enter filename')
            mylist.append(fn)
    
        msg = make_mail(address,body,format,mylist)
        send_mail(msg)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which works fine. However, I only want to return
At the moment I have the following code which works fine. label = new
I have the following code which works just fine when the method is POST,
I have the following code for Android which works fine to play a sound
I have code that looks like the following, which works fine for displaying the
I have the following code which works perfectly, but I want to allow access
I have the following PHP code which works out the possible combinations from a
I have the following code, which works fine: <input type=button name=btnHello value=Hello onclick=Test();/> and
I have the following code which runs fine on my computer but fails online:
I have the following code which works fine on my Windows 2003 server: static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.