Hello, I’ve started a project and it has a feature of sending mails after submitting form. User logs in, fills and submits form. Form renders in the index page and from index page user can reach the detail page for every post (meeting). I send mails as templates and mail content is dynamic, every reciever (selected in the form) recieves their own mail. This form has a file input and I can attach files to the mail and recievers get the mail with attachment but can not open. for example I send a 13kb pdf file but in the inbox it seems as 244 bayt and cannot be open. (content of the email is correct, type of the attachment is correct, name of it is also correct ). Where should I look for this problem. I am lost here and every respond is appreciated.
Here is the code I am using and if I need to share more please tell me.
view for creating meeting (toplanti in my language):
def toplantiekle(request):
toplanti = Toplanti.objects.all()
if request.method == 'POST':
form = ToplantiForm(data=request.POST, files=request.FILES)
if form.is_valid():
tarih = form.cleaned_data['tarih']
konu = form.cleaned_data['konu']
katilimcilar = form.cleaned_data['katilimci']
for katilimci in katilimcilar:
yeni_toplanti = form.save(commit=False)
user_profile = Profil.objects.get(user=request.user)
yeni_toplanti.profil = user_profile
yeni_toplanti.save()
form.save_m2m()
template = render_to_string('toplantiapp/email.html',{
'isim': katilimci.katilimci,
'tarih': tarih,
'konu': konu,
'katilimci': katilimci,
})
plain_message = strip_tags(template)
email = EmailMessage(
'Toplantı Bildirimi',
plain_message,
settings.EMAIL_HOST_USER,
[katilimci.mail],
)
for file in request.FILES.getlist('dosya_yukle'):
email.attach(file.name, file.read(), file.content_type)
# email.fail_silently = False
email.send()
return redirect('toplantiapp:index')
else:
print(form.errors)
else:
form = ToplantiForm()
return render(request, 'toplantiapp/toplantiekle.html', {
'form':form
})
this is the form that I use for creating meeting:
class ToplantiForm(forms.ModelForm):
katilimci = forms.ModelMultipleChoiceField(queryset=Katilimci.objects.all(), required=True)
class Meta:
model = Toplanti
fields = ('konu','tarih','katilimci','dis_katilimci','dosya_yukle','aciklama')
widgets = {
'konu':forms.TextInput(attrs={"placeholder":"Toplantının Konusu","style":"height:32px; border:1px solid #A0A0A0; border-radius:3px"}),
'aciklama':forms.Textarea(attrs={'cols':50,'rows':25, 'placeholder':'Açıklama','style':'width:100%; border:solid 1px #A0A0A0 ; margin-bottom:10px; border-radius:3px; resize: none;'}),
'tarih':forms.DateTimeInput(attrs={"type":"text", "onfocus":"(this.type='datetime-local')","placeholder":"Tarih","id":"tarih"}),
'dis_katilimci':forms.TextInput(attrs={"placeholder":"TGS Dışı Katılımcılar","style":"height:32px; border:solid 1px #A0A0A0; border-radius:3px;"}),
'dosya_yukle':forms.FileInput(attrs={"placeholder":"Dosya Yükle","style":"display:none; border:1px solid #D8D8D8; padding-left:2p; padding-top:10px; padding-bottom:12px; padding-right;52.8%; background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/Upload_alt_font_awesome.svg/1024px-Upload_alt_font_awesome.svg.png'); background-position:98%; background-repeat: no-repeat; background-size: 25px;","id":"dosya_ekleme","name":"dosya_ekleme","class":"dosya_ekleme"}),
'katilimci':forms.CheckboxSelectMultiple(attrs={"placeholder":"TGS Katılımcıları","class":"katilimciselect"}) # bunun classını katılımcıselect'ten değiştirdim
}
labels = {
'konu':'formkonu',
'tarih':'tarih',
'katilimci':'katilimci',
'aciklama':'aciklama',
}
email configuration in settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER ='mailsend@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
Thank you from now