Commit f6fabcca authored by Georgios Gousios's avatar Georgios Gousios
Browse files

Replace custom email send code with Django's mailer

Thanks to [faidon] for the tip
parent e87f4498
import json
from smtplib import SMTP
from email.mime import text
from email.header import Header
from email.utils import parseaddr, formataddr
from django.core.mail import send_mail
from django.conf import settings
import amqp_connection
......@@ -28,54 +24,17 @@ def send_async(frm = settings.SYSTEM_EMAIL_ADDR,
def send (sender = settings.SYSTEM_EMAIL_ADDR,
recipient = None, subject = None, body = None):
"""
Connect to the email server configured in settings.py
and send the email.
All arguments should be Unicode strings (plain ASCII works as well).
Only the real name part of sender and recipient addresses may contain
non-ASCII characters.
The charset of the email will be the first one out of US-ASCII, ISO-8859-1
and UTF-8 that can represent all the characters occurring in the email.
This method does not perform any error checking and does
not guarantee delivery
"""
import logging
logger = logging.getLogger("synnefo.logic")
attempts = 0
# Header class is smart enough to try US-ASCII, then the charset we
# provide, then fall back to UTF-8.
header_charset = 'ISO-8859-7'
# We must choose the body charset manually
for body_charset in 'US-ASCII', 'ISO-8859-7', 'UTF-8':
while attempts < 3:
try:
body.encode(body_charset)
except UnicodeError:
pass
else:
break
# Split real name (which is optional) and email address parts
sender_name, sender_addr = parseaddr(sender)
recipient_name, recipient_addr = parseaddr(recipient)
# We must always pass Unicode strings to Header, otherwise it will
# use RFC 2047 encoding even on plain ASCII strings.
sender_name = str(Header(unicode(sender_name), header_charset))
recipient_name = str(Header(unicode(recipient_name), header_charset))
# Make sure email addresses do not contain non-ASCII characters
sender_addr = sender_addr.encode('ascii')
recipient_addr = recipient_addr.encode('ascii')
# Create the message ('plain' stands for Content-Type: text/plain)
msg = text.MIMEText(body.encode(body_charset), 'plain', body_charset)
msg['From'] = formataddr((sender_name, sender_addr))
msg['To'] = formataddr((recipient_name, recipient_addr))
msg['Subject'] = Header(unicode(subject), header_charset)
send_mail(subject, body, sender, [recipient])
return
except Exception as e:
logger.warn("Error sending email: ", e)
finally:
attempts += 1
logger.warn("Failed all %d attempts to send email, aborting", attempts)
s = SMTP(host=settings.SMTP_SERVER)
s.sendmail(sender, recipient, msg.as_string())
s.quit()
......@@ -257,7 +257,10 @@ INVITATION_ENCR_KEY = '8d342f6e7a0366c632978a80257019af'
INVITATION_VALID_DAYS = 30
# SMTP server to use when sending out emails
SMTP_SERVER="127.0.0.1"
EMAIL_HOST = "127.0.0.1"
EMAIL_HOST_USER = ""
EMAIL_HOST_PASSWORD = ""
DEFAULT_CHARSET = 'utf-8'
# The address to use in the From: field when sending out emails
SYSTEM_EMAIL_ADDR="noreply@grnet.gr"
......@@ -280,9 +283,6 @@ HELPDESK_TOKEN_DURATION_MIN = 30
# IP addresses of the machines allowed to connect as help desk
HELPDESK_ALLOWED_IPS = ("127.0.0.1",)
# Helpdesk auth token
HELPDESK_AUTH_TOKEN = "0xdeadbabe"
#
# Network Configuration
#
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment