Python Forum
email code works in 2.7 but not in 3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
email code works in 2.7 but not in 3
#1
Question 
Running Python 3.8.10 and 2.7.18 under Mint 20.2
I am trying to generate an email on my IONOS 1 & 1 domain using this code
import smtplib
MAIL_2 = ['[email protected]']
FROM_ADDR = '[email protected]'
SUBJECT = 'Mail Subject via Ionos`'
BODY = 'Body of message'
PASSWORD = 'mypasswd'
SMTP_SERVER = "smtp.ionos.co.uk:587"
smtpserver = smtplib.SMTP(SMTP_SERVER)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(FROM_ADDR, PASSWORD)
header = 'To:' + ", " + MAIL_2[0] + '\n' + 'From: ' + FROM_ADDR + '\n' + 'Subject: ' + SUBJECT + '\n'
mmsg = header + '\n' + SUBJECT + '\n' + BODY + '\n\n'
smtpserver.sendmail(FROM_ADDR, MAIL_2,mmsg)
smtpserver.close()
If I run it in Python3 I get this error
Error:
Traceback (most recent call last): File "testmail-ionos01.py", line 14, in <module> smtpserver.starttls() File "/usr/lib/python3.8/smtplib.py", line 783, in starttls self.sock = context.wrap_socket(self.sock, File "/usr/lib/python3.8/ssl.py", line 500, in wrap_socket return self.sslsocket_class._create( File "/usr/lib/python3.8/ssl.py", line 1040, in _create self.do_handshake() File "/usr/lib/python3.8/ssl.py", line 1309, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: SSLV3_ALERT_ILLEGAL_PARAMETER] sslv3 alert illegal parameter (_ssl.c:1131)
If I run it in Python2.7 it works. I have looked with Wireshark and noticed that with 2.7 it uses TSLv1.3 but with 3.8 it uses TSLv1.2, I don't knw why or if this is relevant.
Also if I run similar code but using a gmail account it works in Python3.
Any idea how I can get this to work in Python3?
Thanks
Mick
Reply
#2
In case anyone has the same problem, this code works

import smtplib
MAIL_2 = ['[email protected]']
FROM_ADDR = '[email protected]'
SUBJECT = 'Mail Subject via Ionos`'
BODY = 'Body of message'
PASSWORD = 'mypasswd'
SMTP_SERVER = "smtp.ionos.co.uk:587"
smtpserver = smtplib.SMTP(SMTP_SERVER)
#smtpserver.ehlo()
#smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(FROM_ADDR, PASSWORD)
header = 'To:' + ", " + MAIL_2[0] + '\n' + 'From: ' + FROM_ADDR + '\n' + 'Subject: ' + SUBJECT + '\n'
mmsg = header + '\n' + SUBJECT + '\n' + BODY + '\n\n'
smtpserver.sendmail(FROM_ADDR, MAIL_2,mmsg)
smtpserver.close()
(Sorry but the insert code segment button does not seem to be working)
Note the two lines commented out. I don't know why this works but it does.
Larz60+ write Nov-04-2021, 05:14 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed, please don't forget
Reply
#3
You should use f-string for lines 13 and 14

replace these two strings with:
mssg = f"TO:, {MAIL_2[0]}\nFromL {FROM_ADDR}\nSubject: {SUBJECT}\n" \
    f"\n{SUBJECT}\n{BODY}\n\n"
Reply
#4
Thanks I was not aware of f-string, just read up on it and it looks really good.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 985 Nov-15-2023, 06:56 PM
Last Post: jst
  Code works but doesn't give the right results colin_dent 2 710 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Code used to work 100%, now sometimes works! muzicman0 5 1,427 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  Pyspark - my code works but I want to make it better Kevin 1 1,779 Dec-01-2021, 05:04 AM
Last Post: Kevin
  My simple code don't works !! Nabi666 1 1,601 Sep-06-2021, 12:10 PM
Last Post: jefsummers
  HackerRank Problem: Code works on VS Code but not on the HackerRank site Pnerd 3 2,635 Feb-28-2021, 07:12 PM
Last Post: Pnerd
  Need Outlook send email code using python srikanthpython 3 8,210 Feb-28-2021, 01:53 PM
Last Post: asyswow64
Question Google Foobar- Code works in my IDE but not in foobar. Static method? pr3ttykitty 4 4,939 Feb-24-2021, 05:03 PM
Last Post: nilamo
  yet another code that works on 2 but not in 3 rick666 1 2,017 Dec-12-2019, 08:12 PM
Last Post: Gribouillis
  Code that works on py2 and not in py3 rick666 3 2,846 Nov-29-2019, 01:37 PM
Last Post: rick666

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020