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
JavaScript
x
6
1
server = smtplib.SMTP('smtp.gmail.com')
2
server.starttls()
3
EMAIL_HOST_USER = "email@gmail.com"
4
EMAIL_HOST_PASSWORD = "password"
5
server.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
6
I have executed the following command to send the email:
JavaScript
1
8
1
emailFrom = [settings.EMAIL_HOST_USER]
2
html_content = render_to_string('template.html', {'greeting_text': greeting_text})
3
text_content = strip_tags(html_content)
4
email_subject = "Subject"
5
msg = EmailMultiAlternatives(email_subject,text_content,emailFrom,[email],)
6
msg.attach_alternative(html_content, "text/html")
7
msg.send()
8
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:
JavaScript
1
4
1
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
2
EMAIL_HOST = 'smtp.gmail.com'
3
EMAIL_USE_TLS = True
4