Send email
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg["From"] = FROM_EMAIL
msg["Subject"] = "Subject"
msg["To"] = TO_EMAIL
msg.set_content("This is the message body")
msg.add_attachment(open(filename, "r").read(), filename="log_file.txt")
s = smtplib.SMTP('smtp.sendgrid.net', 587)
s.login(USERNAME, PASSWORD)
s.send_message(msg)
Even better is to install library envelope
by pip3 install envelope
that's aim is to handle many things in a very intuitive manner:
from envelope import Envelope
from pathlib import Path
Envelope()\
.from_(FROM_EMAIL)\
.subject("Subject")\
.to("to")\
.message("message")\
.attach(Path(filename))\
.smtp("smtp.sendgrid.net", 587, USERNAME, PASSWORD)\
.send()