Search code examples
python-3.xsslsmtpgmailsmtplib

Python smtplib.SMTPRecipientsRefused


I been trying to make a python program that sends email but i keep getting this error

Traceback (most recent call last):
  File "C:\Users\25194\PycharmProjects\Gmail\Yo.py", line 25, in <module>
    smtp.sendmail(email_sender, email_password, em.as_string())
  File "C:\Users\25194\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 901, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'google login': (553, b'5.1.3 The recipient address <Gmail app password> is not a valid RFC-5321\n5.1.3 address. Learn more at\n5.1.3  https://support.google.com/mail/answer/6596 k9-20020a7bc409000000b003c6bd91caa5sm17184983wmi.17 - gsmtp')}

The code is

from email.message import EmailMessage
import ssl
import smtplib

email_sender = '[email protected]'
email_password = 'google login'
email_reciver = '[email protected]'

subject = 'Check mate'
body = """
I am making this sending this isnt it cool oro
"""

em = EmailMessage()
em['From'] = email_sender
em['TO'] = email_reciver
em['Subject'] = subject
em.set_content(body)

context = ssl.create_default_context()

with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp:
    smtp.login(email_sender, email_password)
    smtp.sendmail(email_sender, email_password, em.as_string())

i am having hard time figuring out how to fix it


Solution

  • ... b'5.1.3 The recipient address is not a valid RFC-5321\n5.1.3 address. ...

    Obviously you are using a password in place where an email is expected

    smtp.sendmail(email_sender, email_password, em.as_string())
    

    From the documentation of sendmail:

    SMTP.sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=())
    

    So you give the email_password where to_addrs is expected. No wonder that it complains about your password used as recipient. You probably meant to use email_reciver instead.