Django易于使用的基于类的电子邮件

django-generic-mail的Python项目详细描述


易于使用,django基于类的电子邮件。

问题

我们的django项目没有统一的方式发送电子邮件。Django自己的电子邮件功能需要太多样板文件 代码和不够智能。它们也不是基于阶级的。

发送电子邮件应该与创建电子邮件类的实例、提供一段文本和调用 send()方法。这是最简单,最常见的情况。也可以覆盖基本模板, 提供单独的文本和HTML版本,将邮件发送到队列等。

这门课对你有什么帮助

  • 如果:
    1. 有一个自定义文本模板,或
    2. 使用默认模板时,如果设置了文本体
  • 如果:
    1. 有一个自定义HTML模板,或
    2. 当使用默认模板时,如果设置了html_body
  • 当只有HTML实体时,也可以强制发送文本电子邮件。然后它将为您将HTML转换为文本。
  • 当只有文本体时,也可以强制发送HTML电子邮件。然后它会为您将文本转换为html。

它不会做什么

这个电子邮件类不知道邮件队列。您可以使用自定义电子邮件后端或类似的项目 https://github.com/jtauber/django-mailer为了这个。

安装

$ pip install django-generic-mail

用法

from generic_mail import Email

# This will send text email only, uses the email/base_text_email.html template with the "body" template variable
email = Email('to@example.com', 'Subject', 'Line one\n\nLine two')
email.send()

# This will send text- and html email, also with default template
email = Email('to@example.com', 'Subject', 'Line one\n\nLine two', '<p>Line one</p><p>Line two</p>')
email.send()

# This will send text- and html email, will convert text to html using Markdown
email = Email('to@example.com', 'Subject', 'Line one\n\nLine two')
email.send(text=True, html=True)

# This will send html email only
email = Email('to@example.com', 'Subject', html_body='<p>Line one</p><p>Line two</p>')
email.send()

# This will send text- and html email, will convert html to text by removing html tags, converting paragraphs and breaks
email = Email('to@example.com', 'Subject', html_body='<p>Line one</p><p>Line two</p>')
email.send(text=True, html=True)

# This will send text email only, since there is no body given, the text must be in the templates
email = Email('to@example.com', 'Subject', text_template='email/my_text_email.html')
email.send()

# This will send html email only, since there is no body given, the text must be in the templates
email = Email('to@example.com', 'Subject', html_template='email/my_html_email.html')
email.send()

# This will send text- and html email, since there is no body given, the text must be in the templates
email = Email('to@example.com', 'Subject', text_template='email/my_text_email.html', html_template='email/my_html_email.html')
email.send()

# This will generate an error: when proving only one custom template, you can't send both email versions
email = Email('to@example.com', 'Subject', html_template='email/my_html_email.html')
email.send(text=True, html=True)

# This will generate an error: when using default templates, you need to give at least one body (text or html)
email = Email('to@example.com', 'Subject')
email.send()

子类化

如果您想创建自己的子类,它具有不同的默认值(其他模板、主题等),最好的方法是 这是通过更改类属性而不是重写__init__方法实现的。

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

推荐PyPI第三方库


热门话题
java如何将jasper集成到jhipster项目中   java无法忽略lombok注释   关于tomcat日志的java问题   java@Autowired未设置字段>NullPointerException   GUI提交按钮不工作   java气泡和选择排序   java如何编写规则来匹配两个数组?   java如何找出某个字符在字符串中的第一次、第二次或第三次出现?   java通过字符串引用id   javascript在网络视图中加载在线图表   java保留web应用程序中用户更改的日志   在安卓中尝试使用Mandrill SMTP发送电子邮件时出现java错误   用java语言将a2b4c5等字符串转换为AABBCCCCC的程序是什么?   java无需TODO即可删除所有注释   java JMX MBean在应用程序部署时自动注册   java如何使用JSON解析从任何url解析数据   java@transactional注释类使用代理包装,但未创建事务   JavaFx转换和打印