Error with sending emails with python using smtplib, ssl and bottle.py

Viewed 565

So im using https://realpython.com/python-send-email/#sending-a-plain-text-email

and im using this block of code

import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "my@gmail.com"  # Enter your address
receiver_email = "your@gmail.com"  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)

and i have changed some bits like the password and the mail for the things i need, how ever when i send the email i get this error

File "main.py", line 32, in sendmail
    with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
TypeError: __init__() got an unexpected keyword argument 'context'

when i try this code in a plain .py file it works but when i use itwith bottle.py it gives me that error, and i dont think its becasue of the other packages, any help would be gratly apreciated

2 Answers

@Sam H's comment is correct: use context as a positional param instead of passing it as a keyword param with context=context.

with smtplib.SMTP_SSL(smtp_server, port, context) as server:

Someone who i know did it for me today, idrk what he did but it worked, and when i did 'context' instead of 'context=context' it gave me another error, but its fine now

Related