I want to implement django managment command which send emails by smtp with not default settings from settings.py file such as:
EMAIL_HOST
EMAIL_HOST_USER
EMAIL_HOST_PASSWORD
FROM_MAIL
EMAIL_USE_TLS
i want to send with my alternative settings different from settings.py without change it email settings for all site.
how to implement this?
Define your alternate email settings and then create a new mail connection using those settings:
settings.py
ALTERNATE_EMAIL_HOST_PASSWORD = 'your password'
ALTERNATE_EMAIL_HOST_USER = 'your user'
ALTERNATE_EMAIL_HOST = ''
ALTERNATE_EMAIL_PORT = 123
ALTERNATE_EMAIL_USE_TLS = True
Then create new connection using those settings:
from django.core import mail
from django.core.mail import send_mail
from django.conf import settings
# create new connection
connection = mail.get_connection()
connection.password = settings.ALTERNATE_EMAIL_HOST_PASSWORD
connection.username = settings.ALTERNATE_EMAIL_HOST_USER
connection.host = settings.ALTERNATE_EMAIL_HOST
connection.port = settings.ALTERNATE_EMAIL_PORT
connection.use_tls = settings.ALTERNATE_EMAIL_USE_TLS
# send email using new connection you just created
send_mail('my subject', 'my message', settings.DEFAULT_FROM_EMAIL,
['abc#gmail.com'], connection=connection)
Related
During the process of creating my first website using Django framework, I encountered a little problem that I couldn't found a solution yet. So, when an user wants to reset his or her password, i'd like to send to him/her a reset mail. So far, I have this:
urls.py
from django.contrib.auth import views as auth_views
......
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='registration/password_reset_form.html'),
name='password_reset'),
path('password-reset-confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name='registration/password_reset_confirm.html'),
name='password_reset_confirm'),
path('password-reset/done/',
auth_views.PasswordResetDoneView.as_view(template_name='registration/password_reset_done.html'),
name='password_reset_done'),
path('password-reset-complete/',
auth_views.PasswordResetCompleteView.as_view(template_name='registration/password_reset_complete.html')),
....
settings.py
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_POST = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('traces_email')
EMAIL_HOST_PASSWORD = os.environ.get('traces_email_password')
I created a token generator for my link:
token_generator.py
from django.contrib.auth.tokens import PasswordResetTokenGenerator
import six
class TokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.is_active)
)
account_activation_token = TokenGenerator()
When I go through the reset flow, it does not send any email. It is still sent to my terminal.
Can somebody help me with this issue? Thank you so much for your time!
This setting
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
tells django to send the message to your terminal. To actually send an email, you need to use
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
This is described in the Django Docs.
I need to use google's SMTP mail server for my server to send e-mails to x recipients. But the mail never reaches the intended recipient. I have activated the non-secure applications in my gmail account and configured the following:
In Gmail I go to "Manage your GMAIL account" -> "Security" and activate 2-step verification. Then in "Manage your GMAIL account" -> "Security" -> "Application password" in selecting application I choose "Other (custom name)" and I add a name for the application and it generates a password of 16-digit characters.
Now the Django code is as follows:
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'xxxxxxxxx#gmail.com'
EMAIL_HOST_PASSWORD = 'xxxx xxxx xxxx xxxx' #contrasena de 16 digitos
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'default from email'
def send_user_mail(randomNumber):
subject = 'Titulo del correo'
message = EmailMultiAlternatives(subject, #Titulo
"Su nĂºmero es: "+str(randomNumber),
'xxxxx#gmail.com', #Remitente
to=['xxxx#gmail.com']) #Destinatario
message.send()
in settings.py I added:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Am I forgetting something or doing something wrong? Thanks in advance.
I want to use send_mail in django, however it doesn't send any email. Here is my settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'MY-GMAIL-USERNAME#gmail.com'
EMAIL_HOST_PASSWORD = 'MY-GMAIL-PASSWORD'
EMAIL_PORT = 465
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = DEFAULT_FROM_EMAIL
Then, I run python manage.py shell:
from django.conf import settings
from django.core.mail import send_mail
subject = 'Test Subject'
message = 'Test Message'
email_from = settings.EMAIL_HOST_USER
recipient_list = ['MY-YAHOO-USERNAME#yahoo.com']
send_mail(subject, message, email_from, recipient_list, fail_silently=False)
It doesn't send the email and it prints:
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Test Subject
From: MY-GMAIL-USERNAME#gmail.com
To: MY-YAHOO-USERNAME#yahoo.com
Date: Mon, 09 Dec 2019 21:02:16 -0000
Message-ID: <157592533640.18842.5494330274157836181#thinkpad-e560>
Test Message
-------------------------------------------------------------------------------
1
which seems to have no errors. Why doesn't it send the email? What is wrong? Why doesn't it log any errors?
Following this page, I've tried a pure python way and it works fine:
import smtplib, ssl
smtp_server = "smtp.gmail.com"
port = 465
sender_email = "MY-GMAIL-USERNAME#gmail.com"
password = 'MY-GMAIL-PASSWORD'
receiver_email = 'MY-YAHOO-USERNAME#yahoo.com'
context = ssl.create_default_context()
server = smtplib.SMTP_SSL("smtp.gmail.com", port, context=context)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, 'Test Message')
As I mentioned, this pure python way works fine! I'm confused.
What did I do wrong?
Thanks to #IainShelvington and #ivissani, I've figured it out. I use wagtail cms on top of django and created the project using "wagtail start mysite". for more information see this.
in wagtail standard project structure, there are base.py, dev.py and production.py instead of settings.py.
The Email settings was in base.py but in dev.py, EMAIL_BACKEND was overwritten. This is the dev.py when you create a django project using wagtail:
from .base import *
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'MY-SITE-KEY'
# SECURITY WARNING: define the correct hosts in production!
ALLOWED_HOSTS = ['*']
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
try:
from .local import *
except ImportError:
pass
So I fixed the problem by removing EMAIL_BACKEND from dev.py
Start your python file with this:
import os
import sys
import django
django.setup()
# rest of your code
from django.conf import settings imports your settings but it doesn't set it as os environment.
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' - this also should work
I am using rackspace email and having problem sending email via Mezzanine(1.4.10) form page.
This is my settings:
EMAIL_HOST = 'secure.emailsrvr.com'
EMAIL_PORT = 465 # other ports also tried
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'info#example.com'
EMAIL_HOST_PASSWORD = 'secret'
This is Rackspace documentation:
http://www.rackspace.com/apps/support/portal/1088
But I can send email from the console like this:
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from#example.com',
['to#example.com'], fail_silently=False)
It return 1 and I actually got the email. If I do that from Mezzanine, I assumed it also return 1 according the redirect URL after the email was sent, but I don't get the email.
In addition to the settings mentioned in the Django documentation, try setting DEFAULT_FROM_EMAIL. You will probably want to use the same value as EMAIL_HOST_USER.
im trying to use django.core.mail to send emails using the default backend and it doesn't seem to be working. I've set up the email credentials, server, and port number in the settings file but whenever I try to run the send() method of an email message the command hangs indefinitely.
views.py
from django.core.mail import send_mail
def sending_email(request):
message = ""
subject = ""
send_mail(subject, message, from_email, ['to_email',])
Add this in settings.py
# Sending mail
EMAIL_USE_TLS = True
EMAIL_HOST='smtp.gmail.com'
EMAIL_PORT=587
EMAIL_HOST_USER='your gmail account'
EMAIL_HOST_PASSWORD='your gmail password'
I was having the same issue when trying to send via smtp.gmail.com with use_tls=True. It turns out I had the wrong port set. Here's what I'm doing now and it works:
from django.core.mail import get_connection
from django.core.mail.message import EmailMessage
connection = get_connection(use_tls=True, host='smtp.gmail.com', port=587,username='YourEmail#gmail.com', password='YourPassword')
EmailMessage('test', 'test', 'addr#from.com', ['addr#to.com'], connection=connection).send()