Email Sending In Django not working

209 views
Skip to first unread message

peteru mimo

unread,
Nov 9, 2022, 5:55:32 AM11/9/22
to Django users
Good day team,

I have been facing this challenge in sending mail in django

here is the error "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond"

Code
def send_verification_email(request, user):
    from_email = settings.DEFAULT_FROM_EMAIL
    current_site = get_current_site(request)
    mail_subject = 'Please activate your account'
    message = render_to_string('accounts/emails/account_verification_email.html', {
        'user': user,
        'domain': current_site,
        'uid': urlsafe_base64_encode(force_bytes(user.pk)),
        'token': default_token_generator.make_token(user),
    })
    to_email = user.email
    mail = EmailMessage(mail_subject, from_email, message, to=[to_email])
    mail.send()

Settings
EMAIL_HOST = 'smtp.google.com'
EMAIL_POST = '587'
EMAIL_HOST_USER = 'myemail'
EMAIL_HOST_PASSWORD = 'email_password'
EMAIL_USE_TLS = True

Any solution will be appreciated. Thank you

peteru mimo

unread,
Nov 9, 2022, 6:13:29 AM11/9/22
to django...@googlegroups.com
EMAIL_PORT = '587'

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1ed5b9fc-3370-413c-b4cb-edeb2451d8f4n%40googlegroups.com.

Lakshyaraj Dash

unread,
Nov 9, 2022, 6:15:54 AM11/9/22
to django...@googlegroups.com
It's more likely that google is not accepting the credentials you are providing or else the smtp server has just hung up due to several requests.

You can do it without using the django.core.mail. I've written an article recently on dev.to

Follow up and hope the article helps you

Thanks and Regards
Lakshyaraj Dash


Jitendra kumar Patra

unread,
Nov 9, 2022, 6:32:45 AM11/9/22
to django...@googlegroups.com
Please use  int type EMAIL_PORT = 587

It's Aladdin

unread,
Nov 9, 2022, 10:32:19 AM11/9/22
to django...@googlegroups.com
Google hast stopped less secure apps, so u cant send email messages anymore with ur email and password, U will have to find some other way of sending mails.

Lakshyaraj Dash

unread,
Nov 9, 2022, 10:34:49 AM11/9/22
to django...@googlegroups.com
Even if Google has stopped the support, emails can be still sent using two step verification and generating app passwords after enabling two step verification. Please update you email password with the generated app password.

Steps:
1. Go to manage my Google account
2. Go to security tab
3. Enable two step verification and verify yourself
4. Create your app passwords after the app passwords in the security settings is visible
5. Select the app name (or add your custom app name) and click generate.
6. Copy the app password as it's view once and cannot be viewed after the pop up is closed.
7. Go to your settings.py file and replace your actual email password with the generated app password.

Yay! You're all set.

Dear It's Alladin, please try this also to the one who asked this question please try it once.

Thanks and Regards
Lakshyaraj Dash

Ahmed omar miladi

unread,
Nov 9, 2022, 10:36:36 AM11/9/22
to django...@googlegroups.com
you can use the function send mail of django

Téléchargez Outlook pour iOS

De : django...@googlegroups.com <django...@googlegroups.com> de la part de It's Aladdin <iamala...@gmail.com>
Envoyé : Wednesday, November 9, 2022 12:06:33 PM
À : django...@googlegroups.com <django...@googlegroups.com>
Objet : Re: Email Sending In Django not working
 

Lakshyaraj Dash

unread,
Nov 9, 2022, 10:37:53 AM11/9/22
to django...@googlegroups.com

Lakshyaraj Dash

unread,
Nov 9, 2022, 10:40:04 AM11/9/22
to django...@googlegroups.com
Dear It's Alladin and the one who asked this question

Please watch this video

On Wed, Nov 9, 2022, 20:57 It's Aladdin <iamala...@gmail.com> wrote:

peteru mimo

unread,
Nov 14, 2022, 7:11:07 AM11/14/22
to Django users
I have done all this still not working

Lakshyaraj Dash

unread,
Nov 14, 2022, 7:17:01 AM11/14/22
to django...@googlegroups.com
Please watch this video and ensure that you've properly configured the port numbers also your email address and password.
Note: Your email password should be the app password generated no your actual Gmail password


peterumimo2

unread,
Nov 14, 2022, 7:42:03 AM11/14/22
to django...@googlegroups.com
I have watched the video please the app name must be any name or your project name



Sent from my Samsung Galaxy smartphone.

Lakshyaraj Dash

unread,
Nov 14, 2022, 7:43:24 AM11/14/22
to django...@googlegroups.com
The app name can be any name

peteru mimo

unread,
Nov 14, 2022, 9:02:51 AM11/14/22
to Django users
Thank you all i have figure it out.  EMAIL_HOST = 'smtp.google.com' instead of  EMAIL_HOST = 'smtp.gmail.com'

It's Aladdin

unread,
Jan 24, 2023, 11:48:24 PM1/24/23
to django...@googlegroups.com
guys use this code for sending emails, it's working as of now
import smtplib

my_email = "som...@gmail.com"
password = "sfuyyudfgyudgfydsg"

with smtplib.SMTP('smtp.gmail.com') as connection:
connection.starttls()
connection.login(user=my_email, password=password)
connection.sendmail(
from_addr=my_email,
to_addrs="samaniabd...@gmail.com",
msg="Subject: Checking out automated mail in Python\n\nI don't
know what error I will get, there is only one way to find out.."
)
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF7qQgCogwDfQd%2BzfQEJXqKyqg3g9SHar%2B%2BmqyPrYLmvswxTdA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages