Send Excel Attachment by Email Using Django

Here is a snippet for sending an attachment by email in Django:

import base64
import os

from django.core.mail import EmailMessage


def send_mail_w_attachment(to, subject, message, attachment_path,
                           mimetype='application/octet-stream', fail_silently=False):
    """
    :param to: a list of email addresses
    :param subject: a string
    :param message: a string
    :param attachment_path: full path to attachment fil
    """
    email = EmailMessage(subject, message, to=to)

    # Load file and Base64 encode
    file_name = os.path.basename(attachment_path)
    data = open(attachment_path, 'rb').read()
    encoded = base64.b64encode(data)
    email.attach(file_name, encoded, mimetype=mimetype)

    # Send it
    email.send(fail_silently=fail_silently)

Django: Login Using Email and the AllAuth Package

I just started using Django-AllAuth. I chose it because it supports “normal” login as well as Oauth based login.

For “normal” login, I have switched to using email address as the username. In general, people do not like having to think up a username and they do not remember it. Email is already unique and easier to remember because the user uses it all the time.

AllAuth says it can do email based logins. But how? Turns out its just a setting:

ACCOUNT_AUTHENTICATION_METHOD  = “email”