2

I'm asked to add a feature to an existing program which is implemented using the Django/Python framework. This feature will allow the user to click on a button which it will show a small dialog/form to enter a value.

I did write some code which shows a message that the email is sent but in reality, it doesn't send it!

My code:

from django.shortcuts import render
from django.core.mail import send_mail


# Create your views here.
def index(request):

    send_mail('Request for a Clause',
    'This is an automated email. Jeff, please submit the case for 1234567',
    'akohan@mycompay.com',
    ['jjohnson@mycompany.com'],
    fail_silently=False)

    return render(request, 'send/index.html')

In the project root, in the setting.py I have added SMTP configuration:

EMAIL_HOST = 'mail.mycompany.com'
EMIAL_PORT = 587

#EMAIL_HOST_USER = 'akohan@mycompany.com'  ;no need it is on the white list
#EMAIL_HOST_PASSWORD = '' ;no need it is on the white list

EMAIL_USE_TLS = True
EMAIL_USE_SSL = False

I run it by typing:

python manage.py  SendEmailApp

What am I missing here?

SiHa
  • 7,830
  • 13
  • 34
  • 43
amit kohan
  • 1,612
  • 2
  • 25
  • 47
  • To rule out the obvious, did you double and triple check the email credentials, the SMTP configuration, and your spam folder? – ubadub Sep 20 '18 at 17:21
  • Also, what do you mean you ran it by typing "python manage.py SendEmailApp"? This is not how you test a website with django. – ubadub Sep 20 '18 at 17:22
  • Greetings, @ubadub yes, there is nothing in the Spam folder and as far as SMTP configuration I ran it with the IT Dept. I was not sure about TLS and SSL values. Of course, both cannot be set True since they are mutually exclusive. So I set SSL=True and TLS=False then it threw the following error: self._sslobj.do_handshake() ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1045) [20/Sep/2018 10:32:22] "GET / HTTP/1.1" 500 119908 Now, the only possible setting should be SSL=False and TLS=True and this time I get: [20/Sep/2018] "GET / HTTP/1.1" 200 269 – amit kohan Sep 20 '18 at 17:36
  • Again, I'm new to Python but from what I see after writing/updating a code in the command line I type: python manage.py ProjectName – amit kohan Sep 20 '18 at 17:37
  • What is the value of `EMAIL_BACKEND` in `settings.py`? Often it is set to 'django.core.mail.backends.console.EmailBackend' for DEBUG mode, so that emails sent show up in the console and don't spam people while testing. In production you will need to change that to 'django.core.mail.backends.smtp.EmailBackend' – dirkgroten Sep 20 '18 at 17:39
  • @dirkgroten, Thank you! good point. That should be generated by Django itself. I will take that into consideration. – amit kohan Sep 20 '18 at 17:47

1 Answers1

0

Personally, I have sent emails before in this way in a Django project and it worked great. You will have to allow SMTP access to your email.

import smtplib

def sendEmail():

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login('yourEmailAddress@gmail.com', 'yourEmailPassword')

    try:
        server.sendmail('yourEmailAddress@gmail.com', 'emailAddressBeingSentTo', 'messageBeingSent')
    except:
        print('An error occurred when trying to send an email')

    server.quit()

Side note. Security was not an issue for me so I did not check into it.

Hope this helps :)

Kenton Parton
  • 70
  • 1
  • 5
  • I tried this method before and it sends the email but the problem was that it would NOT send the body. I mean the receiver would get the email but no message in it. Any idea why? if this resolves the problem then I will go with your solution. – amit kohan Sep 20 '18 at 18:16
  • This solved the issue for me but email is received blank. – amit kohan Sep 20 '18 at 21:05
  • 1
    If there is a message it shouldn't be empty. Try looking into this Amit. https://www.pythonforbeginners.com/code-snippets-source-code/using-python-to-send-email – Kenton Parton Sep 22 '18 at 22:30
  • The above issue is resolved but now I need to call this routine by clicking on a button which is on JQuery dialog/form? – amit kohan Sep 24 '18 at 19:54