Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Alternatives? #169

Open
OneAdder opened this issue Apr 10, 2019 · 7 comments
Open

Alternatives? #169

OneAdder opened this issue Apr 10, 2019 · 7 comments

Comments

@OneAdder
Copy link

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?

@ThiefMaster
Copy link
Contributor

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.

@OneAdder
Copy link
Author

Cool, thank you!

@michaelbukachi
Copy link

@OneAdder You can check out the inbuilt email package in the standard library.

@bersace
Copy link

bersace commented Apr 27, 2020

Yes, copy the email implementation from Django

This is what https://github.com/waynerv/flask-mailman do.

@Lvl4Sword
Copy link

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()

@ThiefMaster
Copy link
Contributor

@Lvl4Sword except that this won't handle all the ugly edge cases there are when it comes to emails.

@Lvl4Sword
Copy link

I don't believe it does either, as it was something I provided as a simple example which should work for most basic usecases.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

5 participants