Home > Software engineering >  Django Create post and send mail
Django Create post and send mail

Time:10-18

after saving my variables in views.py that I have forwarded, can I send them as mail while saving the same fields? My mail sending codes are below but I didn't know how to do it.

def gcreate(request):
    if request.method == 'POST':
        gmember = gunluk(
            adsoyad=request.POST['adsoyad'],
            adsoyad2=request.POST['adsoyad2'],
            vardiya=request.POST['vardiya'],
            aciklama=request.POST['aciklama'],
            incident=request.POST['incident'],
            alinanaksiyon=request.POST['alinanaksiyon'],
            ulasilmayanekip=request.POST['ulasilmayanekip'],
            ulasilmayanbilgisi=request.POST['ulasilmayanbilgisi'],)
        try:
            gmember.full_clean()
        except ValidationError as e:
            pass
        send_mail(
        'test',
        'testmessage',
        '[email protected]',  
        ['[email protected]'],
        fail_silently=False
    )
        gmember.save()
        messages.success(request, 'Ekleme İşlemi Başarılı!')
        return redirect('/gunlukistakibi')
    else:
        return render(request, 'gcreate.html')

CodePudding user response:

Did you check the Gmail less secure apps settings? Turn on 'less secure app access'.

CodePudding user response:

from django.core.mail import send_mail
from django.conf import settings

def gcreate(request):
    if request.method == 'POST':
        gmember = gunluk(
            adsoyad=request.POST['adsoyad'],
            adsoyad2=request.POST['adsoyad2'],
            vardiya=request.POST['vardiya'],
            aciklama=request.POST['aciklama'],
            incident=request.POST['incident'],
            alinanaksiyon=request.POST['alinanaksiyon'],
            ulasilmayanekip=request.POST['ulasilmayanekip'],
            ulasilmayanbilgisi=request.POST['ulasilmayanbilgisi'],)
        try:
            gmember.full_clean()
        except ValidationError as e:
            pass
        send_mail(
    'Subject',
    'Message.',
    settings.EMAIL_HOST_USER,
    ['[email protected]'],
)
        gmember.save()
        messages.success(request, 'Ekleme İşlemi Başarılı!')
        return redirect('/gunlukistakibi')
    else:
        return render(request, 'gcreate.html')

In settings.py put the following

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com' # else your smtp provider and Less Secure App should be allowed
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your email address'
EMAIL_HOST_PASSWORD = 'your email address password'
  • Related