Email List

100 views
Skip to first unread message

davidt

unread,
Jul 18, 2016, 7:02:18 AM7/18/16
to Django users
I have a model that includes an option to subscibe to an email list. However, being somewhat new to Django I am assure how to create a bulk email list form this to send to the appropriate people. I have read various documents on this and have ended up somewhat confused.

Could someone please offer me some enlightenment as to the best way to do this?

Many thanks in advance.

Fred Stluka

unread,
Jul 18, 2016, 7:34:55 AM7/18/16
to django...@googlegroups.com
David,

There may be a package that does it all for you.  But if not, you can
create a model that stores email addresses and send to those
addresses via the Django mail package.  See docs at:
- https://docs.djangoproject.com/en/dev/topics/email/

Here's what my code to send email looks like:

from django.core.mail import EmailMessage, EmailMultiAlternatives
def send(to, subject, body="", cc=[], bcc=[], html_body=None):
    try:
        # Pulls implicitly from settings.py, but can each be overridden here.
        #       EMAIL_HOST
        #       EMAIL_PORT
        #       EMAIL_HOST_USER
        #       EMAIL_HOST_PASSWORD
        #       EMAIL_USE_TLS
        #       DEFAULT_FROM_EMAIL
        if html_body:
            email_message = EmailMultiAlternatives()
        else:
            email_message = EmailMessage()
        email_message.to = to
        email_message.cc = cc
        email_message.bcc = bcc
        email_message.subject = subject
        email_message.body=body
        if html_body:
            email_message.attach_alternative(html_body, "text/html")
        email_message.send(fail_silently=False)
        success = True

    except smtplib.SMTPServerDisconnected as e:
        msg = u'Unable to connect to SMTP server ' + settings.EMAIL_HOST
        msg += u' to send e-mail to ' + unicode(email_message.to)
        exception = e

    except smtplib.SMTPSenderRefused as e:
        msg = u'The SMTP server ' + settings.EMAIL_HOST
        msg += u' refused to send e-mail from ' + unicode(e.sender)
        msg += u' when asked to send e-mail to ' + unicode(email_message.to)
        exception = e

    except smtplib.SMTPRecipientsRefused as e:
        msg = u'The SMTP server ' + settings.EMAIL_HOST
        msg += u' refused to send e-mail to recipients ' + unicode(e.recipients)
        msg += u' when asked to send e-mail to ' + unicode(email_message.to)
        exception = e

    except smtplib.SMTPDataError as e:
        msg = u'The SMTP server ' + settings.EMAIL_HOST
        msg += u' refused to accept the message data'
        msg += u' when asked to send e-mail to ' + unicode(email_message.to)
        exception = e

    except smtplib.SMTPConnectError as e:
        msg = u'Could not establish a connection with the'
        msg += u' SMTP server ' + settings.EMAIL_HOST
        msg += u' to send e-mail to ' + unicode(email_message.to)
        exception = e

    except smtplib.SMTPHeloError as e:
        msg = u'The SMTP server ' + settings.EMAIL_HOST
        msg += u' refused our HELO message'
        msg += u' when sending e-mail to ' + unicode(email_message.to)
        exception = e

    except smtplib.SMTPAuthenticationError as e:
        msg = u'Unable to authenticate with SMTP server ' + settings.EMAIL_HOST
        msg += u' when sending e-mail to ' + unicode(email_message.to)
        exception = e

    except smtplib.SMTPResponseException as e:
        msg = u'The SMTP server ' + settings.EMAIL_HOST
        msg += u' returned: (' + unicode(e.smtp_code) + u') ' + unicode(e.smtp_error)
        msg += u' when sending e-mail to ' + unicode(email_message.to)
        exception = e

    except smtplib.SMTPException as e:
        msg = u'Unable to send e-mail to ' + unicode(email_message.to)
        exception = e

    except BaseException as e:
        msg = u'An unexpected error occurred '
        msg += u' when sending e-mail to ' + unicode(email_message.to)
        msg += u' during step: "' + progress + u'"'
        exception = e

    finally:
        if not success:
            raise EmailException(msg, exception)

--Fred

Fred Stluka -- mailto:fr...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/38f8b1b0-133d-4c32-8c09-a5fc334a9fe2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shem Nashon

unread,
Jul 18, 2016, 10:30:49 AM7/18/16
to Django users
Integrating email to django is a good but complex feature, My advice if you want your emails in the inbox and not spam box of your subscribers is to user sendgrid email gateway, they have a python sdk as well as a REST api that takes json, see  https://sendgrid.com/docs/API_Reference/SMTP_API/index.html or https://sendgrid.com/docs/API_Reference/Web_API/index.html

David Turner

unread,
Jul 18, 2016, 12:03:41 PM7/18/16
to django...@googlegroups.com
Hello

Thanks for this.
Would then the suggested route be to export my email list via an admin action and use this list via Sendmail?


On 18 July 2016 at 15:11, Shem Nashon <shemn...@gmail.com> wrote:
Integrating email to django is a good but complex feature, My advice if you want your emails in the inbox and not spam box of your subscribers is to user sendgrid email gateway, they have a python sdk as well as a REST api that takes json, see  https://sendgrid.com/docs/API_Reference/SMTP_API/index.html or https://sendgrid.com/docs/API_Reference/Web_API/index.html

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/ZHXhRZUfiPA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

James Schneider

unread,
Jul 20, 2016, 1:19:16 AM7/20/16
to django...@googlegroups.com


> Would then the suggested route be to export my email list via an admin action and use this list via Sendmail?
>

You can do this via a custom management command.

https://docs.djangoproject.com/en/1.9/howto/custom-management-commands/

If you do go this route, though, I would suggest looking at a batch processor such as Celery to handle sending the messages for you. You'll have much better control over tracking what messages have been successfully sent and the ability to cancel an ongoing job much more easily in the event issues arise.

If you feel the need to host the service yourself, might I also suggest looking at something like Mailman to handle your mailing list needs. It's written in Python and has all of the features you would need for making list management, many of which would be difficult or lengthy to write in yourself.

https://www.gnu.org/software/mailman/

Getting your message out to your uses can be quite strenuous, as SMTP servers can be very picky , as well as user inboxes, even for experienced admins.

Otherwise, if you can offload the sending off messages to a 3rd party provider that is dedicated to mailing list-like services, I'd suggest using them than trying to roll one yourself. Any decent service should be able to send on behalf of another domain and should already be setup to try and avoid bounce backs, black lists, and spam boxes. You can uses your Django site to collect user information and manage membership, and just fed the third party service with your messages.

-James

Reply all
Reply to author
Forward
0 new messages