Home > Software design >  I need to send mail for more than one member in django
I need to send mail for more than one member in django

Time:09-27

I need to send an email to the users associated to the Shop when a purchase is done in the shop. If two users was associated for two of them also email should go.

Note: shop_user is the feild name and it is a many to many feild

def generate_invoice(request):
    if request.user.is_authenticated:
        billingshop = shop.objects.all()
        Product = product.objects.all()
        if request.method == 'POST':
            form = Invoicing(request.POST)
            if form.is_valid():
                user = form.save(commit=False)
                Billing_shop = form.cleaned_data['Billing_shop']
                Supplier = form.cleaned_data['Supplier']
                Payment_mode = form.cleaned_data['Payment_mode']
                Upi_transaction_id = form.cleaned_data['Upi_transaction_id']
                products = form.cleaned_data['Product']
                Quantity = form.cleaned_data['Quantity']
                shoping_product = product.objects.filter(Product_name= products).first()
                sub_total = shoping_product.product_price * Quantity
                user.Gst = (18 / 100)*sub_total
                
                user.Price = sub_total   user.Gst
                user.save()
                shoppingcartuser= shop.objects.get(shop_name= Billing_shop) // Match the shop name
                shoppingcartemails= shoppingcartuser.shop_users.all().values_list('email', flat=True) // Retriving the email address associated with the shopname in the feild name shop_users.

                date = datetime.today()
                html_content = render_to_string("invoices/invoice_email.html", {'title':"test mail","date":date,"invoiceuser":Billing_shop,"supplier":Supplier,"payment":Payment_mode,"transactionid":Upi_transaction_id})
                text_content = strip_tags(html_content)

                email = EmailMultiAlternatives(
                    "Congratulations! Invoice generated successfully",
                    text_content,
                    settings.EMAIL_HOST_USER,
                    [shoppingcartemails] // this place is the to email
                )
                email.attach_alternative(html_content,"text/html")
                email.send()
                messages.success(request, 'Registration successful.')
                return redirect('home')
        else:
            form = Invoicing()
        return render(request, 'invoices/create_invoice.html', context={'form': form,'shop':billingshop,'Product':Product})
    else:
        messages.error(request, 'Please login into your account.')
        return render("login")

The feild shop_users consists of two mail addresses and i am facing this error in the browser. I don't know how to rectify it.

Invalid address; only <QuerySet > could be parsed from "<QuerySet ['[email protected]', '[email protected]']>"

Can someone help me please. I need to send email for both of the email id.

CodePudding user response:

you should put it on a list, and it will not be necessary the [ ]

shoppingcartemails= list(shoppingcartuser.shop_users.all().values_list('email', flat=True))
email = EmailMultiAlternatives(
                    "Congratulations! Invoice generated successfully",
                    text_content,
                    settings.EMAIL_HOST_USER,
                    shoppingcartemails 
                )
  • Related