Django.Core.Email导致服务器错误(500)

时间:2016-02-03 20:49:23

标签: python django email

我想在创建并支付新订阅时向客户发送确认电子邮件,但我的Django电子邮件功能导致服务器错误(500)。服务器的错误日志没有提供任何详细信息,但我已将错误隔离到views.py中的subscription.send_subscription_email()函数。

我错过了send_subscription_email()中的内容吗?

models.py

import decimal
import datetime
import hashlib
import random
import re
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template import RequestContext, TemplateDoesNotExist
from django.template.loader import render_to_string
from django.utils import six
from django.dispatch import Signal, receiver
from django.utils import timezone
from datetime import datetime, timedelta
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from decimal import *
from django.utils.encoding import python_2_unicode_compatible

class Subscription(StripeObject):

    sub_user        = models.ForeignKey(Customer)
    creation_date   = models.DateTimeField(auto_now_add=True)
    delivery_date   = models.DateTimeField(blank=True, null=True)
    last_update     = models.DateTimeField(auto_now=True)
    checked_out     = models.NullBooleanField(blank=True, null=True, default=False, verbose_name=_('checked out'))
    weekly_plan     = models.CharField(_('plan'), max_length=150, blank=True, null=True)
    weekly_amt      = models.PositiveSmallIntegerField(blank=True, null=True, default=0)
    price           = models.PositiveSmallIntegerField(blank=True, null=True, default=0)
    apartment       = models.CharField(_('apartment'), max_length=150, blank=True, null=True)

def send_subscription_email(self):
    ctx_dict = {}
    ctx_dict.update({
        'user': self.sub_user,
        'apartment': self.apartment,
        'first_delivery_date': self.delivery_date,
        'weekly_plan': self.weekly_plan,
    })
    subject = render_to_string('templates/subject.txt', ctx_dict)
    from_email = 'xxx@xxxxx.com'
    message_txt = render_to_string('templates/subscription_success_email.txt',ctx_dict)
    email_message = EmailMultiAlternatives(subject,message_txt,from_email,[self.sub_user.email])
    email_message.send()

views.py

 if request.method == "POST":
                form = SubscriptionPaymentForm(request.POST)
                if form.is_valid():
                        token = stripe.Token.create(
                                card = {
                                  "number": form.cleaned_data['number'],
                                  "exp_month": form.cleaned_data['expiration'].month,
                                  "exp_year": form.cleaned_data['expiration'].year,
                                  "cvc": form.cleaned_data['cvc'],
                                  "address_line1": form.cleaned_data['address']
                                },
                        )
                        c = stripe.Customer.retrieve(customer.stripe_id)
                        c.sources.create(source=token.id)
                        try:
                                subscription = c.subscriptions.retrieve(subscription.stripe_id)
                                subscription.plan = subscription.weekly_plan
                                subscription.save()
                        except:
                                sub = c.subscriptions.create(plan=subscription.weekly_plan)
                                subscription.stripe_id = sub.id
                                subscription.save()
                                subscription.send_subscription_email()
                        subscription.checked_out = True
                        subscription.status = "Paid"
                        subscription.save()
                        return HttpResponseRedirect('/profile/')

subscription_success_email.txt

{% load i18n %}

Dear Customer,

Thank you for creating your first subscription! Just to confirm, your weekly deliveries will be sent to {{ apartment }}, and your first delivery will arrive on {{ first_delivery_date }}. After that, you'll receive deliveries every Monday until you cancel your subscription. Currently, you have selected the option {{ weekly_plan }}, but you can upgrade or downgrade at any time. We'll be sure to notify you once your first delivery arrives.

If anything looks incorrect, please email us at info@gather-food.us or update your subscription at http://www.gather.kitchen/profile/.

Enjoy!
Addison

0 个答案:

没有答案