port = 587
smtp_server = 'smtp.mail.me.com'
sender_email = 'sender@icloud.com'
recipient_email = 'recipient@icloud.com'
password = 'app-specific-password' # For more on this: https://support.apple.com/en-gb/HT204397#:~:text=App%2Dspecific%20passwords%20are%20passwords,services%20not%20provided%20by%20Apple.
subject = 'Test'
# The message must have the sender and recipient email in
message = f'From: {sender_email}
To: {recipient_email}
Subject: {subject}
This is a test'
# IF the following line doesn't work
context = ssl.create_default_context()
# Use:
context = ssl._create_unverified_context()
with smtplib.SMTP(smtp_server, port) as server:
try:
server.ehlo()
server.starttls(context=context)
server.ehlo()
server.login(sender_email, password)
server.sendmail(
sender_email,
recipient_email,
message)
print('email sent!')
except:
print('email did not send')