/contact/ 处的 TypeError - "to" 参数必须是列表或元组 - Django

TypeError at /contact/ - "to" argument must be a list or tuple - Django

我想将联系表单电子邮件数据发送到域电子邮件 ID,但是当我填写联系表单的所有字段并提交时,出现错误 -

异常值:“to”参数必须是列表或元组


views.py code

from django.contrib.messages.api import error
from django.shortcuts import render
from django.core.mail import send_mail
from website.models import Contact
from django.contrib import messages


def contact(request):
    if request.method == 'POST':
        name = request.POST['name']
        phone = request.POST['phone']
        email = request.POST['email']
        address = request.POST['address']
        subject = request.POST['subject']
        message = request.POST['message']

        # All Filling Fields Values sending to the contact page so that fields values can be available during any error

        form_data = {'name': name, 'phone': phone, 'email': email,
                     'address': address, 'subject': subject, 'message': message}

                            
        # send an email
        send_mail(
            name, # Subject
            phone, # message
            email, # from email
            address, # Address
            subject, # Subject
            message, # message
            'mail@domain.com', # from Email
            ['toemail@gmail.com',], # To email            
        )

        # Form Validation
        error_message = None

        if (not name):
            error_message = "Name Required !!"
        elif name:
            if len(name) < 3:
                error_message = "Name must be 3 character long"
        elif not phone:
            error_message = "Phone Number Requird"
        elif len(phone) < 10:
            error_message = "Phone number must be 10 character long."
        elif not subject:
            error_message = "Please fill the value of Subject. "
        elif not message:
            error_message = "Please tell us some brief about the requirement in the Message Box."

        if not error_message:
            contact = Contact(name=name, phone=phone, email=email,
                              address=address, subject=subject, message=message)
            contact.save()
            data = {'error': error_message, 'name': name, 'values': form_data}
            return render(request, 'contact.html', data)
        else:
            data = {'error': error_message, 'name': name, 'values': form_data}
            return render(request, 'contact.html', data)
    else:
        return render(request, 'contact.html', {})

Models.py - Code

from django.db import models

class Contact(models.Model):
    sno = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    phone = models.CharField(max_length=15)
    email = models.EmailField()
    address = models.CharField(max_length=200)
    subject = models.CharField(max_length=150)
    message = models.TextField()
    time = models.DateTimeField(auto_now_add=True, blank=True)

Contact.html - file Code

{% if name %}
<div class="text-center alert alert-success alert-dismissible fade show" role="alert">
    <h2>Hello {{ name }}!</h2>
    <p class="fs-4">We have received your email and will respond shortly.</p>
    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

{% else %}

<section class="ContactForm">
    <div class="container">
        <div class="row">
            <div class="col-sm-12 col-md-6 col-lg-12 col-12">
                <!-- {{ name }}  It is just of testing that contact form is workig or not-->
                <form class="row g-4 m-2" action="{% url 'contact' %}" method="post">
                    {% csrf_token %}

                    {% if error %}
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <strong>{{error}}</strong>
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                    {% endif %}

                    <div class="col-md-6">
                        <input type="text" id="name" name="name" value="{{values.name}}" class="form-control lineH" placeholder="Your Name">
                    </div>
                    <div class="col-md-6">
                        <input type="text" id="phone" name="phone" value="{{values.phone}}" class="form-control lineH" placeholder="Your Phone">
                    </div>
                    <div class="col-md-6">
                        <input type="email" id="email" name="email" value="{{values.email}}" class="form-control lineH" placeholder="Your 
Email">
                    </div>
                    <div class="col-md-6">
                        <input type="text" id="address" name="address" value="{{values.address}}" class="form-control lineH" placeholder="Your 
Address">
                    </div>
                    <div class="col-md-6">
                        <input type="text" id="subject" name="subject" value="{{values.subject}}" class="form-control lineH" 
placeholder="Subject">
                    </div>
                    <div class="col-md-6">
                        <select id="timingschedule" name="timingschedule" class="form-select lineH">
                            <option selected>Choose Your Schedule</option>
                            <option value="9 AM to 10 AM">10 AM to 11 AM</option>
                            <option value="11 AM to 12 PM">11 AM to 1 PM</option>
                            <option value="2 PM to 4 PM">2 PM to 4 PM</option>
                            <option value="8 PM to 10 PM">4 PM to 7 PM</option>
                        </select>
                    </div>
                    <div class="col-12">
                        <textarea name="message" id="message" name="message" value="{{values.message}}" class="form-control" placeholder="Your 
Message"></textarea>
                    </div>

                    <div class="col-12 text-center">
                        <button type="submit" class="btn btn-primary lead">SUBMIT</button>
                    </div>

                </form>
            </div>
        </div>

    </div>

</section>
{% endif %}

Settings.py File Code

EMAIL_HOST = 'mail.domain.com'

EMAIL_PORT = 465

EMAIL_HOST_USER = 'mail@domain.com'

EMAIL_HOST_PASSWORD = '********'

EMAIL_USE_TLS = 真

请提出解决方法。

您的 send_mail 参数与要求的参数不匹配(参见 https://docs.djangoproject.com/en/3.2/topics/email/#quick-example)。

我认为您需要使用表单中的数据构建邮件正文。您可以将表单值合并到一个新字符串中,然后发送电子邮件。


email_body = '{name}\n\n    {phone} - {email} - {address} \n\n {message}'.format(**form_data))

send_mail(
    subject,
    email_body,
    'mail@domain.com', # from Email
    ['toemail@gmail.com',], # To email  
)