how can i get mail from from_mail in django?

Viewed 192

I am sending mail from my django site. Everything is fine and mail is also sent and received successfully, but not from_email(that put on email field in contact form). Email sent from EMAIL_HOST_USER = 'example@gmail.com' that I put on setting.py.

def contact(request):
    if request.method == 'POST':
        name = request.POST['name']
        email = request.POST['email']
        subject = request.POST['subject']
        phone = request.POST['phone']
        message = request.POST['message']
        try:
            send_mail(subject,message,email,['to@gmail.com',],fail_silently=False)
            messages.success(request, 'We get your message and reply shortly...')
        except:
            messages.error(request, "failed")

    return render(request, 'pages/contact.html')

I want mail will be sent from email(that user put on the email field)

3 Answers

you have to provide your email address instead of passing email of user who put in contact form so change your code like this

send_mail(subject,message,'mydomain@example.com',['to@gmail.com',],fail_silently=False)

check official doc.

Try this:-

def contact(request):
    if request.method == 'POST':
        name = request.POST['name']
        sender = request.POST['email']
        subject = request.POST['subject']
        phone = request.POST['phone']
        message = request.POST['message']
        try:
            msg_mail = str(message)" " + str(sender)
            send_mail(subject , msg_mail,sender ,  ['to@gmail.com'], fail_silently=False)
            messages.success(request, 'We get your message and reply shortly...')
        except:
            messages.error(request, "failed")

    return render(request, 'pages/contact.html')

when you are sending the email the 'from' is you and the 'to' is the reciever you can't send a mail from the email in the field as it is the email of the reciever.

Related