예제 #1
0
def send_broadcast_email(request):
	if not get_media_file_contents('generic_email.html'):
		return HttpResponseBadRequest('Generic email template not defined. Visit the NEMO customizable_key_values page to upload a template.')
	form = EmailBroadcastForm(request.POST)
	if not form.is_valid():
		return render(request, 'email/compose_email.html', {'form': form})
	dictionary = {
		'title': form.cleaned_data['title'],
		'greeting': form.cleaned_data['greeting'],
		'contents': form.cleaned_data['contents'],
		'template_color': form.cleaned_data['color'],
	}
	content = get_media_file_contents('generic_email.html')
	content = Template(content).render(Context(dictionary))
	users = None
	audience = form.cleaned_data['audience']
	selection = form.cleaned_data['selection']
	active_choice = form.cleaned_data['only_active_users']
	try:
		if audience == 'tool':
			users = User.objects.filter(qualifications__id=selection)
		elif audience == 'project':
			users = User.objects.filter(projects__id=selection)
		elif audience == 'account':
			users = User.objects.filter(projects__account__id=selection)
		elif audience == 'all':
			users = User.objects.all()
		if active_choice:
			users.filter(is_active=True)
	except:
		dictionary = {'error': 'Your email was not sent. There was a problem finding the users to send the email to.'}
		return render(request, 'email/compose_email.html', dictionary)
	if not users:
		dictionary = {'error': 'The audience you specified is empty. You must send the email to at least one person.'}
		return render(request, 'email/compose_email.html', dictionary)
	if audience == 'tool':
		t = Tool.objects.filter(id=selection)
		subject = t[0].name + ': ' + form.cleaned_data['subject']
	else:
		subject = form.cleaned_data['subject']
	users = [x.email for x in users]
	if form.cleaned_data['copy_me']:
		users += [request.user.email]
	try:
		email = EmailMultiAlternatives(subject, from_email=request.user.email, bcc=set(users))
		email.attach_alternative(content, 'text/html')
		email.send()
	except SMTPException as e:
		dictionary = {
			'title': 'Email not sent',
			'heading': 'There was a problem sending your email',
			'content': 'NEMO was unable to send the email through the email server. The error message that NEMO received is: ' + str(e),
		}
		return render(request, 'acknowledgement.html', dictionary)
	dictionary = {
		'title': 'Email sent',
		'heading': 'Your email was sent',
	}
	return render(request, 'acknowledgement.html', dictionary)
예제 #2
0
파일: email.py 프로젝트: krzywon/NEMO
def send_broadcast_email(request):
	content = get_media_file_contents('generic_email.html')
	if not content:
		return HttpResponseBadRequest('Generic email template not defined. Visit the customization page to upload a template.')
	form = EmailBroadcastForm(request.POST)
	if not form.is_valid():
		return render(request, 'email/compose_email.html', {'form': form})
	dictionary = {
		'title': form.cleaned_data['title'],
		'greeting': form.cleaned_data['greeting'],
		'contents': form.cleaned_data['contents'],
		'template_color': form.cleaned_data['color'],
	}
	content = Template(content).render(Context(dictionary))
	users = None
	audience = form.cleaned_data['audience']
	selection = form.cleaned_data['selection']
	active_choice = form.cleaned_data['only_active_users']
	try:
		if audience == 'tool':
			users = User.objects.filter(qualifications__id=selection)
		elif audience == 'project':
			users = User.objects.filter(projects__id=selection)
		elif audience == 'account':
			users = User.objects.filter(projects__account__id=selection)
		if active_choice:
			users = users.filter(is_active=True)
	except Exception as error:
		warning_message = 'Your email was not sent. There was a problem finding the users to send the email to.'
		dictionary = {'error': warning_message}
		logger.warning(warning_message + ' audience: {}, only_active: {}. The error message that was received is: {}'.format(audience, active_choice, str(error)))
		return render(request, 'email/compose_email.html', dictionary)
	if not users:
		dictionary = {'error': 'The audience you specified is empty. You must send the email to at least one person.'}
		return render(request, 'email/compose_email.html', dictionary)
	subject = form.cleaned_data['subject']
	users = [x.email for x in users]
	if form.cleaned_data['copy_me']:
		users += [request.user.email]
	try:
		send_mail(subject=subject, content=content, from_email=request.user.email, bcc=set(users), email_category=EmailCategory.BROADCAST_EMAIL)
	except SMTPException as error:
		site_title = get_customization('site_title')
		error_message = f"{site_title} was unable to send the email through the email server. The error message that was received is: " + str(error)
		logger.exception(error_message)
		dictionary = {
			'title': 'Email not sent',
			'heading': 'There was a problem sending your email',
			'content': error_message,
		}
		return render(request, 'acknowledgement.html', dictionary)
	dictionary = {
		'title': 'Email sent',
		'heading': 'Your email was sent',
	}
	return render(request, 'acknowledgement.html', dictionary)
예제 #3
0
파일: email.py 프로젝트: krzywon/NEMO
def email_preview(request):
	generic_email_template = get_media_file_contents('generic_email.html')
	if generic_email_template:
		form = EmailBroadcastForm(request.POST)
		email_context = {
			'title': form.data['title'],
			'greeting': form.data['greeting'],
			'contents': form.data['contents'],
			'template_color': form.data['color'],
		}
		email_content = Template(generic_email_template).render(Context(email_context))
		return HttpResponse(mark_safe(email_content))
	return HttpResponse()
예제 #4
0
파일: email.py 프로젝트: SolitonMan/NEMO
def send_broadcast_email(request):
    if not get_media_file_contents('generic_email.html'):
        return HttpResponseBadRequest(
            'Generic email template not defined. Visit the NEMO customizable_key_values page to upload a template.'
        )
    recipients = request.POST.getlist('recipient', None)
    form = EmailBroadcastForm(request.POST)
    if not form.is_valid():
        return render(request, 'email/compose_email.html', {'form': form})
    dictionary = {
        'title': form.cleaned_data['title'],
        'greeting': form.cleaned_data['greeting'],
        'contents': form.cleaned_data['contents'],
        'template_color': form.cleaned_data['color'],
    }
    content = get_media_file_contents('generic_email.html')
    content = Template(content).render(Context(dictionary))
    users = None
    audience = form.cleaned_data['audience']
    selection = form.cleaned_data['selection']
    active_choice = form.cleaned_data['only_active_users']
    try:
        users = User.objects.filter(id__in=recipients)

        if active_choice:
            users = users.filter(is_active=True)
    except Exception as error:
        warning_message = 'Your email was not sent. There was a problem finding the users to send the email to.'
        dictionary = {'error': warning_message}
        logger.warning(
            warning_message +
            ' audience: {}, only_active: {}. The error message that NEMO received is: {}'
            .format(audience, active_choice, str(error)))
        return render(request, 'email/compose_email.html', dictionary)
    if not users:
        dictionary = {
            'error':
            'The audience you specified is empty. You must send the email to at least one person.'
        }
        return render(request, 'email/compose_email.html', dictionary)
    subject = form.cleaned_data['subject']
    users = [x.email for x in users]
    if form.cleaned_data['copy_me']:
        users += [request.user.email]
    try:
        email = EmailMultiAlternatives(subject,
                                       from_email=request.user.email,
                                       bcc=set(users))
        content = content.replace("\r\n\r\n", "</p><p>")
        content = content.replace("\r\n", "<br />")
        email.attach_alternative(content, 'text/html')
        email.send()
    except SMTPException as error:
        error_message = 'NEMO was unable to send the email through the email server. The error message that NEMO received is: ' + str(
            error)
        logger.exception(error_message)
        dictionary = {
            'title': 'Email not sent',
            'heading': 'There was a problem sending your email',
            'content': error_message,
        }
        return render(request, 'acknowledgement.html', dictionary)
    dictionary = {
        'title': 'Email sent',
        'heading': 'Your email was sent',
    }
    return render(request, 'acknowledgement.html', dictionary)