Permalink
Browse files

Completed basic mailer using database for storing credentials

  • Loading branch information...
FinnStokes committed May 19, 2012
1 parent 2e25964 commit 37bab62b104e15066d9aa95f76b354e2b91579c7
Showing with 74 additions and 8 deletions.
  1. +4 −0 mailer/admin.py
  2. +6 −1 mailer/models.py
  3. +5 −0 mailer/urls.py
  4. +20 −7 mailer/views.py
  5. +1 −0 settings.py
  6. +10 −0 templates/mailer/error_mail.html
  7. +17 −0 templates/mailer/send_mail.html
  8. +10 −0 templates/mailer/sent_mail.html
  9. +1 −0 urls.py
View
@@ -0,0 +1,4 @@
+from mailer.models import *
+from django.contrib import admin
+
+admin.site.register(MailAccount)
View
@@ -1,3 +1,8 @@
from django.db import models
-# Create your models here.
+class MailAccount(models.Model):
+ priority = models.IntegerField()
+ host = models.CharField(max_length=30)
+ port = models.PositiveIntegerField()
+ user = models.CharField(max_length=30)
+ password = models.CharField(max_length=30)
View
@@ -0,0 +1,5 @@
+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('mailer.views',
+ url(r'^$', 'send_mail'),
+)
View
@@ -1,17 +1,30 @@
from django import forms
+from django.core.context_processors import csrf
+from django.shortcuts import render_to_response
from django.core.mail import EmailMessage
+from email.mime.text import MIMEText
from accounts.models import UserProfile
+from mailer.models import MailAccount
def send_mail(request):
if request.method == 'POST':
form = EmailForm(request.POST)
if form.is_valid():
- message = EmailMessage(form.cleaned_data['subject'],
- form.cleaned_data['body'],
- 'Jamalaide <contact@jamalaide.org.au>',
- bcc = UserProfile.mailing_list())
- message.send()
- return render_to_response('mailer/sent_mail.html')
+ message = MIMEText(form.cleaned_data['body'])
+ message['Subject'] = form.cleaned_data['subject']
+ message['From'] = 'Jamalaide <contact@jamalaide.org.au>'
+ recipients = UserProfile.objects.filter(notify=True)
+ for m in MailAccount.objects.order_by('priority'):
+ smtp = smtplib.SMTP( m.host , m.port )
+ smtp.ehlo()
+ smtp.starttls()
+ smtp.ehlo()
+ smtp.login( m.user, m.password )
+ refused = smtp.sendMail('contact@jamalaide.org.au', recipients, message.as_string())
+ smtp.quit()
+ if len(refused) == 0:
+ return render_to_response('mailer/sent_mail.html', { 'subject': message['subject'] })
+ return render_to_response('mailer/error_mail.html', { 'subject': message['subject'] })
else:
form = EmailForm()
@@ -22,4 +35,4 @@ def send_mail(request):
class EmailForm(forms.Form):
subject = forms.CharField()
- body = forms.TextField()
+ body = forms.CharField(widget=forms.Textarea())
View
@@ -33,6 +33,7 @@
'sidebar',
'accounts',
'forums',
+ 'mailer',
# djangoappengine should come last, so it can override a few manage.py commands
'djangoappengine',
@@ -0,0 +1,10 @@
+{% extends "base.html" %}
+{% load url from future %}
+
+{% block title %}Jamalaide - Send Mail - Error{% endblock %}
+
+{% block content %}
+
+Error sending email "{{ subject }}".
+
+{% endblock %}
@@ -0,0 +1,17 @@
+{% extends "base.html" %}
+{% load url from future %}
+
+{% block title %}Jamalaide - Send Mail{% endblock %}
+
+{% block content %}
+
+<form method="post" action="{% url 'mailer.views.send_mail' %}">
+{% csrf_token %}
+<table>
+{{ form.as_table }}
+</table>
+
+<input type="submit" value="send" />
+</form>
+
+{% endblock %}
@@ -0,0 +1,10 @@
+{% extends "base.html" %}
+{% load url from future %}
+
+{% block title %}Jamalaide - Send Mail - Sent{% endblock %}
+
+{% block content %}
+
+Email "{{ subject }}" sent to all members on mailing list.
+
+{% endblock %}
View
@@ -39,6 +39,7 @@
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/', include('accounts.urls')),
url(r'^forums/', include('forums.urls')),
+ url(r'^mailer/', include('mailer.urls')),
url(r'^', include('news.urls')),
(r'^about$', 'django.views.generic.simple.direct_to_template',
{'template': 'about.html'}),

0 comments on commit 37bab62

Please sign in to comment.