我在为django项目设置电子邮件选项时遇到了一些麻烦。 我正在使用邮局作为后端,但我似乎无法排队等待。
views.py:
from django.views.generic import TemplateView
from post_office import mail
class EmailView(TemplateView):
model = Customer
mail.send(
'a.sophiewirth@gmail.com', # List of email addresses also accepted
'your.generic.test.email@gmail.com',
subject='My email',
message='Hi there!',
html_message='Hi <strong>there</strong>!',
)
template_name = 'customers/send_email.html'
settings.py:
# using post office as the default email backend
EMAIL_BACKEND = 'post_office.EmailBackend'
POST_OFFICE = {
'DEFAULT_PRIORITY' : 'now'
}
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "your.generic.test.email@gmail.com"
EMAIL_PORT = 25 # default smtp port
EMAIL_HOST_PASSWORD = "password"
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = 'your.generic.test.email@gmail.com'
urls.py:
from django.conf.urls import patterns, include, url
from customers.views import CustomerList, CreateCustomerView, CustomerUpdateView, EmailView
from . import views
urlpatterns = patterns('',
url(r'^$', CustomerList.as_view(), name="customer-list"),
url(r'^create-customer$', CreateCustomerView.as_view(), name="create-customer"),
url(r'^customer-update/(?P<pk>\d+)$', CustomerUpdateView.as_view(), name="customer-update"),
url(r'^send-email$', EmailView.as_view(), name="send-email"),
)
到目前为止,send_email.html本身是空的,它只包含一个段落,告诉您发送了一封电子邮件。
有谁能告诉我为什么没有发送?另外,如何将电子邮件的收件人作为我要发送邮件的特定用户进入View?
非常感谢:)
答案 0 :(得分:0)
我安装了djcelery和Django celery email。这是一个较旧的项目,我相信不再需要djcelery,但我的设置仍然应该给你一个开始。我的Django设置包括以下内容:
import djcelery
...
djcelery.setup_loader()
EMAIL_BACKEND = 'post_office.EmailBackend'
POST_OFFICE_BACKEND = 'djcelery_email.backends.CeleryEmailBackend'
我的项目还包括一个shell脚本,我运行它来启动和配置celery守护程序。我的项目正在virtualenv中运行,因此您可能不需要其中的一部分。
#!/bin/bash
set -e
# user/group to run as
USER=your_username
GROUP=your_groupname
cd /path/to/virtualenv/
source ./bin/activate
cd /path/to/directory/containing/manage.py/
exec python manage.py celeryd --settings=directory.path.settings.py
运行shell脚本应该会显示正在运行的芹菜进程,您的电子邮件现在应该发送并排队。
答案 1 :(得分:0)
立即删除默认优先级并将 'CELERY_ENABLED': True
, 添加到 POST_OFFICE
设置中,如下所示:
POST_OFFICE = {
'CELERY_ENABLED': True,
}
在此之后,当您启动 celery worker 时,您应该会看到 2 个任务来自
post_office
post_office.tasks.cleanup_expired_mails
post_office.tasks.send_queued_mail