Open VolodyaCO opened 2 years ago
This is a little hard to follow for me and neither of the examples that were provided are complete (able to be run on their own without further modifications). Would you be able to provide the smallest piece of code that reproduces the issue?
Ok this is an attempt of a somewhat reproducible code:
import smtplib, ssl
import glob
from email.message import EmailMessage
import mimetypes
port = 465 # For SSL
message = EmailMessage()
message["Subject"] = "Some subject"
message["From"] = sender_email
message["To"] = receiver_email
password="your password"
context = ssl.create_default_context()
msg_content = """
<html><body>
Hello world
</body></html>"""
content = bytes(msg_content, "utf-8")
ctype, encoding = mimetypes.guess_type("second.html")
maintype, subtype = ctype.split("/", 1)
message.add_attachment(
content,
maintype=maintype,
subtype=subtype,
filename="second.html",
)
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login(sender_email, password)
server.send_message(message)
print("Sent!")
You need to input the sender, receiver and password for gmail.
Can you show the generated message's as_string()
representation, ideally with a diff between a working and a non-working example? Does it work if you hardcode the text/html
type instead of having Python guess the type?
The MWE does not produce the described failure, neither for 3.9.10 nor for 3.10.13.
I suggest closing this issue.
Bug report
I am trying to send a 1.5 MB .html file using
from email.message import EmailMessage
through theadd_attachment
method. If thefilename
ends in ".html", the email will not be sent, nor any warning/exception is raised.This is the code that I am using:
You see that I have two filenames, one is filename and the other one is mapname. filename is a csv file and mapname is an html file. If I only use the filename, the mail is sent. If I add mapname, the mail is not sent. If I only use mapname, the email is not sent either.
If the filename is changed:
f"{i}_{fn.split('.')[0]}"
, so that the extension is removed, the email is sent! This shows that the HTML is not corrupt, it's just because of the extension! Also, I tried sending a simple html such as this one:And it still does not work.
Your environment
I'm using a virtual environment with Python version
Python 3.9.10
. The architecture isarm64
.pip freeze
shows: