Django的增强电子邮件类

django-enhanced-emails的Python项目详细描述


Django增强电子邮件

电池包括Django的电子邮件。

  • 强大的模板引擎
  • 默认情况下,多部分电子邮件(HTML+文本)
  • Web版本呈现(带管理员)
  • 简易文件附件
  • 还有更多…

开始

简单设置

  • 安装程序包:pipenv install django-enhanced-emails

  • 创建新的电子邮件类:

    # myapp/emails.pyfromenhanced_emailsimportEnhancedEmailclassWelcomeEmail(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
    
  • 实例化电子邮件并发送:

    email=WelcomeEmail(to=[user.email],context={"first_name":user.first_name})email.send()
  • 全部完成!我们的用户收到了如下信息:

    Content-Type: multipart/alternative;
    boundary="===============7747654958126582044=="
    MIME-Version: 1.0
    Subject: hello
    From: hello@oursite.com
    To: user@gmail.com
    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==--
    

高级设置(用于Web版本呈现)

  • enhanced_emails应用程序添加到INSTALLED_APPS

    # settings.pyINSTALLED_APPS=[..."enhanced_emails",...]
  • 将新条目添加到urlpatterns

    # urls.pyurlpatterns=[path("admin/",admin.site.urls),path("emails/",include("enhanced_emails.urls")),...]
  • 使用WebVersionEnhancedEmail而不是EnhancedEmail

    fromenhanced_emailsimportWebVersionEnhancedEmailclassWelcomeEmail(WebVersionEnhancedEmail):subject="Welcome to our site!"html_template="emails/welcome.html"
  • 使用电子邮件模板中的web_url变量:

    <!-- myapp/templates/emails/welcome.html --><strong>Welcome to our site {{ first_name }}!</strong><br/>
    
    Best, The OurSite team<br/><ahref="{{ web_url }}">View in browser</a>
  • 实例化一封电子邮件并发送(请注意,我们现在也需要传递请求):

    email=WelcomeEmail(to=[user.email],context={'first_name':user.first_name},request=request)email.send()
  • 电子邮件在管理员和网站上都可见!
    A sent email in the adminThe web version of the email

开发

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何修复尝试将用户签名到服务器主机时出现的“字符串无法转换为json对象”错误?   控制台Java:不使用新行更新命令行中的文本   java servlet接收xml数据时,出现错误   使用REST API在Bitbucket中复制或复制存储库   java如何在JavaFX中对齐一行?   java如何在活动中显示通过服务获得的数据?   返回BigDecimal作为字符串:返回int   java组织。openqa。硒。InvalidSelectorException:尝试查找元素时选择器无效   java仅在阻塞状态下通知和通知所有影响线程   java JBOSS无法启动部署   java方法的返回值能保证类型安全吗?   JavaeShadoop序列化组织。阿帕奇。hadoop。木卫一。短写失败   java如果我在同一个类上同步了两个方法,它们能同时运行吗?   不使用java数据库的spring分页实现   java如何将字符串切碎成这样的数组?