Email Sending issues in Django

Dear Team,

I am facing the issues while sending email from the Django application and in my organisation gmail mailing server using. Below the codes and problem.

Problem :

Whenever I receive an email, it shows me that I receive email from admin email address instead of user email. It should show me that it is received from user email address who are logged in.

settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'abc786@gmail.com'
EMAIL_HOST_PASSWORD = 'xyz98233'
EMAIL_PORT = 587
ACCOUNT_EMAIL_VERIFICATION = 'none'
EMAIL_USE_SSL = False

**views.py**
*******************
def ViewSendEmail(request):
    send_mail('Hello from Bilal Khan',
        'Hello there, This is an automated message.',
        request.user.email, #FROM 
        ['abc786@gmail.com'], #TO
        fail_silently=False)
    return render(request, 'nextone/email_send.html')



Please help me how to resolve this issues. Really, appreciated if resolve this issues


Thanks in advance

My recollection (from about 12 years ago) is that Gmail does not let you send emails from “unassociated” accounts. You have to confirm each email address that is going to be used as a from address before Gmail will let you use it. (Granted, this is from about 2008, things may have changed.)

Hi Sir,

Thanks for the quick response.

Yes, you are right, but my organization using the gmail server/ g-suit server for internal emailing or outside communication.
e.g. abc@oais.com (On g-suit server).

I am developing the workflow based application like

User Request-- Line manager–HR Department – final approval

I need like once User Login and raise a request mail goes to Line Manager from logged user email id.

Once Line Manager approver mail goes to HR Department from liner manager email id. Same like

Problem:

Mail going to correct but from email id not changed and go by the settings.py email host user id.

That is the basically problem

CODE:

subject =‘Requested by ‘+str(request.user.profile.full_name)+’ for your approval’
to = request.POST.get(‘level1’) # send to
from_email =request.user.email # fro
text_content = ‘This is an important message.’
html_content = “Hi, Please find the below URL to approve the details Click Here”
try:
msg = EmailMultiAlternatives(subject,text_content,from_email,[to])
msg.attach_alternative(html_content,“text/html”)
msg.send() # Mail send code
except BadHeaderError:
return HttpResponse(‘Invalid header found.’)

Thanks Sir in advance for the solution.

I understand basically what you’re saying. (Some of the specifics aren’t clear, but I’m not sure it matters.)

As I currently understand it, and I acknowledge that I could be quite wrong here, you have three choices.

  • Define all email addresses intended to be used as the sender as gmail alias addresses in your gmail logon account.

  • Use a different mail server for outgoing mail

  • Acknowledge that what you want to do is not allowed.

It could be the ‘EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend’’ ’ line. Try getting rid of that line and re-running your code. From my own experience, you shouldn’t need to specify an EMAIL_BACKEND if you’re using SMTP.

1 Like

I think this is true. I ran into the same problem 2 days ago and it worked!

Try using an app-specific password. This is what mine looks like:

EMAIL_HOST= 'smtp.gmail.com'
EMAIL_HOST_USER= 'xxx@gmail.com'
EMAIL_HOST_PASSWORD='app-specific-pass-word'
EMAIL_USE_TLS= True
EMAIL_PORT= 587

Glad to hear it! Feel free to keep the thread going if you have anymore questions.

1 Like

I’m puzzled. I can’t see how/why omitting that line would make any difference.

The default, as defined in django.conf.global_settings is precisely this line:
EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend’

If you can identify a specific situation where adding that line to your settings creates an identifiable difference of behavior, I would think that that would be classified as a bug. (Now, it might rank extremely low on the urgency scale, but if explicitly setting a setting that matches the defaults changes behavior, my concern would be that this would happen with other settings as well.)

Ken

Hi,

Thanks for the details.

Now I have removed the line EMAIL_BACKEND from the settings.py. But still same problem.

Mail is going, but whom send to is not changing. e.g. below code there is Email_Host_User email id given.

EMAIL_HOST= 'smtp.gmail.com'
EMAIL_HOST_USER= 'xxx@gmail.com'
EMAIL_HOST_PASSWORD='app-specific-pass-word'
EMAIL_USE_TLS= True
EMAIL_PORT= 587

If, mail is going to user or any person. Now, he/she received from an EMAIL_HOST_USER id , instead of the current login user (current login user email id is yyyzzz@domain.com ) .

I need to user receive email form this id not from the EMAIL_HOST_USER= 'xxx@gmail.com’

Thanks

Dear All,

Thanks for the details.

@ Code is in settings.py and removed the line in the settings.py EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend

EMAIL_HOST= 'smtp.gmail.com'
EMAIL_HOST_USER= 'xxx@gmail.com'
EMAIL_HOST_PASSWORD='app-specific-pass-word'
EMAIL_USE_TLS= True
EMAIL_PORT= 587

My question is if any current user login in the application(like leave application) and he raise a request other user have received the emails notification from the application, but why mail is going to the EMAIL_HOST_USER= ‘xxx@gmail.com’ instead of current login user email id .

I need to other person receive from the current login user email id not from the “EMAIL_HOST_USER”

Thanks

If you find an answer, please be sure to share it here, because I’d be curious to see it.

Everything I’m finding on line gives me the impression that unless you register every desired account as an alias, it’s not going to work.

Ken

My apologies, I think I misunderstood your initial question. I believe @KenWhitesell is correct. I do not know of a way (off the top of my head) to use the built-in email functional like you’r describing. With that being said, using a different resource for emails might be more in your ballpark. What is that resource? Not too sure at the moment, unfortunately, but I am also curious if you can find a solution to this problem :slight_smile: