0

I'm trying to send an attachment via e-mail in my project with Django. But I am getting such an error. I know this question asked before but they're almost 10 years old and some of them didn't even answered.

My codes are as follows:

Django=3.2.7
django-crispy-forms==1.12.0

views.py

# send email
def sendMail(request):
    message = request.POST.get("message", "")
    subject = request.POST.get("subject", "")
    receivers = request.POST.get("email", "")
    email = EmailMessage(subject, message, settings.EMAIL_HOST_USER, [receivers])
    email.content_subtype = "html"

    file = request.FILES["file"]
    #file = request.POST.get("file")
    email.attach(file.name, file.read(), file.content_type)
    
    email.send()
    return HttpResponse(f"Sent to {receivers}")

forms.py

class EmailForm(forms.Form):
    # receivers email
    email = forms.EmailField()
    subject = forms.CharField(max_length=100)
    file = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
    message = forms.CharField(widget = forms.Textarea)

models.py

# for sending email
class EmailReportModel(models.Model):
    """
    this model created just for email foreignKey
    """
    #receiver_emails = models.ForeignKey(BolgeMailListeleri, on_delete=models.CASCADE, related_name = "receiver_emails")
    receiver_emails = models.ManyToManyField(BolgeMailListeleri)

    subject = models.CharField(max_length=255, default="")
    message = models.TextField()
    upload = models.FileField(upload_to='uploads', null=True, blank=True)
    #upload = models.Field()
    #image1 = forms.Field(label='sample photo', widget = forms.FileInput,    required = True )

SendEmail.html

{% load crispy_forms_tags %}
<div class="main">
    <!-- Create a Form -->
    <form method="post" enctype="multipart/form-data">
        <!-- Security token by Django -->
        {% csrf_token %}
        {{ form|crispy }}
    <button type="submit">Gönder</button>
    </form>
    <button onclick="goBack()" class="btn btn-danger">İptal Et</button>

</div>

<script>
    function goBack() {
      window.history.back();
    }
</script>

settings.py

# need an actual email
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = 587
EMAIL_HOST_USER = "mygmail@gmail.com"
EMAIL_HOST_PASSWORD = "****"
EMAIL_USE_TLS = True

urls.py

urlpatterns = [
    path('sendEmail', sendMail, name='send-email'),
]
xyres
  • 20,487
  • 3
  • 56
  • 85
  • 2
    Does this answer your question? [django MultiValueDictKeyError error, how do I deal with it](https://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it) Similar to `request.POST`, `request.FILES` is a `QueryDict`, hence that question should help you solve it... Next you have a _form_ that can clean and get those values for you, why not _use_ it? – Abdul Aziz Barkat Oct 21 '21 at 18:03
  • This is one of the questions that 10 years old which I mentioned . That question was not a solution for me – Yusuf Berkay GIRGIN Oct 21 '21 at 18:05
  • @YusufBerkayGIRGIN Doesn't matter how old it is. Django's core hasn't changed much in that regard. The error means `request.FILES` doesn't have a key called `file`. – xyres Oct 21 '21 at 18:39

0 Answers0