Sending mail though django - python

In my project, I tried to allow users to change and reset password. But can't figure out why this mail configuration isn't working. Actually, it doesn't send mail to the user. But showing e-mail has been sent on API.
Here is my code:
settings.py
REST_SESSION_LOGIN = True
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
SITE_ID = 1
ACCOUNT_EMAIL_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_EMAIL_VERIFICATION = 'optional'
#mail config
EMAIL_HOST = 'smtp.gmail.net'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'mygmail#gmail.com'
EMAIL_HOST_PASSWORD = '************' #gmail app password
EMAIL_USE_TLS = True
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('authentication/', include('dj_rest_auth.urls')),
path('authentication/registration/', include('dj_rest_auth.registration.urls')),
path('api/', include('articles.api.urls')),
re_path(r'^authentication/password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', PasswordResetConfirmView.as_view(), name='password_reset_confirm')
]
And in terminal at from it shows webmaster#localhost. But why it isn't showing EMAIL_HOST_USER e-mail.
Subject: Password reset on example.com
From: webmaster#localhost
To: gicof53256#tdcryo.com
Date: Sun, 29 Nov 2020 08:14:16

When you have mentioned EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' this will mock the email, but not actually send it. To actually send it, replace it with the below settings.
Also, it's better to store the credentials in environment variables. I am attaching the code snippet that will do the job.
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST = os.environ.get('EMAIL_HOST')
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_PORT = 587

Related

Djnago send mail not working, also not showing any error

Settings.py
DEFAULT_FROM_EMAIL = 'testing.email2908#gmail.com'
SERVER_EMAIL = 'testing.email2908#gmail.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'testing.email2908#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAILL_USE_TLS = True
views.py
print('Helloo')
send_mail(
'Testing',
'Hi',
'testing.email2908#gmail.com',
['xyz#gmail.com'], #my personal gmail id
fail_silently=False,
)
print('Hiiii')
When i run this code, only Helloo is getting printed, I've imported send_mail as well,tried using smtplib as well but that was giving smpt auth extension error so i'm trying send_mail method but it also doesn't seem to work, don't know what is the exact issue.
try this
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'emailId'
EMAIL_HOST_PASSWORD = 'password'
Need to set Following 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 = 'xyz.gmail.com' # sender's email-id
EMAIL_HOST_PASSWORD = 'xyz' # password associated with above email-id
I think you forgot EMAIL_BACKEND.
To send mail add following code
from django.conf import settings
from django.core.mail import send_mail
subject = 'email subject'
message = 'Hi , Thank you for your help.'
email_from = settings.EMAIL_HOST_USER
recipient_list = [user.email, ] # email to send
send_mail( subject, message, email_from, recipient_list )
If you are using gmail for sending mails then you need to turn on Less Secure App Access. The option is present in Manage Your Account in gmail.
I hope it will help you.

Sending Godaddy email via Django using python

I have these settings
EMAIL_HOST = 'smtpout.secureserver.net'
EMAIL_HOST_USER = 'username#domain.com'
EMAIL_HOST_PASSWORD = 'password'
DEFAULT_FROM_EMAIL = 'username#domain.com'
SERVER_EMAIL = 'username#domain.com'
EMAIL_PORT = 465
EMAIL_USE_TLS = True
SMTP_SSL = True
Speaking to Godaddy I have found out these are the ports and settings
smtpout.secureserver.net
ssl
465
587
TLS ON
3535
TLS ON
25
TLS ON
80
TLS ON
or
TLS OFF
I have tried all the combinations. If I set TLS to True I am getting
STARTTLS extension not supported by the server.
If I set to 465 I am getting
If I set other combinations like
EMAIL_HOST = 'smtpout.secureserver.net'
EMAIL_HOST_USER = 'username#domain.com'
EMAIL_HOST_PASSWORD = 'password'
DEFAULT_FROM_EMAIL = 'username#domain.com'
SERVER_EMAIL = 'username#domain.com'
EMAIL_PORT = 25
EMAIL_USE_TLS = False
For verification, I used Google Mail settings to test if the email sending via python works, and it is working.
Now I want to switch to GoDaddy and I know for the email we use TLS to log in even for POP3 download and it is working, so I am not sure why python / Django option is not working. Can you please help?
I have called Godaddy, they cannot help because it is a software issue - all their settings and ports are working, so I have no one to ask.
This worked for me with my GoDaddy email. Since GoDaddy sets up your email in Office365, you can use smtp.office365.com.
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.office365.com'
EMAIL_HOST_USER = 'myemail#GoDaddyDomain.com'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = 'myPassword'
EMAIL_PORT = 587
EMAIL_USE_SSL = False
EMAIL_USE_TLS = True
method in views.py that handles sending email. The email variable is from a user form.
from django.conf import settings
from django.core.mail import EmailMessage
def send_email(subject, body, email):
try:
email_msg = EmailMessage(subject, body, settings.EMAIL_HOST_USER, [settings.EMAIL_HOST_USER], reply_to=[email])
email_msg.send()
return "Message sent :)"
except:
return "Message failed, try again later :("
I found this code worked for me.. Hope this will be useful to somebody.I was using SMTP godaddy webmail..you can put this code into your django setting file.
Since you cannot set Both SSL and TSL together... if you do so you get the error
something as, At one time either SSL or TSL can be true....
setting.py
# Emailing details
EMAIL_HOST = 'md-97.webhostbox.net'
EMAIL_HOST_USER = 'mailer#yourdomain'
EMAIL_HOST_PASSWORD = 'your login password'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
I managed to decide to access the site
https://sso.secureserver.net/?app=emailsetup&realm=pass&
It seems that when accessing, you can enable email for external use.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtpout.secureserver.net'
EMAIL_HOST_USER='aXXXX#xxxx'
EMAIL_HOST_PASSWORD='AsgGSDAGSasdsagas'
DEFAULT_FROM_EMAIL='aXXXX#xxxx'
EMAIL_PORT=465
EMAIL_USE_SSL=True
EMAIL_USE_TLS=False
It worked for me
It's been a while but, maybe the answer can help somebody else. I just talked to GoDaddy about the issue and the needed configration they told me was like,
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtpout.secureserver.net'
EMAIL_HOST_USER = env('EMAIL_HOST_USER')
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = env('EMAIL_HOST_PASSWORD')
EMAIL_PORT = 80
EMAIL_USE_SSL = False
EMAIL_USE_TLS = True
these are the django config settings but, I'm sure that it can be copied to other platforms.
Also, the email dns configration they provided was,
use 'smtpout.asia.secureserver.net' as EMAIL_HOST in Django settings.py. you can refer to the GoDaddy email help page link.
https://in.godaddy.com/help/mail-server-addresses-and-ports-for-business-email-24071
In my case, this way work for me:
#settings.py
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST='smtpout.secureserver.net'
EMAIL_PORT=465
EMAIL_USE_SSL=True
EMAIL_USE_TLS=False
EMAIL_HOST_USER='your#domain'
EMAIL_HOST_PASSWORD='yourpass'
ADMIN_EMAIL='your#domain'

SMTP AUTH extension not supported by server - Sending emails through a private host

I registered a domain and a private email using namecheap.com. I am trying to send an email from this private email. However, I get the error in the title.
In my settings.py, I have these settings:
EMAIL_HOST = 'mail.privateemail.com'
EMAIL_HOST_USER = 'contact#mysite.com'
EMAIL_HOST_PASSWORD = 'my password'
EMAIL_PORT = 587
EMAIL_USE_TLS = False
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
And I am trying to send my mail through a view:
send_mail(
'Subject here',
'Here is the message.',
'contact#mysite.com',
['myname#gmail.com'],
fail_silently=False,
)
I get this error :
SMTP AUTH extension not supported by server.
This is happening because you have conflicting settings:
EMAIL_PORT = 587 # Port 587 is reserved for TLS
EMAIL_USE_TLS = False # But you have disabled TLS
You either need to set EMAIL_USE_TLS to True or use the default port for unencrypted connections (25).
I was searching answer for this since 4 hours. I still dont know why this configuration works for me, but yes, IT WORKS for me.
I simply removed,
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
from my settings.py, and add below configuration in settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'username#domain.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Remove the following config since "SMTP AUTH extension not supported by" your EMAIL_HOST:
EMAIL_HOST_PASSWORD = 'my password'
EMAIL_PORT = 587
I was using a server that is not Gmail by default and turns out to be a port issue, I found that I was using the wrong port, I changed it to 465 for SMTP with TLS, Thanks to this link..
So here are the settings I used:
EMAIL_PORT = 465
EMAIL_USE_TLS = True

How to change settings.py variables using admin?

I'm working on a "contact us" page in Django. I want to allow admin to change their smtp,name,password and port in django admin.
So there is a table admin_contact which can be modified via admin page. The problem is that I don't know how to set EMAIL_HOST,EMAIL_PORT,EMAIL_HOST_USER and
EMAIL_HOST_PASSWORD inside settings.py (using model admin_contact) which I have to set to be able to use send_mail method in views.py.
Is there a way to do this?
This is force solution but it solved my problem.
Generate a file (email_config.py) with your Information (Email, Password, smtp, etc) from a Form.
email_config.py
CONFIG_HOST = "smtp.gmail.com"
CONFIG_EMAIL = "mail#gmail.com"
CONFIG_PASS = "******"
CONFIG_PORT = "587"
Import email_config.py from you setting.
setting.py
from email_config import CONFIG_HOST, CONFIG_EMAIL, CONFIG_PASS, CONFIG_PORT
...
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
# G-mail
EMAIL_HOST = CONFIG_HOST
EMAIL_HOST_USER = CUSTOM_EMAIL
EMAIL_HOST_PASSWORD = CONFIG_PASS
EMAIL_PORT = CONFIG_PORT
EMAIL_FROM_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = EMAIL_HOST_USER
Test
> python manage.py shell
> from django.core.mail import EmailMessage
> msg = EmailMessage("Hi, man","This is a test message",to=["juan#gmail.com"])
> msg.send()
1
Apache and mod_wgsi need restart after change information

how to setup hmail server with a django project

I am trying to setup open source hmailserver with a django project. I have set up the server according to documentation. But I dont know how to use it with django.
For gmail i was using this setting:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = Email
EMAIL_HOST_PASSWORD = Password
DEFAULT_FROM_EMAIL = Email
SERVER_EMAIL = Email
I tried changing the settings like this, But didn't worked:
EMAIL_USE_TLS = True
EMAIL_HOST = 'something.com'
EMAIL_PORT = 25
EMAIL_HOST_USER = 'info#something.com'
EMAIL_HOST_PASSWORD = 'password'
DEFAULT_FROM_EMAIL = 'info#something.com'
SERVER_EMAIL = 'info#something.com'
hmail credentials are:
My domain name: something.com
My account name: info#somthing.com
How to configure it with django?
Did you add EMAIL_BACKEND?
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'no-reply#*****'
EMAIL_HOST = 'smtp.gmail.com'
# Port for sending e-mail.
EMAIL_PORT = 25
# Optional SMTP authentication information for EMAIL_HOST.
EMAIL_HOST_USER = '*****'
EMAIL_HOST_PASSWORD = '*****'
EMAIL_USE_TLS = False
SERVER_EMAIL should be the name of the computer which hMailServer runs on (usually it's not the same as the domain name).

Categories

Resources