Reputation: 119
I want to generate an email from within Python3 code. I can get it to work using a gmail account but not with my 1and1 Ionos account. This is the code
import smtplib
MAIL_2 = ['[email protected]']
FROM_ADDR = '[email protected]'
SUBJECT = 'Mail Subject via Ionos`'
BODY = 'Body of message'
PASSWORD = 'mypassword'
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 + '\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()
The error message is
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)
How can I get it to work?
Upvotes: 0
Views: 1758
Reputation: 21
Try the below code, it worked with me, you need to put the address of ionos server where you host your domain with, in my case it is Spain:
#send email
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
server = smtplib.SMTP('smtp.ionos.es', 587)
try:
name="Name" #You need to fill the name here
server.connect('smtp.ionos.es', 587)
server.starttls()
server.ehlo()
server.login("Youremailhostedonionos","password")
TOADDR = "[email protected]"
FromADDR = "Youremailhostedonionos"
msg = MIMEMultipart('alternative')
msg['Subject'] = "email subject here"
msg['From'] = FromADDR
msg['To'] = TOADDR
#The below is email body
html = """\
<html>
<body>
<p><span style="color: rgb(0,0,0);">Dear {0},</span></p>
<p>
your email body
</p>
<p>Kind Regards,<br />
Your name ....
</p>
</body>
</html>
""".format(name.split()[0])
msg.attach(MIMEText(html, 'html'))
server.sendmail(FromADDR, TOADDR, msg.as_string())
except Exception as e:
# Print any error messages to stdout
print(e)
finally:
server.quit()
Upvotes: 1