title: How encoding correctly for mailing with smtplib in Python
body: <p>I'm trying to make a python script to verify some file and send an email if there is some change.</p>
  
  <p>The script is working good but if I wrote some special caracters, he won't send the mail...</p>
  
  <p>I've looked website for encoding but I can not solve my problem.</p>
  
  <p>Here is my script :</p>
  
  <pre><code>#!/usr/bin/env python3
  # -*- coding:Latin-1 -*-
  
  import os
  import shutil
  import smtplib
  
  # Fonctions
  def compare (old,new):
          x = 0
          if old == new:
                  print('Les fichiers sont à jour')
                  x = 0
          if old != new:
                  print('Nouveaux Fichiers de tests')
                  x = 1
                  mail = open('mail.txt', 'w', encoding='utf8')
                  mail.write("test d'écriture\n")
                  mail.write("si ca marche c'est top\n")
                  mail.close()
          return x
  
  # Récupération des fichiers
  os.chdir("/var/www/corealpi/integration/integration_core")
  rep = os.getcwd()
  print(rep)
  
  # copie du fichier tests.txt
  shutil.copyfile('tests.txt','/home/git/integration/tests.new')
  
  os.chdir("/home/git/integration")
  rep_int = os.getcwd()
  print(rep_int)
  
  # Ouverture des fichiers
  oldFile = open('tests.old','r')
  newFile = open('tests.new','r')
  tOld = oldFile.read()
  tNew = newFile.read()
  
  verif = compare (tOld,tNew)
  print(verif)
  # Envoie de mails
  if verif == 1:
  
          from email.mime.text import MIMEText
          mailfile = 'mail.txt'
          with open(mailfile) as fp:
                  # Create a text/plain message
                  msg = MIMEText(fp.read())
                  msg.set_charset('utf-8')
          msg['Subject'] = 'Nouveaux Fichiers de %s' % mailfile
          msg['From'] = 'sender@mail.fr'
          msg['To'] = 'receiver@mail.fr'
  
          # Envoi du message
          s = smtplib.SMTP('192.168.3.2')
          s.send_message(msg)
          s.quit
  else:
          print('Rien à signaler')
  </code></pre>
  
  <p>I encode in utf8... but it seems not work with <code>send_message</code> ? but perhaps I confused me or that I am too complicated life.</p>
  
  <p>If someone can help me please ?</p>
  
  <p><strong>EDIT :</strong> I've tried to encoding in many way but I can't solve this problem... Please heeelp :)</p>
  
  <p>Thanks for help.</p>