title: How to send email with embedded image file in Python without using smtplib?
body: <p>I am currently working on a project in which I build a mail client using Python's default library and socket programming (MAIL FROM, RCPT TO, DATA, etc.) instead of smtplib.  I've tried sending an image as binary over the socket connection:</p>
  
  <pre><code>if file_path != '':
      imageFile = open(file_path, 'rb')
      client_socket.send(imageFile.read())
  </code></pre>
  
  <p>This didn't work.  I then tried to make a MIME message that I could send without smtplib:</p>
  
  <pre><code>def create_message(from_var, to_var, cc_var, subject_var, message, file_path):
      # Create the root message and fill in the from, to, and subject headers
      msgRoot = MIMEMultipart('related')
      msgRoot['Subject'] = subject_var
      msgRoot['From'] = from_var
      msgRoot['To'] = to_var
      msgRoot.preamble = 'This is a multi-part message in MIME format.'
  
      # Encapsulate the plain and HTML versions of the message body in an
      # 'alternative' part, so message agents can decide which they want to display.
      msgAlternative = MIMEMultipart('alternative')
      msgRoot.attach(msgAlternative)
  
      msgText = MIMEText(message)
      msgAlternative.attach(msgText)
  
      # We reference the image in the IMG SRC attribute by the ID we give it below
      msgText = MIMEText(message + '&lt;img src="cid:' + file_path + '"&gt;', 'html')
      msgAlternative.attach(msgText)
  
      # This example assumes the image is in the current directory
      fp = open(file_path, 'rb')
      msgImage = MIMEImage(fp.read())
      fp.close()
  
      # Define the image's ID as referenced above
      msgImage.add_header('Content-ID', '&lt;' + file_path + '&gt;')
      msgRoot.attach(msgImage)
  
      return msgRoot.as_string()
  </code></pre>
  
  <p>Taken from <a href="https://stackoverflow.com/a/920928/2101702">this question</a>.  Unfortunately, that also didn't work, returning this error stack:</p>
  
  <pre><code>Exception in Tkinter callback
  Traceback (most recent call last):
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
      return self.func(*args)
    File "C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py", line 209, in &lt;lambda&gt;
      toVariable.get(), ccVariable.get(), subjectVariable.get(), textEntry.get('1.0', 'end-1c'), gFilePath))
    File "C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py", line 164, in validate_input
      send_mail(mail_server, from_val, to_arr, cc_arr, subject_val, message, val_filePath)
    File "C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py", line 117, in send_mail
      client_socket.send(create_message(from_var, to_var, cc_var, subject_var, message, file_path))
    File "C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py", line 50, in create_message
      return msgRoot.as_string()
    File "C:\Python27\lib\email\message.py", line 137, in as_string
      g.flatten(self, unixfrom=unixfrom)
    File "C:\Python27\lib\email\generator.py", line 83, in flatten
      self._write(msg)
    File "C:\Python27\lib\email\generator.py", line 115, in _write
      self._write_headers(msg)
    File "C:\Python27\lib\email\generator.py", line 164, in _write_headers
      v, maxlinelen=self._maxheaderlen, header_name=h).encode()
    File "C:\Python27\lib\email\header.py", line 410, in encode
      value = self._encode_chunks(newchunks, maxlinelen)
    File "C:\Python27\lib\email\header.py", line 370, in _encode_chunks
      _max_append(chunks, s, maxlinelen, extra)
    File "C:\Python27\lib\email\quoprimime.py", line 97, in _max_append
      L.append(s.lstrip())
  AttributeError: 'list' object has no attribute 'lstrip'
  </code></pre>
  
  <p>I'm using Tkinter as a GUI, but I think the problem begins when I call msgRoot.as_string().  I'm just lost at this point so any help would be greatly appreciated.</p>