0

I have a simple python program that simply sends a simple email.

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders


def send_email(sender,sender_pass,receiver,mail_content,subject):
    '''Parameters are already some of the needed data'''

    #Setup the MIME
    message = MIMEMultipart()

    message['From'] = sender
    message['To'] = receiver
    message['Subject'] = subject

    message.attach(MIMEText(mail_content, 'plain'))

    #Create SMTP session for sending the mail
    session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
    session.starttls() #enable security
    session.login(sender, sender_pass) #login with mail_id and password
    text = message.as_string()
    session.sendmail(sender, receiver, text)
    session.quit()
    print('Mail Sent')

if __name__ == "__main__":
    send_email(
    "xxxxsenderxxxxxx@gmail.com",
    "xxxxxpasswordxxxxxxxx",
    "xxxxreceiverxxx@gmail.com",
    "Hello this is a test email for the purpose of testing",
    "Email Test - Thank you"
    )

Now when I run this program on my personal computer (Ubuntu). It is working perfectly fine. I even got the email

Email received:

enter image description here


But when I run the program on a server (Amazon LightSail Server) (Cpanel is the Interface) it doesn't work and I get this error.

 File "/opt/alt/python38/lib64/python3.8/smtplib.py", line 723, in login
    (code, resp) = self.auth(
  File "/opt/alt/python38/lib64/python3.8/smtplib.py", line 646, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'Incorrect authentication data')

Notes

  1. Make sure to turn ON Less secure app access -- I already did but I still got the error

enter image description here


Any possible solutions for this problem? I am very very confused to why because it works perfectly when I run it on my computer but when I run it on the server it doesn't. I get the error specified above. Also I have done this before actually (YEAR 2020) SAME CODE and it worked but the server or hosting company was namecheap but I don't see if the company that provides the service matters. Could it be because there is a configuration to be done on the server? Is there something need to be change on the code?

Ice Bear
  • 2,676
  • 1
  • 8
  • 24
  • As an aside, your code seems to be written for Python 3.5 or earlier. The `email` library was overhauled in 3.6 and is now quite a bit more versatile and logical. Probably throw away what you have and start over with the [examples from the `email` documentation.](https://docs.python.org/3/library/email.examples.html) – tripleee Jan 24 '22 at 10:58
  • will try thanks! I'll give some updates as well. – Ice Bear Jan 24 '22 at 11:03
  • 1
    Does this answer your question? ["Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer](https://stackoverflow.com/questions/14297264/password-not-accepted-from-server-535-incorrect-authentication-data-when-send) (it's a PHP question but it should not matter; the culprit seems to be cPanel in both cases). – tripleee Jan 24 '22 at 11:04
  • just a sec, this could be it but I am having trouble finding the "tweak" – Ice Bear Jan 24 '22 at 11:12
  • This seems to be for php and can be a potential solution but I haven't tried it yet as I can't find the "tweak" section. I think the interface is pretty different – Ice Bear Jan 24 '22 at 11:21
  • Alas, I have no experience with cPanel, other than as a flabbergasted observer from a safe distance. – tripleee Jan 24 '22 at 11:24
  • Amazing! I have a new issue but this issue is solved. Thanks to [this](https://stackoverflow.com/a/67383252/14425271) – Ice Bear Jan 25 '22 at 10:45

0 Answers0