def send_email_reply(request): # Save this email in the database. emailLog = EmailLog() emailLog.subject = request.POST.get('reply_subject') emailLog.body = request.POST.get('reply_body') emailLog.from_email = request.POST.get('reply_from') emailLog.to = request.POST.get('reply_to') emailLog.cc = request.POST.get('reply_cc') reply_department_id = request.POST.get('department_id') reply_department = Department.objects.get(pk=reply_department_id) emailLog.department = reply_department emailLog.timestamp = timezone.now() emailLog.sent_successfully = False emailLog.save() # Send the message. email_message = EmailMessage() email_message.subject = emailLog.subject email_message.body = emailLog.body email_message.from_email = emailLog.from_email if emailLog.to: email_message.to = emailLog.to.split(',') if emailLog.cc: email_message.cc = emailLog.cc.split(',') try: email_message.send() emailLog.sent_successfully = True emailLog.save() return HttpResponse() except: logger.exception("Error sending email. from_email=" + str(emailLog.from_email) + ", to_department=" + str(emailLog.department) + ", subject=" + str(emailLog.subject)) return HttpResponseServerError()
def custom_email(label, to, cc=None, attachments=None, context=None): if not isinstance(to, (list, tuple)): to = [to] if not isinstance(cc, (list, tuple)): cc = [cc] full_context = dict() full_context.update({} or context) mail_obj = EmailMessage() mail_obj.to = to mail_obj.cc = cc mail_obj.subject = render_to_string('email/{}/subject.txt'.format(label), context=full_context) mail_obj.from_email = settings.EMAIL_HOST_USER mail_obj.body = render_to_string('email/{}/message.html'.format(label), context=full_context) if attachments: for file_name in attachments: if os.path.exists(file_name): mail_obj.attach_file(file_name) else: logging.debug( "file is not available in specified location: {}".format( file_name)) mail_obj.content_subtype = "html" try: return mail_obj.send() except Exception as e: msg = u"sending email failed\n" msg += unicode(e) print >> sys.stderr, e
def contact_us(request): # Save this email in the database. emailLog = EmailLog() emailLog.subject = request.POST.get('subject') emailLog.body = request.POST.get('message') emailLog.from_email = request.POST.get('from_email') to_department_id = request.POST.get('to_department') to_department = Department.objects.get(pk=to_department_id) emailLog.to = to_department.email cc = [] emailLog.cc = None cc_departments = Department.objects.filter(chair=True).exclude( id=to_department_id) for cc_department in cc_departments: cc.append(cc_department.email) if emailLog.cc == None: emailLog.cc = cc_department.email else: emailLog.cc += "," + cc_department.email emailLog.department = to_department emailLog.timestamp = timezone.now() emailLog.sent_successfully = False emailLog.save() # Send the message. email_message = EmailMessage() subject = settings.CONTACT_US_SUBJECT_PREFIX + emailLog.subject body = "-------------------------------------------------------------------------------\n" body += " Message sent from %s on %s.\n" % ( emailLog.from_email, emailLog.timestamp.strftime("%Y-%m-%d %I:%M %p")) body += " Go to %s?emailId=%s to reply.\n" % ( request.build_absolute_uri("/staff-only/email-log/"), emailLog.id) body += "-------------------------------------------------------------------------------\n" if emailLog.body == None or emailLog.body == "": subject = subject + "<eom>" else: body += emailLog.body email_message.subject = subject email_message.body = body email_message.to = [emailLog.to] if cc: email_message.cc = cc try: email_message.send() emailLog.sent_successfully = True emailLog.save() return HttpResponse() except: logger.exception("Error sending email. from_email=" + str(emailLog.from_email) + ", to_department=" + str(emailLog.department) + ", subject=" + str(emailLog.subject)) return HttpResponseServerError()
def contact_us(request): # Save this email in the database. emailLog = EmailLog() emailLog.subject = request.POST.get('subject') emailLog.body = request.POST.get('message') emailLog.from_email = request.POST.get('from_email') to_department_id = request.POST.get('to_department') to_department = Department.objects.get(pk=to_department_id) emailLog.to = to_department.email cc = [] emailLog.cc = None cc_departments = Department.objects.filter(chair=True).exclude(id=to_department_id) for cc_department in cc_departments: cc.append(cc_department.email) if emailLog.cc == None: emailLog.cc = cc_department.email else: emailLog.cc += "," + cc_department.email emailLog.department = to_department emailLog.timestamp = timezone.now() emailLog.sent_successfully = False emailLog.save() # Send the message. email_message = EmailMessage() subject = settings.CONTACT_US_SUBJECT_PREFIX + emailLog.subject body = "-------------------------------------------------------------------------------\n" body += " Message sent from %s on %s.\n" % (emailLog.from_email, emailLog.timestamp.strftime("%Y-%m-%d %I:%M %p")) body += " Go to %s?emailId=%s to reply.\n" % (request.build_absolute_uri("/staff-only/email-log/"), emailLog.id) body += "-------------------------------------------------------------------------------\n" if emailLog.body == None or emailLog.body == "": subject = subject + "<eom>" else: body += emailLog.body email_message.subject = subject email_message.body = body email_message.to = [emailLog.to] if cc: email_message.cc = cc try: email_message.send() emailLog.sent_successfully = True emailLog.save() return HttpResponse() except: logger.exception("Error sending email. from_email=" + str(emailLog.from_email) + ", to_department=" + str(emailLog.department) + ", subject=" + str(emailLog.subject)) return HttpResponseServerError()