def build_contact(request, slug=""):
    controller = get_object_or_404(ContactFormController, slug=slug)
    site = Site.objects.get_current()
    UserModel = get_user_model()
    user = request.user
    form = ContactForm(request.POST or None, request.FILES or None, controller=controller)
    if user.is_authenticated:
        try:
            name = user.display_name
        except AttributeError:
            name = user.username
        form.fields['sender_name'].widget.attrs['readonly'] = 'true'
        form.fields['sender_name'].initial = name
        form.fields['sender_email'].widget.attrs['readonly'] = 'true'
        form.fields['sender_email'].initial = user.email
    if form.is_valid():
        if controller.store_in_db:
            new_msg = Contact(**form.cleaned_data)
            new_msg.controller = controller
            new_msg.site = site
            if controller.override_subject:
                new_msg.subject = controller.override_subject
            new_msg.save()
        if controller.send_emails:
            form_data = form.cleaned_data
            if controller.override_subject:
                subject = controller.override_subject
            elif 'subject' in form_data:
                subject = form_data['subject']
            else:
                subject = "{} message from {}".format(controller.name, form_data['sender_name'])
            body = "{} \n\n {}".format(form_data['body'], form_data['sender_name'])
            if controller.request_contact_info:
                body += "\nAddress: {} \nCity: {} \nState: {} \nPhone: {}".format(
                    form_data['contact_address'],
                    form_data['contact_city'],
                    form_data['contact_state'],
                    form_data['contact_phone']
                )
            if controller.email_options == '2':
                try:
                    to = [UserModel.objects.get(username=form.cleaned_data['to']).email]
                except Exception:
                    to = [form.cleaned_data['to']]
            if controller.email_options == '1':
                to = [r.email for r in controller.recipients.all()]
                for r in controller.other_recipients.all():
                    to.append(r.email)
            if 'send_a_copy' in form.cleaned_data:
                to.append(form.cleaned_data['sender_email'])
            mail = EmailMessage(
                subject=subject,
                body=body,
                from_email=form.cleaned_data['sender_email'],
                to=to
            )
            if 'photo' in request.FILES:
                photo = request.FILES['photo']
                mail.attach(photo.name, photo.read(), photo.content_type)
            mail.send()
        return render(request, 'success_url', {'controller': controller})
    return render(request, 'contact/form.html', {
        'form': form,
        'site': site,
        'controller': controller
    })