I use Django to send email,everything is OK when running on development environment, which uses command "python manage.py runserver 0.0.0.0:8100". But in the production environment which deployed by nginx+uwsgi+Django do not work.
Here is the code:
#Email settings
EMAIL_HOST='smtp.exmail.qq.com'
EMAIL_PORT='465'
EMAIL_HOST_USER='sender#qq.cn'
EMAIL_HOST_PASSWORD='password'
EMAIL_USE_SSL=True
RECEIVE_EMIAL_LIST=['receiver#qq.com']
send_mail('subject','content',setting.EMAIL_HOST_USER,setting.RECEIVE_EMIAL_LIST, fail_silently=False)
Have you checked that your production environment has the same network settings as the development environment?
Have you tried to ping or telnet the SMTP server from production?
Is possible that the production host is in a DMZ or in a subnet that has restricted access to the SMTP server you are trying to reach.
EMAIL_HOST = 'smtp.qq.com'
change EMAIL_PORT='465' to EMAIL_PORT = 587, with no quotes.
EMAIL_HOST_USER = 'abc#qq.com' # try to use your real email address.
RECEIVE_EMIAL_LIST=['receiver#qq.com'] to RECEIVE_EMIAL_LIST=['receiver#qq.com'**,**] # add a comma at the end of the list is a good habit.
Related
I have a django project in my pc.
In terminal I've run python3 manage.py runserver <my ipaddress>:8001
When I try to open the link in another pc, it is showing error page which says:
Invalid HTTP_HOST header: '<my ipaddress>:8001'. You may need to add '<my ipaddress>' to ALLOWED_HOSTS.
What should I do?
And moreover is it possible to put some text in place of ipaddress in the url?
For example, I want to host it as myproject/ instead of that complex url.
On one condition this will work
if both computers are on the same network like local Hotspot or same
LAN network
steps:
add '*' in your django projects's setting file in Allowed Host it will look like
ALLOWED_HOSTS = ['*']
run your server on this ip 0.0.0.0 and port any like 8000 using this command
manage.py runserver 0.0.0.0:8000
run ifconfig if you are using linux ipconfig if windows then you will get your ip address of your server
Open browser in another computer and enter the ip of server shown in 3rd step with port as 8000
http://ip-of-server:8000
Instead of passing <my-ip-address> to the runserver command, pass 0.0.0.0.
If both the machines are in the same network you can run the application on 0.0.0.0 IP address (refers to all IPv4 addresses on the local machine). Refer this link wiki 0.0.0.0 for more details. So, on application server run this:
manage.py runserver 0.0.0.0:8001
Now, from the other machines, access it using http://youripaddresss:8001 , where < youripaddress > is the actual ip address of your machine.
If both computers are not on the same network (local hotspot or LAN network)
You can use ngrok to view whatever is running on your localhost from any device
Using ngrok to view your django project from any device
follow the steps below:
Add '*' in your django projects's setting file in Allowed Hosts:
ALLOWED_HOSTS = ['*']
Download ngrok from the official website
Unzip the downloaded file and then go to the directory where the ngrok file is located via your terminal
Then type the command:
ngrok http 8000
or
./ngrok http 8000
Now you can open the url generated by ngrok on any device to view what is running on your pc:
https://randomly_generated_subdomain.ngrok.io
PS: this can also be used for any webserver running locally, not just django site
ngrok is a great tool that can be used to:
Run personal cloud services from your home
Demo websites without deploying
Build webhook consumers on your dev machine
Test mobile apps connected to your locally running backend
Stable addresses for your connected devices that are deployed in the field
Run your server with this command python manage.py runserver 0.0.0.0:8000
Then you can access your website from any system connected to your wifi with an URL like 192.168.0.1:8000
You may get your IP Address using this command in CMD ipconfig in windows and ifconfig in mac
Also add your IP to ALLOWED_HOSTS in setting.py
ALLOWED_HOST = ['*'] in your settings.py
python manage.py runserver 0.0.0.0:8000
then make sure your machine firewall allows incoming and outgoing traffic.
I use linux machine so from control center go to firewall and allow both incoming and outgoing.
then on your local network machine.
:8000
This is it !
worked for me
I am looking to have one of my django docker containers setup a smtp backend so I can send input from a contact form to a gmail address as well as possibly send out an email when someone puts in their email to subscribe for updates: https://docs.djangoproject.com/en/1.11/topics/email/#smtp-backend
How do I go about setting up the SMTP server? Is it a separate docker container? How do I set it up?
The email backend is just an imported module. You can just use it like it is described in the docs: https://docs.djangoproject.com/en/1.11/topics/email/#email-backends
Before that, you have to configure the data for the smtp server. You just have to configure the settings, like your smtp host, username and password, etc. Here are the docs to configure it: https://docs.djangoproject.com/en/1.11/topics/email/#smtp-backend
You can use an existing smtp server. If you have a package with a domain you probably have a an email account included.
There is no need for another container. But you can setup your own in a a separate container. There are already some docker-ready solutions present. Here are a few:
https://github.com/namshi/docker-smtp
https://mailu.io/
https://github.com/tomav/docker-mailserver
just do changes in some file
#add it in docker_compose.yml
maildev:
image: maildev/maildev
ports:
- "49180:80"
# add it in your settings.py file
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'maildev'
EMAIL_PORT = 25
# then run docker-compose up -d
# when it done then run docker build -t <your tag Name> .
happy coding.......
refrence link:
[1]: https://melvinkoh.me/configure-and-send-email-in-django-for-both-development-and-production-cjye4y0l7000nuns1m43vstaq
After setting DEBUG = False, and SECURE_SSL_REDIRECT = True and deploying a version on my app to the server, I wish now to develop further locally. The problem is, I think at one point I forgot to remove the SECURE_SSL_REDIRECT = True from settings.py, and I ran the local dev server with heroku local. My local browser always tries not to connect to localhost with SSL, so it just hangs.
I tried removing the site-specific cookies for localhost in the browser settings (Chrome) but localhost now still always tries to establish an SSL connection.
I am trying just to get back to using a non-SSL local connection for development. Any Ideas?
Django version 1.10.2
Heroku
Thanks
EDIT
Seems if I clear ALL the cache and cookies and restart the browser then it will not ask for SSL again. So it seems to be a browser problem. Anyway if anyone has an idea of how to accomplish this without having to clear all the data in Chrome, that would be appreciated.
UPDATE
I have learned a better way to handle this situation. I have set up some code to automatically sense if the software is running on the local environment or the cloud production environment, like this:
if os.environ.get('LOCAL'):
DEBUG = True
SECURE_SSL_REDIRECT = False
else:
DEBUG = False
SECURE_SSL_REDIRECT = True
Of course you have to take care of setting up the environ object, which happens automatically in heroku.
I am using custom settings both for local (development) and production environments.
For instance:
myproject/settings/dev.py
DEBUG = True
SECURE_SSL_REDIRECT = False
...
myproject/settings/production.py
DEBUG = False
SECURE_SSL_REDIRECT = True
...
And then I specify the settings I want to use. On localhost like this:
python myproject/manage.py runserver --settings=settings.dev
and for production using the Heroku Procfile:
web: gunicorn myproject.wsgi.production --log-file -
Content of myproject/wsgi/production.py is:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings.production")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
If you use heroku local for your local development, you can create similar Procfile.local with local wsgi file.
I have a mac running Lion, and a D-link router DIR-601. I want to run my django development server as a public website for testing. I set up port forwarding to my computers local ip address, through port 8000. Then i entered the ip address of my router (obtained by typing my ip address in a browser) followed by :8000. Nothing. Can any one assist or point me to a tutorial for accomplishing this?
Try:
python manage.py runserver 0.0.0.0:8000
Because runserver will bind to address 127.0.0.1 by default, which is not accessible by other computers.
And don't forget to change settings.py. You have to configure also ALLOWED_HOSTS. For example:
ALLOWED_HOSTS = ['*']
I want send email from django application.
I want send an email to my mail id from other username without authentication. Just i used smtp server for the authenticated mail.In the django mail api How it is supposed to send an mail with the local smtp?
As stated on the docs: https://docs.djangoproject.com/en/dev/topics/email/?from=olddocs you need to:
On your settings.py define the following:
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
Then, in another shell run the following command:
python -m smtpd -n -c DebuggingServer localhost:1025
That will run a dummy SMTP server, that actually will not send any email but you'll be able to see the output and check if that's correct. If you want to actually send your emails during development, you will need to install a SMTP server like sendmail and use that in your configuration.