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:
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
- Make sure to turn ON Less secure app access -- I already did but I still got the error
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?