Мне нужно отправить почту для более чем одного пользователя в django
Мне нужно отправить электронное письмо пользователям, связанным с магазином, когда в магазине совершается покупка. Если два пользователя были ассоциированы для двух из них также должно приходить письмо.
Примечание: shop_user - это имя фейла, и это фейл "многие ко многим"
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")
<
Invalid address; only <QuerySet > could be parsed from "<QuerySet ['email1@gmail.com', 'email2@gmail.com']>"
Филд shop_users состоит из двух почтовых адресов, и я сталкиваюсь с этой ошибкой в браузере. Я не знаю, как ее исправить.
Может ли кто-нибудь помочь мне, пожалуйста. Мне нужно отправить электронное письмо для обоих почтовых идентификаторов.
вы должны записать его в список, и в этом не будет необходимости
shoppingcartemails= list(shoppingcartuser.shop_users.all().values_list('email', flat=True))
email = EmailMultiAlternatives(
"Congratulations! Invoice generated successfully",
text_content,
settings.EMAIL_HOST_USER,
shoppingcartemails
)