Django send_mail via gmail is very slow

When trying to send via. The /manage.py shell takes several minutes to send one email. When I try to send a confirmation email to the user after submitting the form in the browser, the browser disconnects from 504, but the email is ultimately sent. What could be?

settings.py

... EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = ' myemail@gmail.com ' EMAIL_PORT = 587 EMAIL_USE_TLS = True DEFAULT_FROM_EMAIL = EMAIL_HOST_USER EMAIL_HOST_PASSWORD = os.environ.get('PASSWORD') ... 

views.py

 class SignUpView(CreateView): model = User template_name = 'eventMap/register.html' form_class = RegistrationForm success_url="/" def form_valid(self, form): form.save() username = form.cleaned_data['username'] email = form.cleaned_data['email'] salt = hashlib.sha1(str(random.random())).hexdigest()[:5] activation_key = hashlib.sha1(salt+email).hexdigest() key_expires = datetime.datetime.today() + datetime.timedelta(2) #Get user by username user=User.objects.get(username=username) # Create and save user profile new_profile = UserProfile(user=user, activation_key=activation_key, key_expires=key_expires) new_profile.save() # Send email with activation key email_subject = 'Account confirmation' email_body = "Hey %s, thanks for signing up. To activate your account, click this link within \ 48hours http://mywebsite.com/accounts/confirm/%s" % (username, activation_key) send_mail(email_subject, email_body, ' myemail@gmail.com ', [' mytestemail@gmail.com '], fail_silently=False) return super(SignUpView, self).form_valid(form) 

I came across this post about something similar, but the logs don't mention anything about an unqualified host name, etc. /var/log/mail.log

 Jul 27 16:26:04 django postfix/qmgr[5975]: CAF7C1226F2: from=<>, size=3063, nrcpt=1 (queue active) Jul 27 16:26:34 django postfix/smtp[12874]: connect to example.com[2606:2800:220:1:248:1893:25c8:1946]:25: Connection timed out Jul 27 16:27:04 django postfix/smtp[12874]: connect to example.com[93.184.216.34]:25: Connection timed out Jul 27 16:27:04 django postfix/smtp[12874]: CAF7C1226F2: to=< myemail@example.com >, relay=none, delay=368178, delays=368118/0.02/60/0, dsn=4.4.1, status=deferred (connect to example.com[93.184.216.34]:25: Connection timed out) 
+5
source share
2 answers

I had a similar case where the delay and timeout were caused by a system trying to connect to gmail via IPv6.

This thread has been helpful in unlocking secrets.

TL; DR:

On the host machine, try using telnet to connect to gmail.

 telnet smtp.gmail.com 587 

If it first tries to connect via IPv6 (and blocks for several minutes), followed by a successful connection to IPv4, this is most likely your problem.

Decision:

 import socket EMAIL_HOST = socket.gethostbyname('smtp.gmail.com') 

This ensures that python will connect to gmail over IPv4 from the start.

The best solution:

Indicate why your host cannot connect to gmail via IPv6 and fix this problem. Perhaps setting up firewall rules to reject packets rather than drop them.

+6
source

You may not want to send emails via gmail. It really is not intended for a standard smtp service such as Gmail.

Have you tried setting up email alerts to start through a mail distribution service such as Mandrill to start with?

There are several Django applications, such as Djrill , that "just work" using standard Django email features such as send_mail ().

-1
source

All Articles