0

我目前正在我的应用程序上建立一个联系我们的表格。我希望当用户提交表单并提交时,它将发送用户welcome message并将用户数据转发到我的电子邮件。

我能够使用EmailMultiAlternatives 下面的代码实现将 html 模板发送给我的用户。

但问题是EmailMultiAlternatives不支持向不同的电子邮件地址发送不同的消息。我想过使用send_mass_mail,但它不支持 html 内容。我如何自定义 EmailMultiAlternatives 以将多条消息发送到不同的电子邮件。这是我的代码

class Conactme(CreateView):
    form_class = Contactusform
    context_object_name = "forms"
    template_name = "registration/contactus.html"
    success_url = reverse_lazy('home')

    def form_valid(self, form):
        if form.is_valid():
        email = form.cleaned_data['email']
        full_name = form.cleaned_data['full_name']
        purpose = form.cleaned_data['purpose']
        phone = form.cleaned_data['phone']
        comment = form.cleaned_data['comment']
        form.save()
    content = {"fullname": full_name,
               "phone":  phone,
               "purpose": purpose
               }
    userssubject = "Users Registrations"
    adminsubject = "Welcome message"
    html_message = render_to_string(
        template_name="emails/email_comfirmation.html", context=content)
    meg = EmailMultiAlternatives(
        adminsubject, full_name, EMAIL_HOST_USER, [email])
    meg.attach_alternative(html_message, 'text/html')
    meg.send()
    if meg.send:
     return render(self.request, "registration/sucesss.html")
4

0 回答 0