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.
2 Likes
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