Alternatives? #169
Comments
Yes, copy the email implementation from Django; its dependencies on other Django stuff are basically non-existent, and as far as I know it's the best mail sending implementation that's available for python (and it handles all the nasty corner cases like when non-ascii stuff is involved). Actually, I did exactly this for the project I'm working on about two years ago. Feel free to copy from there (but I'd check if Django fixed anything since then); it'll be even less effort than copying it from Django yourself since the external dependencies in my version are even smaller. |
Cool, thank you! |
This is what https://github.com/waynerv/flask-mailman do. |
You can do this right within the standard library: import smtplib
import ssl
from email.mime.text import MIMEText
sender = 'root@example.com'
the_email_password = 'P@s5w0|^\D'
destination = 'root@example.com'
def email_user(sender, the_email_password, destination):
mail_body = 'lorem ipsum'
email_sender = sender
email_cipher = 'ECDHE-RSA-AES256-GCM-SHA384'
email_server = 'smtp_server'
email_port = 465
# https://support.office.com/en-us/article/Outlook-com-no-longer-supports-AUTH-PLAIN-authentication-07f7d5e9-1697-465f-84d2-4513d4ff0145
# https://en.wikipedia.org/wiki/SMTP_Authentication#Details
email_auth = 'LOGIN'
email_password = the_email_password
email_destination = destination
subject = 'subject'
msg = MIMEText(mail_body, 'plain')
msg['Subject'] = subject
msg['From'] = email_sender
ssl_context = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH)
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context.check_hostname = True
ssl_context.set_ciphers(email_cipher)
ssl_context.options |= ssl.HAS_SNI
ssl_context.options |= ssl.OP_NO_COMPRESSION
# No need to explicitally disable SSLv* as it's already been done
# https://docs.python.org/3/library/ssl.html#id7
# The below options are done so as to force TLS1.2
ssl_context.options |= ssl.OP_NO_TLSv1
ssl_context.options |= ssl.OP_NO_TLSv1_1
ssl_context.options |= ssl.OP_SINGLE_DH_USE
ssl_context.options |= ssl.OP_SINGLE_ECDH_USE
conn = smtplib.SMTP_SSL(email_server, port=email_port,
context=ssl_context)
conn.esmtp_features['auth'] = email_auth
conn.login(email_sender, email_password)
try:
conn.sendmail(email_sender, email_destination, msg.as_string())
finally:
conn.quit() |
@Lvl4Sword except that this won't handle all the ugly edge cases there are when it comes to emails. |
I don't believe it does either, as it was something I provided as a simple example which should work for most basic usecases. |
I really need something to send emails with Flask and I really don't want to program it from scratch.
This package doesn't seem to be maintained. Are there any other options?
The text was updated successfully, but these errors were encountered: