0

I'm setting up on a Django site the elegant forum application Spirit, but having just a bit of trouble getting it to send emails on the Heroku installation. This is undoubtedly because I'm a novice, a little unsteady about sending emails in Django, and not quite sure how to get the relevant debugging info. Some help would be much appreciated.

I installed the Postmark addon plus the Python-Postmark library. On the local installation, everything is perfectly fine: registration emails get delivered as they should when using the installation on my machine. The problem is only with the deployment version.

Also, when I enter

heroku run python
from django.config import settings
from django.core.mail import send_mail
send_mail('this is a test from the heroku server','body',settings.DEFAULT_FROM_EMAIL, ['the.robot@example.com'],fail_silently=False)

then the Postmark server sends the message and it is received. So... it seems like the Postmark installation, and the Django command send_mail at least sort of work on the Heroku server. It's just when I request e.g. a user activation message through the browser while using the forum application itself, on the Heroku server, that the mail is not sent.

Postmark is configured in the site's settings.py files like this.

EMAIL_BACKEND = 'postmark.django_backend.EmailBackend'
POSTMARK_API_KEY = os.environ['POSTMARK_API_KEY']
POSTMARK_SENDER = 'the.robot@example.com'
POSTMARK_TEST_MODE = False
POSTMARK_TRACK_OPENS = False
DEFAULT_FROM_EMAIL = POSTMARK_SENDER
SERVER_EMAIL = POSTMARK_SENDER

Here is the (slightly modified) code which is supposed to be sending the email.

def sender(request, subject, template_name, context, to):
    site = 'example.com'
    context.update({'site_name': 'example',
                'domain': 'example.com',
                'protocol': 'https' if request.is_secure() else 'http'})
    message = render_to_string(template_name, context)
    from_email = "the.robot@example.com"
    if len(to) > 1:
        kwargs = {'bcc': to, }
    else:
        kwargs = {'to': to, }

    email = EmailMessage(subject, message, from_email, **kwargs)

def send_activation_email(request, user):
    subject = _("User activation")
    template_name = 'spirit/user/activation_email.html'
    token = UserActivationTokenGenerator().generate(user)
    context = {'user_id': user.pk, 'token': token}
    sender(request, subject, template_name, context, [user.email, ])

I also tried using gmail as a mail server, as suggested in this answer, and the same problem recurred. That is, mail gets sent perfectly fine from the local installation, but is apparently not sent by the Heroku version. Likewise, the command send_mail then still works on Heroku.

Community
  • 1
  • 1
mmw
  • 740
  • 8
  • 12

0 Answers0