Skip to content
Advertisement

Django: Gmail SMTP error: please run connect() first

I am trying to send mail when a certain query is executed. But I am getting error in the connection. I have tried the following settings in my settings.py file

server = smtplib.SMTP('smtp.gmail.com')
server.starttls()
EMAIL_HOST_USER = "email@gmail.com"
EMAIL_HOST_PASSWORD = "password"
server.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)

I have executed the following command to send the email:

emailFrom = [settings.EMAIL_HOST_USER]
html_content = render_to_string('template.html', {'greeting_text': greeting_text})
text_content = strip_tags(html_content)
email_subject = "Subject"
msg = EmailMultiAlternatives(email_subject,text_content,emailFrom,[email],)
msg.attach_alternative(html_content, "text/html")
msg.send()

But whenever I run the above code I get ‘please run connect() first’ error. What is the error Exactly about and how do I solve this?

Advertisement

Answer

Make sure you have all the email settings configured:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement