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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:53:07+00:00 2026-06-10T05:53:07+00:00

I need to generate a MIME attachment that contains a Base64 encoded file. However

  • 0

I need to generate a MIME attachment that contains a Base64 encoded file. However what I need to also allow for is encoding the attachment WITHOUT any new lines. The code I have is as followed:

msg_obj = MIMEMultipart()
msg_atch = MIMEBase(mime_type, mime_subtype)
msg_atch.set_payload(file_data)
Encoders.encode_base64(msg_atch)
msg_obj.attach(msg_atch)

What I have tried to perform to remove the new lines in the attach base64 message was this:

msg_obj = MIMEMultipart()
msg_atch = MIMEBase(mime_type, mime_subtype)
msg_atch.set_payload(file_data)
Encoders.encode_base64(msg_atch)
msg_atch.strip()
msg_obj.attach(msg_atch)

However this failed to change the results of the data. If anyone has any ideas on how to allow for this, it would be great.

  • 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-06-10T05:53:08+00:00Added an answer on June 10, 2026 at 5:53 am

    I noticed in the penultimate line of your 2nd sample code, you call the msg_atch.strip() function. The problem with this is that there isn’t any function strip() of MIMEBase.

    What you probably want to do is something along the lines of this:

    msg_obj = MIMEMultipart()
    msg_atch = MIMEBase(mime_type, mime_subtype)
    msg_atch.set_payload(file_data)
    Encoders.encode_base64(msg_atch)
    msg_atch._payload = msg_atch._payload.replace('\n','')
    msg_obj.attach(msg_atch)
    

    The MIMEBase._payload string is the actual (in this case, base64) content used by the attachment.

    This code will take the inner content of the MIMEBase attachment and eliminate the extra newlines – including the ones inside to provide nice formatting of base64 text for “human readability” (my question is why they even bother). If you just want to get rid of the newlines at the end, just use msg_atch._payload = msg_atch._payload.rstrip('\n').

    Keep in mind that the header of the attachment (Content-Type: application/octet-stream and MIME-Version: 1.0 are parts) require these newlines.

    Also, try to remember that you shouldn’t normally be editing internal variables in this fashion. However, one of the things I find nice about Python is that there are really no private members of a class, so you can modify whatever you want in a class. We can do whatever we want, especially if it’s a special condition.

    Happy Coding!

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

Sidebar

Related Questions

I need to generate a 9 digit order id without any leading zeroes. What
I need to generate report that will show number of new / changed rows
I need to generate some token that can only take on a range of
I need to generate an editable xml file to supply content to a flash
I need to generate a .PO file from a .properties file using msgcat from
I need to generate real Spotify playlists (not a text string that you can
I need to generate a pdf from a tex file, as a temporary file.
I have an action that returns a Dynamically Generated image however I need the
I need to generate a MS Excel 2007+ file from my application according to
I need to generate a fixed width file with few of the columns in

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.