Giter Club home page Giter Club logo

django-enhanced-emails's Introduction

django-enhanced-emails ๐Ÿ’Œ

๐Ÿ”‹ Batteries-included emails for Django.

  • Powerful templating engine
  • Multipart emails by default (HTML + text)
  • Web version rendering (with admin)
  • Easy file attachment
  • and more ...

Getting started

Simple setup

  • Install the package: pipenv install django-enhanced-emails

  • Create a new email class:

    # myapp/emails.py
    from enhanced_emails import EnhancedEmail
    
    class WelcomeEmail(EnhancedEmail):
        subject = "Welcome to our site!"
        html_template = "emails/welcome.html"
    <!-- myapp/templates/emails/welcome.html -->
    <strong>Welcome to our site {{first_name}}!</strong><br />
    
    Best, The OurSite team
  • Instanciate an email and send it:

    email = WelcomeEmail(
        to=[user.email],
        context={
          "first_name": user.first_name
        }
    )
    email.send()
  • โœจ All done! Our user received something like:

    Content-Type: multipart/alternative;
    boundary="===============7747654958126582044=="
    MIME-Version: 1.0
    Subject: hello
    From: [email protected]
    To: [email protected]
    Date: Wed, 11 Apr 2018 17:13:02 -0000
    Message-ID: <152346678269.275.17989388690220812241@cf7f5f3375c9>
    
    --===============7747654958126582044==
    Content-Type: text/plain; charset="utf-8"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    
    Welcome to our site Elon!
    
    Best,
    The OurSite team
    --===============7747654958126582044==
    Content-Type: text/html; charset="utf-8"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    
    <strong>Welcome to our site Elon!</strong>
    
    Best,
    The OurSite team
    --===============7747654958126582044==--

Advanced setup (for web version rendering)

  • Add the enhanced_emails app to INSTALLED_APPS:

    # settings.py
    INSTALLED_APPS = [
        ...
        "enhanced_emails",
        ...
    ]
  • Add a new entry to urlpatterns:

    # urls.py
    urlpatterns = [
        path("admin/", admin.site.urls),
        path("emails/", include("enhanced_emails.urls")),
        ...
    ]
  • Use WebVersionEnhancedEmail instead of EnhancedEmail:

    from enhanced_emails import WebVersionEnhancedEmail
    
    class WelcomeEmail(WebVersionEnhancedEmail):
        subject = "Welcome to our site!"
        html_template = "emails/welcome.html"
  • Use the web_url variable in the email template:

    <!-- myapp/templates/emails/welcome.html -->
    <strong>Welcome to our site {{ first_name }}!</strong><br />
    
    Best, The OurSite team<br />
    
    <a href="{{ web_url }}">View in browser</a>
  • Instanciate an email and send it (notice that we need to pass the request as well now):

    email = WelcomeEmail(
        to=[user.email],
        context={
          'first_name': user.first_name
        },
        request=request
    )
    email.send()
  • The email is visible in the admin and on the site! โœจ
    A sent email in the admin The web version of the email

Development

  • Deploy: python setup.py sdist && twine upload dist/*

django-enhanced-emails's People

Contributors

dependabot[bot] avatar n6g7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-enhanced-emails's Issues

Support "Open in a browser" links

  • Find a way to re-render sent email over HTTP
  • Embed links into email (probably by adding a context variable)

Ideas

  • when web_version flag is set, template is given an extra browser_url context variable
  • on send, 2 options:
    • store HTML in the database
    • only store context data and path to template, then re-render template on request
      • what if template changes? ๐Ÿค”
# emails.py
class MyEmail(EnhancedEmail):
    html_template = 'emails/my_email.html'
    web_version = True
<!-- emails/my_email.html -->
<a href="{{ browser_url }}">Open in a browser</a>
# urls.py
urlpatterns = [
    path("admin/", admin.site.urls),
    path("email/", include(enhanced_emails.urls)),
]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.