kootenpv / yagmail

Send email in Python conveniently for gmail using yagmail
MIT License
2.66k stars 265 forks source link

Trouble sending HTML emails #179

Open Destaq opened 4 years ago

Destaq commented 4 years ago

Roughly 50% of the time that emails are sent, the email received is somehow corrupted.

Case in point:

contents = [f"""
    <p>We were asked to email you password reset instructions for Life Calendar. If you don't recall doing this and remember your passwords, you can safely ignore this email.</p>

    <p>If it <strong>was</strong> you, then you can head over <a href="http://localhost:5000/resetpassword/{generatedlink.decode("utf-8")}/">here</a> to reset your password.</p>

    <p>Having trouble clicking the link? Just paste the following link into your browser: http://localhost:5000/resetpassword/{generatedlink.decode("utf-8")}.</p>"""]

Thread(target=self.send_email, args=(email, "Password reset instructions from Life Calendar", contents)).start()
def send_email(self, to, subject, content):
        yag.send(to, subject, content)

This says that it is sent successfully - and it is indeed received. However, an odd error message pops up in the CLI:

 ... raise SMTPDataError(code, repl)
smtplib.SMTPDataError: (250, b'2.0.0 OK  1600629089 c8sm7186465ejp.30 - gsmtp')

Roughly half of the time that the email is sent, it is received strangely:

data
Content-Type: multipart/mixed; boundary="===============3039785278415742229=="
MIME-Version: 1.0
Date: Sun, 20 Sep 2020 19:11:27 -0000
Subject: Password reset instructions from Life Calendar
From: ---
To: ---

--===============3039785278415742229==
Content-Type: multipart/alternative; boundary="===============5120930108217942824=="
MIME-Version: 1.0

--===============5120930108217942824==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

PGJyPiAgICAgICAgICAgICAgICA8cD5XZSB3ZXJlIGFza2VkIHRvIGVtYWlsIHlvdSBwYXNzd29y
ZCByZXNldCBpbnN0cnVjdGlvbnMgZm9yIExpZmUgQ2FsZW5kYXIuIElmIHlvdSBkb24ndCByZWNh
bGwgZG9pbmcgdGhpcyBhbmQgcmVtZW1iZXIgeW91ciBwYXNzd29yZHMsIHlvdSBjYW4gc2FmZWx5
IGlnbm9yZSB0aGlzIGVtYWlsLjwvcD48YnI+PGJyPiAgICAgICAgICAgICAgICA8cD5JZiBpdCA8
c3Ryb25nPndhczwvc3Ryb25nPiB5b3UsIHRoZW4geW91IGNhbiBoZWFkIG92ZXIgPGEgaHJlZj0i
aHR0cDovL2xvY2FsaG9zdDo1MDAwL3Jlc2V0cGFzc3dvcmQvZXlKMGVYQWlPaUpLVjFRaUxDSmhi
R2NpT2lKSVV6STFOaUo5LmV5SnlaWE5sZEY5d1lYTnpkMjl5WkNJNkluQjFjR2xzTG5OcGJXOXVM
bWxzYVc1alpYWkFjR0Z5YTJ4aGJtVXRhWE11WTI5dElpd2laWGh3SWpveE5qQXdOakk1TlRnMkxq
WTRNVEk0TlRsOS5RU3RoejZSN1NpeGJNZlc2TU82dmczeGg0aG5aalZYQnQ1UzBWdEdVczVBLyI+
aGVyZTwvYT4gdG8gcmVzZXQgeW91ciBwYXNzd29yZC48L3A+PGJyPjxicj4gICAgICAgICAgICAg
ICAgPHA+SGF2aW5nIHRyb3VibGUgY2xpY2tpbmcgdGhlIGxpbms/IEp1c3QgcGFzdGUgdGhlIGZv
bGxvd2luZyBsaW5rIGludG8geW91ciBicm93c2VyOiBodHRwOi8vbG9jYWxob3N0OjUwMDAvcmVz
ZXRwYXNzd29yZC9leUowZVhBaU9pSktWMVFpTENKaGJHY2lPaUpJVXpJMU5pSjkuZXlKeVpYTmxk
Rjl3WVhOemQyOXlaQ0k2SW5CMWNHbHNMbk5wYlc5dUxtbHNhVzVqWlhaQWNHRnlhMnhoYm1VdGFY
TXVZMjl0SWl3aVpYaHdJam94TmpBd05qSTVOVGcyTGpZNE1USTROVGw5LlFTdGh6NlI3U2l4Yk1m
VzZNTzZ2ZzN4aDRoblpqVlhCdDVTMFZ0R1VzNUEuPC9wPg==

--===============5120930108217942824==
Content-Type: multipart/related; boundary="===============3397337571496640741=="
MIME-Version: 1.0

--===============3397337571496640741==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

PGRpdj48YnI+ICAgICAgICAgICAgICAgIDxwPldlIHdlcmUgYXN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UZzJMalk0TVRJNE5UbDkuUVN0aHo2UjdT
aXhiTWZXNk1PNnZnM3hoNGhuWmpWWEJ0NVMwVnRHVXM1QS48L3A+PC9kaXY+

--===============3397337571496640741==--

--===============5120930108217942824==--

--===============3039785278415742229==--

Is there any workaround to this or solution I'm missing?

kootenpv commented 4 years ago

I have personally not done much testing with yagmail in threads, it could be related to that. I think it's best to make sure you are only sending 1 mail at a time (only 1 connection to gmail) at a time, maybe that would help.