|
| 1 | +from email.Utils import * |
| 2 | +from email.MIMEText import MIMEText |
| 3 | +from email.MIMEMessage import MIMEMessage |
| 4 | +from email.MIMEMultipart import MIMEMultipart |
| 5 | +import smtplib |
| 6 | +from django.conf import settings |
| 7 | +from django.template.loader import render_to_string |
| 8 | +from django.template import RequestContext |
| 9 | + |
| 10 | +def add_headers(msg): |
| 11 | + if not(msg.has_key('Message-ID')): |
| 12 | + msg['Message-ID'] = make_msgid('idtracker') |
| 13 | + if not(msg.has_key('Date')): |
| 14 | + msg['Date'] = formatdate(time.time(), True) |
| 15 | + if not(msg.has_key('From')): |
| 16 | + msg['From'] = settings.DEFAULT_FROM_EMAIL |
| 17 | + return msg |
| 18 | + |
| 19 | +def send_smtp(msg): |
| 20 | + ''' |
| 21 | + Send a Message via SMTP, based on the django email server settings. |
| 22 | + The destination list will be taken from the To:/Cc: headers in the |
| 23 | + Message. The From address will be used if present or will default |
| 24 | + to the django setting DEFAULT_FROM_EMAIL |
| 25 | + ''' |
| 26 | + add_headers(msg) |
| 27 | + (fname, frm) = parseaddr(msg.get('From')) |
| 28 | + to = [addr for name, addr in getaddresses(msg.get_all('To') + msg.get_all('Cc', []))] |
| 29 | + # todo: exception handling |
| 30 | + server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT) |
| 31 | + if settings.DEBUG: |
| 32 | + server.set_debuglevel(1) |
| 33 | + if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD: |
| 34 | + server.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD) |
| 35 | + server.sendmail(frm, to, msg.as_string()) |
| 36 | + server.quit() |
| 37 | + |
| 38 | +def copy_email(msg, to): |
| 39 | + ''' |
| 40 | + Send a copy of the given email message to the given recipient. |
| 41 | + ''' |
| 42 | + add_headers(msg) |
| 43 | + new = MIMEMultipart() |
| 44 | + # get info for first part. |
| 45 | + # Mode: if it's production, then "copy of a message", otherwise |
| 46 | + # "this is a message that would have been sent from" |
| 47 | + # hostname? |
| 48 | + # django settings if debugging? |
| 49 | + # Should this be a template? |
| 50 | + if settings.SERVER_MODE == 'production': |
| 51 | + new.attach(MIMEText("This is a copy of a message sent from the I-D tracker.")) |
| 52 | + else: |
| 53 | + new.attach(MIMEText("The attached message would have been sent, but the tracker is in %s mode.\nIt was not sent to anybody.\n" % settings.SERVER_MODE)) |
| 54 | + new.attach(MIMEMessage(msg)) |
| 55 | + new['From'] = msg['From'] |
| 56 | + new['Subject'] = '[Django %s] %s' % (settings.SERVER_MODE, msg.get('Subject', '[no subject]')) |
| 57 | + new['To'] = to |
| 58 | + send_smtp(new) |
| 59 | + |
| 60 | +def send_mail_subj(request, to, frm, stemplate, template, context, cc=None, extra=None): |
| 61 | + ''' |
| 62 | + Send an email message, exactly as send_mail(), but the |
| 63 | + subject field is a template. |
| 64 | + ''' |
| 65 | + subject = render_to_string(template, context, context_instance=RequestContext(request)) |
| 66 | + return send_mail(request, to, frm, subject, template, context, cc, extra) |
| 67 | + |
| 68 | +def send_mail(request, to, frm, subject, template, context, cc=None, extra=None): |
| 69 | + ''' |
| 70 | + Send an email to the destination [list], with the given return |
| 71 | + address (or "None" to use the default in settings.py). |
| 72 | + The body is a text/plain rendering of the template with the context. |
| 73 | + extra is a dict of extra headers to add. |
| 74 | + ''' |
| 75 | + txt = render_to_string(template, context, context_instance=RequestContext(request)) |
| 76 | + return send_mail_text(request, to, frm, subject, txt, cc, extra) |
| 77 | + |
| 78 | +def send_mail_text(request, to, frm,subject, txt, cc=None, extra=None): |
| 79 | + msg = MIMEText(txt) |
| 80 | + if isinstance(frm, tuple): |
| 81 | + frm = formataddr(frm) |
| 82 | + if isinstance(to, list) or isinstance(to, tuple): |
| 83 | + to = ", ".join([isinstance(addr, tuple) and formataddr(addr) or addr for addr in to]) |
| 84 | + if isinstance(cc, list) or isinstance(cc, tuple): |
| 85 | + cc = ", ".join([isinstance(addr, tuple) and formataddr(addr) or addr for addr in cc]) |
| 86 | + if frm: |
| 87 | + msg['From'] = frm |
| 88 | + msg['To'] = to |
| 89 | + if cc: |
| 90 | + msg['Cc'] = cc |
| 91 | + msg['Subject'] = subject |
| 92 | + msg['X-Test-IDTracker'] = (settings.SERVER_MODE == 'production') and 'no' or 'yes' |
| 93 | + if extra: |
| 94 | + for k, v in extra.iteritems(): |
| 95 | + msg[k] = v |
| 96 | + if settings.SERVER_MODE == 'production': |
| 97 | + send_smtp(msg) |
| 98 | + copy_email(msg, "ietf.tracker.archive+%s@gmail.com" % settings.SERVER_MODE) |
0 commit comments