Django didnt receive email - python

i am trying to send email from the website that i create. It does not display any error but there are no email that I received.
here is my setting code
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'my email's pswrd'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Here is the function
from django.core.mail import send_mail
def sentmes(request):
if request.method == "POST":
name = request.POST.get('name')
email = request.POST.get('email')
subject = request.POST.get('subject')
message = request.POST.get('message')
data = {
'name': name,
'email': email,
'subject': subject,
'message': message
}
mail = '''
New message: {}
From: {}
'''.format(data['message'], data['email'])
send_mail(data['subject'], mail, ' ', ['myemail#gmail.com'], fail_silently=False)
return render(request, ' ')
this is the output that i get
[14/Jun/2021 06:52:15] "POST / HTTP/1.1" 200 18516
Where is my mistake. Hope someone can help. Thank you.

You should have a closer look at the user's guide.
https://docs.djangoproject.com/fr/3.2/topics/email/
A good check is to use the console to have a feedback, while testing.
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
and to test your parameters via the shell.
If it works, switch to smtp,
It it works, then implement your function.
As you use the smtp backend, make sure that all the parameters are the right ones.

Related

How to send Email in Django using office365

I am working in a project where I need to send the email to the user who is filling the Email in the form. I am able to send email by using my Gmail Account details but while using outlook.365. This is the image with error that I am getting.
My requirement is :
Once users come in the registration form and fill the details as Name, Email, Mobile
Whatever Email is put there in the form, send the email with link to create new password.
Here is my code:
settings.py:
# Using outlook365:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'support#academic.com'
EMAIL_HOST = 'smtp.outlook.office365.com' # (also tried : smtp.office365.com and
outlook.office365.com)
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = 'My_Password'
# Using my gmail account:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'admin#gmail.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = 'My_Password'
views.py:
subject = "Academic - Create New Password"
message = "Hi %s! Please create your new Password here : http://127.0.0.1:8000/api/create-password" % firstname
send_mail(
subject, # Subject of the email
message, # Body or Message of the email
'support#academic.com', # from#gmail.com (admin#gmail.com for gmail account)
[email], # to#gmail.com # email that is filled in the form
)
Please guide me that what extra setting or configuration I need to send email using Outlook365.
Please put this in settings.py file.
SERVER_EMAIL = EMAIL_HOST_USER
Your settings look correct. Use EMAIL_HOST = "smtp.office365.com". Verify that you are using the correct credentials. Check that your outlook account is set up properly to send emails from support#academic.com.

Django does not send emails via smtp

I'm trying to send a email via smtp using Django. When I try to send the email, I do not get any error, but my application does not respond and it keeps waiting on to send the email, but obviously it does not send nothing.
I have tried with smtp gmail and smtp hotmail, but it is not working. I have already checked my Windows firewall, and again it is not working. I have tried to send the email using Python shell but it does not send nothing.
I think that I have tried almost everything that I saw on the other posts here in Stack Overflow.
settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.office365.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myemail#hotmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
SERVER_EMAIL = EMAIL_HOST_USER
mails.py:
from django.conf import settings
from django.template.loader import get_template
from django.core.mail import EmailMultiAlternatives
class Mail:
#staticmethod
def send_complete_order(orden, user):
subject = 'order sent'
template = get_template('orders/mails/complete.html')
content = template.render({
'user': user
})
message = EmailMultiAlternatives(subject, 'Testing',
settings.EMAIL_HOST_USER, [user.email])
message.attach_alternative(content, 'text/html')
message.send()
views.py:
#login_required(login_url='login')
def complete(request):
cart = get_or_create_cart(request)
order = get_or_create_order(cart, request)
if request.user.id != order.user_id:
return redirect('carts:cart')
order.complete()
Mail.send_complete_order(order, request.user)
destroy_cart(request)
destroy_order(request)
messages.success(request, "order complete")
return redirect('index')
Create a secure SSL context
context = ssl.create_default_context()
try:
with smtplib.SMTP_SSL(smtp_mail, port, context=context) as server2:
server2.login(your_mail, passwort)
server2.sendmail(mail_from, mail_to, message.as_string())
print("Successfully sent email")

django email not being sent : ConnectionRefusedError WinError 10061

I am writing django application and trying to send email using it I have "Access to low security app" in gmail enabled and django setting are given below which I think are right. but I am still getting error as mentioned it title. I dont know the problem but I know I am not getting logged in to send email.
Edit 1: I have made two changes first in settings.py and second in views.py in views.py I replaced email entered by user to mine(syedfaizan824#gmail.com) and settings.py DEFAULT_EMAIL_FROM is changed form testing#example.com to syedfaizan824#gmail.com
searched on internet and find out that gmail does not allow low security app to login by default but I turned it off in setting of gmail. find out that my email backend was wrong so made it right. fail silently is False.
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = 'syedfaizan824#gmail.com'
EMAIL_HOST_USER = 'syedfaizan824#gmail.com'
EMAIL_HOST_PASSWORD = '******'
views.py
def post(self, request, *args, **kwargs):
form = contact(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
phone = form.cleaned_data['phone']
organization = form.cleaned_data['organization']
message = form.cleaned_data['message']
ref_code = form.cleaned_data['ref_code']
plan = form.cleaned_data['plan']
message = message + ref_code
send_mail(
'from website' + name + " " + organization,
message,
'syedfaizan824#gmail.com',
['syedfaizan824#gmail.com'],
fail_silently=False,
)
print("sent")
else:
#print('something is wrong with forms!')
return render(request, self.template_name, self.context)
Error message is ConnectionRefusedError WinError[10061]. and statement of error is : No connection could be made because the target machine actively refused it. which means I am not getting logged in.
change
EMAL_HOST = 'smtp.gmail.com'
to
EMAIL_HOST = 'smtp.gmail.com'
You are missing the 'I' character in the word 'EMAIL'

How to send email to official account from logged in user in django?

I am trying to send email from the user who is logged in to an account which I have given bilalkhangood4#gmail.com. But I have typed it manually. There could be more accounts means more users from which I want to send. But how could I send email from just those users who are logged in? Basically it is an APP where a user will click on withdraw button to request and send mail to official admin account.
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'bilalkhangood4#gmail.com'
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 587
ACCOUNT_EMAIL_VERIFICATION = 'none'
EMAIL_USE_SSL = False
views.py
from django.contrib.auth.models import User
def ViewSendEmail(request):
send_mail('Hello from Bilal Khan',
'Hello there, This is an automated message.',
'bilalkhangood6#gmail.com', #FROM
['bilalkhangood4#gmail.com'], #TO
fail_silently=False)
return render(request, 'nextone/email_send.html')
use request.user.email to access the email address of logged in user.
update your code with following:
def ViewSendEmail(request):
send_mail('Hello from Bilal Khan',
'Hello there, This is an automated message.',
request.user.email, #FROM
['bilalkhangood6#gmail.com'], #TO
fail_silently=False)
return render(request, 'nextone/email_send.html')

Email message isn't sent by django on hosting (502 Bad Gateway) but works on localhost

Email message isn't sent by django on hosting (502 Bad Gateway), but it works on my localhost. It also works if i send mail from ssh in shell.
This is a view handle form and send data on email, but it works only on my localhost.
On hosting this view, there is an interrupt on the email.send() function - 1 minute loading and 502 BAD GATEWAY nginx error appears.
def ContactsView(request):
form_data = AddContactsForm()
if request.method == 'POST':
form_data = AddContactsForm(request.POST)
if form_data.is_valid():
subject = u'Форма обратной связи'
recipient = ['snab-service.com#yandex.ru']
sender = 'snab-service.com#yandex.ru'
message = u'Имя: ' + form_data.cleaned_data['name'] + '\n'
if form_data.cleaned_data['phone']:
message += u'Телефон: ' + form_data.cleaned_data['phone'] + '\n'
message += u'Email: ' + form_data.cleaned_data['email'] + '\n'
if form_data.cleaned_data['message']:
message += u'Сообщение: ' + form_data.cleaned_data['message']
email = EmailMessage(subject=subject, body=message, from_email=sender, to=recipient)
email.send(fail_silently=False)
return HttpResponseRedirect('/contacts/')
return render(request, 'pages/contacts.html', context = {'form': form_data}, content_type = 'text/html')
My email settings found in in settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.yandex.ru'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'snab-service.com'
EMAIL_HOST_PASSWORD = '***********'
SERVER_EMAIL = 'snab-service.com#yandex.ru'
DEFAULT_FROM_EMAIL = 'snab-service.com#yandex.ru'
What can I do to solve this problem? I have tested email send functions in python manage.py shell in ssh and it works.

Categories