使用python发送电子邮件时的特殊字符

时间:2019-04-09 09:12:55

标签: python

我正在使用python发送电子邮件,如下所示:

if myresult > 0:
   remetente    = 'xxxxxxxx@gmail.com'
   senha        = 'xxxxx'

   destinatario = ['xxxxx@xxxxxxx.xx']
   assunto      = 'Encomendas'
   texto        = 'Existem Encomendas na plataforma aguardar decisão.'

   msg = '\r\n'.join([
      'From: %s' % remetente,
      'To: %s' % destinatario,
      'Subject: %s' % assunto,
      '',
      '%s' % texto
   ])

   server = smtplib.SMTP('smtp.gmail.com:587')
   server.starttls()
   server.login(remetente,senha)
   server.sendmail(remetente, destinatario, msg)
   server.quit()

电子邮件发送没有问题,但是由于使用了“decisão” 这个字眼的~,用户收到的电子邮件有误。

发给用户的电子邮件中的内容:decisão 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

尝试一下:

  

texto = texto.encode('utf-8'),如果isinstance(texto,unicode)否则为texto