I always had the impression that Django email was behaving like an email client and that's why you have to specify an EMAIL_HOST, an EMAIL_HOST_PASSWORD and an EMAIL_HOST_USER. The Django app then behaves like an ordinary email client just like Outlook or Apple mail....
I am testing an application which has to send email every once in a while. but when in my app I execute these statements:
subject, from_email, to = 'hello', '[email protected]', '[email protected]'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.send()
I am getting...
Traceback (most recent call last):
File "<console>", line 1, in <module>
.
.
SMTPRecipientsRefused: {'[email protected]': (504, '5.5.2 <wmsdesktop>: Helo command rejected: need fully-qualified hostname')}
in which wmsdesktop is the name of my development system. From this error I get the impression that the Django app is behaving like a smtp server.
Or am I missing something here. Please shed some light on this...
Every email "client", when sending email, uses SMTP.
SMTP is how email is sent.
So yes, Django is acting as an SMTP client, not an email (POP or IMAP) client, when sending email.
It looks like the STMP server you're using restricts access to only other SMTP servers which have a fully-qualified domain name (example.com) which can be verified through a reverse DNS lookup.
This is a very common practice for SMTP servers, as it helps eliminate anonymous SMTP servers, which are often used for sending SPAM.
Generally speaking, this restriction doesn't apply for authenticated users, though that's dependent on the SMTP server.
Wright this code in your settings.py file
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'email password'
And After that use this code in your view.py file
def sendEmail(request):
subject, from_email, to = 'Hello', '[email protected]', '[email protected]'
text_content = 'Dear Sir,'
html_content = '<html><body><p>Hello World</p></body></html>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With