1

我知道这行得通,因为我有 django-registrations 应用程序发送激活电子邮件。但是我没有运气为另一个应用程序重新创建此功能。在views.py的add_article函数中,我执行了celery任务send_published_article.delay(request.user.email, subject, body),这在本地工作。但是当我部署到 Heroku 时,当我将 is_published 设置为 true 时,不会发送任何电子邮件。没有产生错误,所以我不知道从哪里开始寻找。

Heroku 日志显示了通过电子邮件发送 POST 的失败尝试:2013-04-30T20:57:55.650246+00:00 heroku[router]: at=info method=POST path=/story/add/article host=secure-brook- 2084.herokuapp.com fwd="108.167.16.106" dyno=web.1 连接=0ms 服务=265ms 状态=302 字节=5

任务.py

from django.core.mail import send_mail
from celery import task
from apps.profiles.models import Client

@task(name='send-email')
def send_published_article(sender, subject, body):
    for client in Client.objects.all():
        recipient = client.email
        send_mail(subject, body, sender, [recipient], fail_silently=False)

视图.py

import os
import redis
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.shortcuts import redirect, render_to_response
from django.template import RequestContext
from models import Article
from forms import ArticleForm
from story.tasks import send_published_article

@login_required 
def add_article(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST, request.FILES or None)
        if form.is_valid():
            article = form.save(commit=False)
            article.author = request.user
            article.save()
            msg = "Article saved successfully"
            messages.success(request, msg, fail_silently=True)
            if article.is_published:
                subject = article.title
                body = article.text
                send_published_article.delay(request.user.email, subject, body)
                msg = "Article saved and published successfully"
                messages.success(request, msg, fail_silently=True)
            return redirect(article)
    else:
        form = ArticleForm()
    return render_to_response('story/article_form.html', 
                              { 'form': form },
                              context_instance=RequestContext(request))

我已经在我的 settings.production 中设置了这样的设置:

EMAIL_BACKEND = 'django_ses.SESBackend'
DEFAULT_FROM_EMAIL = 'ME <my.email@email.com>'

如果您需要更多信息,或者对帮助查找错误的测试有想法,请告诉我。

谢谢,

4

1 回答 1

4

这个问题的解决方法很简单,让我想哭。我的 Heroku 应用程序中没有启用 celeryd 和 celerybeat dynos。打开它们,应用更改,然后发送电子邮件。

于 2013-05-01T13:52:14.343 回答