Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I noticed in the penultimate line of your 2nd sample code, you call the <code>msg_atch.strip()</code> function. The problem with this is that there <em>isn't</em> any function <code>strip()</code> of <code>MIMEBase</code>.</p> <p>What you probably want to do is something along the lines of this:</p> <pre><code>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) </code></pre> <p>The <code>MIMEBase._payload</code> string is the actual (in this case, base64) content used by the attachment.</p> <p>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 <code>msg_atch._payload = msg_atch._payload.rstrip('\n')</code>.</p> <p>Keep in mind that the header of the attachment (<code>Content-Type: application/octet-stream</code> and <code>MIME-Version: 1.0</code> are parts) <strong>require</strong> these newlines.</p> <p>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.</p> <p>Happy Coding!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload