Z
Z
Zenoviy Burychko2019-01-24 22:35:35
Django
Zenoviy Burychko, 2019-01-24 22:35:35

How to customize email sent via smtp?

I have a function that sends a letter to the mail with the data that the user fills in on the site. The problem is that there is to send all these data, then it turns out just porridge. How can I customize this letter so that it looks at least like a data plate.

def checkout(request):
    session_key = request.session.session_key
    products_in_basket = ProductInBasket.objects.filter(session_key=session_key, is_active=True, order__isnull=True)
    print (products_in_basket)
    for item in products_in_basket:
        print(item.order)


    form = CheckoutContactForm(request.POST or None)
    if request.POST:
        print(request.POST)
        if form.is_valid():
            print("yes")
            data = request.POST
            name = data.get("name", "3423453")
            phone = data["phone"]
            address = data.get("address")
            user, created = User.objects.get_or_create(username=name, defaults={"first_name": name})

            order = Order.objects.create(user=user, customer_name=name, customer_phone=phone, customer_address=address)
            msg = phone + name
            smtp = smtplib.SMTP("localhost", 1025)
            smtp.sendmail('[email protected]', '[email protected]', ('Thank for order!'+msg))

            for name, value in data.items():
                if name.startswith("product_in_basket_"):
                    product_in_basket_id = name.split("product_in_basket_")[1]
                    product_in_basket = ProductInBasket.objects.get(id=product_in_basket_id)
                    print(type(value))

                    product_in_basket.nmb = value
                    product_in_basket.order = order
                    product_in_basket.save(force_update=True)

                    ProductInOrder.objects.create(product=product_in_basket.product, nmb = product_in_basket.nmb,
                                                  price_per_item=product_in_basket.price_per_item,
                                                  total_price = product_in_basket.total_price,
                                                  order=order)




            return HttpResponseRedirect(request.META['HTTP_REFERER'])
        else:
            print("no")

    return render(request, 'orders/checkout.html', locals())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Shatalov, 2019-01-24
@netpastor

https://docs.djangoproject.com/en/dev/topics/email...
Well, if you collect the contents of the letter yourself - make a text template and render it, passing the necessary data to it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question