2

I've wrote a really simple piece of code to send simple string to my gmail, and it works perfectly when using small strings; When I started to work with a bit larger strings I received, instead of a normal string, a bunch of encrypted text.

Someone with this kind of problem? Is it something related with security and is it possible to avoid?

def send_email(sender_email,receiver_email,password,txt, subject) :

    message = MIMEMultipart(txt)
    message["Subject"] = subject
    message["From"] = sender_email
    message["To"] = receiver_email

    # Turn these into plain/html MIMEText objects
    part1 = MIMEText(txt, "plain")
    message.attach(part1)

    context = ssl.create_default_context()

    with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as     server: 
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, txt)

This is what I'm receiving :

TsK6IEFsYXJtZTogKDcyCk1lbnNhZ2VtOiAgJ0FWSVNPIC0gU0VSVk8nClRpcG8gZGUgRXJybzog ICdFJwpTdGF0dXMgcGFyYWRvOiAgJ0gnClByb2NlZGltZW50byBkZSBlbGltaW5hw6fDo286ICAn TycKRXhpYmnDp8Ojb286ICAnQXp1bCcKQ2F1c2E6ICAnTyBzZXJ2byBtb3RvciBlc3TDoSBjYXJy ZWdhZG8gZGUgZm9ybWEgYW5vcm1hbC4nCkHDp8OjbzogICdEZXNsaWd1ZSBvIE5DIGUgYSBtw6Fx dWluYQo=

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • 4
    it's base64, search in google for a decoder – Shinra tensei Jan 31 '19 at 11:05
  • the problem is, i'm assuming that the person who is going to receive the email can't access a decoder, or need to read the text immediately – Henrique Joaquim Jan 31 '19 at 11:09
  • In order to solve this you need to change encoding mechanism for `MIMEText` by default it `base64`. You can get more details for encoding mechanism in [encoders docs](https://docs.python.org/2/library/email.encoders.html#module-email.encoders) – Dmytro Chasovskyi Jan 31 '19 at 11:19
  • 1
    May I ask a question? why do you build the `message` if you don't use it later? Or is it in a different part of the code? – Shinra tensei Jan 31 '19 at 11:26
  • 1
    1: You probably want to use `MIMEText` yes, and 2: `server.sendmail(sender_email, receiver_email, txt)` should be `server.sendmail(sender_email, receiver_email, message)` – Torxed Jan 31 '19 at 11:27
  • If your recipient has an email client newer than circa 1990, it will decode the base64 completely transparently. – tripleee Jan 31 '19 at 11:32
  • Possible duplicate of [How do I determine if an email is Base64 encoded?](https://stackoverflow.com/questions/324980/how-do-i-determine-if-an-email-is-base64-encoded) – tripleee Jan 31 '19 at 11:33

1 Answers1

0

Like Torxed said "You probably want to use MIMEText yes, and 2: server.sendmail(sender_email, receiver_email, txt) should be server.sendmail(sender_email, receiver_email, message)"

This and server.sendmail(sender_email, receiver_email, message.as_string)

"message.as_string"

apparently solved the problem! Thank you