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 6859103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:14:31+00:00 2026-05-27T02:14:31+00:00

I am programming with python. I already have a function that sends mails with

  • 0

I am programming with python. I already have a function that sends mails with attachments, but the problem is that it takes the message and puts it as an attachment. I need that it respects the message as message and the attachment as attachment. I have investigated and I found that has to do with MIME Multipart “MIXED” but i don’t know how to add or change this to my actual functions.

Here is the python code of the function I am using:

def enviarCorreo(fromaddr, toaddr, file):
    msg = MIMEMultipart('mixed')
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = 'asunto'
    #adjunto
    adjunto = MIMEBase('application', "octet-stream")
    adjunto.set_payload(open(file, "rb").read())
    encode_base64(adjunto)
    adjunto.add_header('Content-Disposition', 'attachment; filename= "%s"' % file)
    msg.attach(adjunto)
    #enviar
    server = smtplib.SMTP('localhost')
    server.set_debuglevel(1)
    server.sendmail(fromaddr, toaddr, msg.as_string())
    server.quit()
    return
  • 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-27T02:14:32+00:00Added an answer on May 27, 2026 at 2:14 am

    You forgot to attach the text as msg.attach( MIMEText(text) )

    def enviarCorreo(fromaddr, toaddr, text, file):
        msg = MIMEMultipart('mixed')
        msg['From'] = fromaddr
        msg['To'] = toaddr
        msg['Subject'] = 'asunto'
    
        #This is the part you had missed.
        msg.attach( MIMEText(text) )
    
        #adjunto
        adjunto = MIMEBase('application', "octet-stream")
        adjunto.set_payload( open(file,"rb").read() )
        Encoders.encode_base64(adjunto)
        adjunto.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
        msg.attach(adjunto)
    
        server = smtplib.SMTP('localhost')
        server.set_debuglevel(1)
        server.sendmail(fromaddr, toaddr, msg.as_string())
        server.close()
    
    enviarCorreo("x@from.com", ["y@to.com"], "Hello World", ['/tmp/sample.png'])
    

    See if this works for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am programming with Python. I already have a function that sends an email
first of all I'm new to python and programming but you guys already helped
I have been programming in Python, PHP, Java and C for a couple or
I'm pretty new to Python programming and would appreciate some help to a problem
I'm currently working on the topic of programming-languages and interpreter-design. I have already created
We have a Linux application that makes use of OpenSSL's Python bindings and I
What are some new and exciting programming languages? I have already looked at ruby
I am new to socket programming and ran into a puzzling problem: I have
For a class project I'm trying to do some socket programming Python but running
I am programming in python and need to access the name I have given

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.