Esempio n. 1
0
    def test_send_mail_set_template_after_with_autoload_template(self):
        msg = FancyMail(subject=MailTest.SUBJECT, from_email=MailTest.FROM_EMAIL, to=MailTest.RECIPIENT_LIST)

        msg.html_template = MailTest.HTML_TEMPLATE
        msg.context = MailTest.CONTEXT
        msg.text_template = MailTest.TEXT_TEMPLATE
        msg.send()

        # Check that is correct
        # 1 read email file
        email_file = read_single_file(TMP_DIR)

        # 2 Check headers data:
        content_type = "Content-Type: multipart/alternative;"
        subject = "Subject: {0}".format(MailTest.SUBJECT)
        sender = "From: {0}".format(MailTest.FROM_EMAIL)
        receiver = "To: {0}".format(MailTest.RECIPIENT_LIST[0])
        self.assertTrue(content_type in email_file)
        self.assertTrue(subject in email_file)
        self.assertTrue(sender in email_file)
        self.assertTrue(receiver in email_file)

        # 3 Check that there are 2 types of email (text and HTML)
        plain = 'Content-Type: text/plain; charset="utf-8"'
        html = 'Content-Type: text/html; charset="utf-8"'
        self.assertTrue(plain in email_file)
        self.assertTrue(html in email_file)

        # 4 Check text content
        self.assertTrue(MailTest.CORRECT_TEXT in email_file)

        # 5 Check html content
        self.assertTrue(MailTest.CORRECT_HTML in email_file)
Esempio n. 2
0
def index(request):

    if request.method == 'POST':
        form = MailForm(request.POST)
        if form.is_valid():

            #Create the email
            msg = FancyMail()
            msg.subject = form.cleaned_data['subject']
            msg.to = [form.cleaned_data['to'], ]
            msg.from_email = form.cleaned_data['from_email']
            password = form.cleaned_data['password']
            use_tls = form.cleaned_data['use_tls']
            email_host = form.cleaned_data['email_host']
            email_port = form.cleaned_data['email_port']

            # Set Django live settings (Do not do this in production!!!!)
            settings.EMAIL_HOST_USER = msg.from_email
            settings.EMAIL_HOST_PASSWORD = password
            settings.EMAIL_USE_TLS = use_tls
            settings.EMAIL_HOST = email_host
            settings.EMAIL_PORT = email_port

            #Set context
            context = {
                    'user': form.cleaned_data['context_user']
            }

            # Load templates
            template = form.cleaned_data['template']
            msg.load_template("email/{0}/welcome.html".format(template),
                                context)
            # Send!
            msg.send()

            messages.success(request,
                    "Mail sent to {0} ".format(context['user']))
    else:
        form = MailForm()

    data = {
        'mail_form': form,
    }

    return render_to_response('example/index.html',
                            data,
                            context_instance=RequestContext(request))