0

I'm trying to send an actual email to myself from my website using send_mail. I had used localhost and the following cmd command,

python -m smtpd -n -c DebuggingServer localhost:1025

in order to test it. It intercepts with no problem, but I don't see anything in my inbox.

My email provider works with SSL, so I turned it to True and enabled 'access to the mailbox using mail clients' in my mail settings.

Here is the settings.py file:

EMAIL_HOST = '178.68.164.41' # My current ip address
EMAIL_PORT = '465' # Port of the email host 
EMAIL_HOST_USER = 's...6@rambler.ru' # Here is my actual email 
EMAIL_HOST_PASSWORD = '.....' # Here is my actual password from my email login 

EMAIL_USE_TLS = False 
EMAIL_USE_SSL = True

Here is the views.py file:

    from django.shortcuts import render
    from django.core.mail import send_mail
    from .forms import ContactForm
    
    def contact(request):
        form = ContactForm
        if request.method == 'POST': 
            message_name = request.POST.get('name') 
            message_email = request.POST.get('email') 
            message = request.POST.get('message')
            send_mail(message_name, message, message_email, ['s****6@rambler.ru'])
        return render(request, 'contact.html', {'form': form})

Here's the exception:

Internal Server Error: /contact/
Traceback (most recent call last):
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\1\PycharmProjects\StasProject\sales_project\contact\views.py", line 12, in contact
    send_mail(message_name, message, message_email, ['sta6006@rambler.ru'])
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\__init__.py", line 61, in send_mail
    return mail.send()
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\backends\smtp.py", line 62, in open
    self.connection = self.connection_class(self.host, self.port, **connection_params)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 1043, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 1051, in _get_socket
    new_socket = self.context.wrap_socket(new_socket,
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1040, in _create
    self.do_handshake()
  File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()

Exception Type: ConnectionResetError at /contact/
Exception Value: [WinError 10054] Remote host forcibly terminated existing connection
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
Badabuska
  • 1
  • 2
  • 1
    Port 465 is specifically for TLS, but you have MAIL_USE_TLS set to false. You should be using port 587 with MAIL_USE_TLS set to True. – Tim Roberts Nov 28 '21 at 07:55
  • @TimRoberts I did that and now it says `SMTPConnectError at /contact/ (421, b'Cannot connect to SMTP server 178.68.174.185 (178.68.174.185:587), connect error 10061')` – Badabuska Nov 28 '21 at 08:02
  • 1
    Hmm, so maybe your mail server is behind the standards. So, go back to 465, but try leaving MAIL_USE_TLS at True. – Tim Roberts Nov 28 '21 at 08:06
  • @TimRoberts it's either 'forcibly terminated' or 'closed unexpectedly' that way – Badabuska Nov 29 '21 at 09:59
  • Your comment on the EMAIL_HOST line says "My current ip address". Is that literally true? Because That needs to be the host or address of your email server. – Tim Roberts Nov 29 '21 at 17:58
  • @TimRoberts yes. that was the issue. Thank you. Do you know how to make it so that gmail doesn't rewrite the 'from' email as my own? – Badabuska Dec 02 '21 at 14:15

0 Answers0