+ 3

SMTPlib with Python

I have made a script that should send an email with python using smtplib. Here it is: import smtplib msg = "test email" mail = smtplib.SMTP("smtp.gmail.com",578) mail.ehlo() mail.starttls() mail.login("myemail@gmail.com", "mypassword") mail.sendmail("fromemail@gmail.com","toemail@gmail.com",msg) mail.close() NOTE: I have replaced my actual credentials for this code, they are correct, that is not the issue. This is my error: Either nothing happens, and it just sits there, no error showing, not ending the script, or it doesn't accept my username and password.

10th Mar 2019, 2:38 PM
Paul Grasser
Paul Grasser - avatar
17 Answers
+ 7
Yay ;)
10th Mar 2019, 5:13 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
Usually, the port for SMTP is 587, like Diego suggests. Also, check for spelling mistakes. Eventhough this is just an example login, its domain is gmai.com instead of gmail.com
10th Mar 2019, 4:00 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
You wrote myemail@gmai.com when it should be myemail@gmail.com
10th Mar 2019, 4:18 PM
Diego
Diego - avatar
+ 4
Try with the login only, no @gmail.com
10th Mar 2019, 4:28 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
Oh, one more thing -- gmail has a strict security policy and the default settings of each email account DO NOT allow unauthorized applications to connect *at all* You have to change your security settings perhaps? https://support.google.com/accounts/answer/6010255
10th Mar 2019, 4:30 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
Can you post the code at the code playground? (delete the password first of course :) I have published my code that works (outside Sololearn) and this part of code is pretty similar. I wonder what might be your problem... https://code.sololearn.com/c03tR6z8U0NB/?ref=app
10th Mar 2019, 4:38 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 3
Ok, got an "unauthorized access" message and a hint to log in via web browser and change the settings. After that, I was able to successfully send an email using your script.
10th Mar 2019, 4:57 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 3
So what kind of error are you receiving now?
10th Mar 2019, 5:04 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 2
Try using port 587 instead of 578, and call mail.ehlo() again after mail.starttls().
10th Mar 2019, 3:36 PM
Diego
Diego - avatar
+ 2
Hmmm. That's strange. Now it works. đŸ€”đŸ€”đŸ€”đŸ€”đŸ€”đŸ€”
10th Mar 2019, 5:06 PM
Paul Grasser
Paul Grasser - avatar
+ 1
I found that earlier and already did that, didn't change anything apparently
10th Mar 2019, 4:31 PM
Paul Grasser
Paul Grasser - avatar
0
So I should change it to smtp.gmai.com? if I do that then it just doesn't do anything, and when I cancel it it got stuck in create connection.
10th Mar 2019, 4:15 PM
Paul Grasser
Paul Grasser - avatar
0
Ohhhh.... ok I changed that it still doesn't work Username and Password not accepted
10th Mar 2019, 4:19 PM
Paul Grasser
Paul Grasser - avatar
0
Same error
10th Mar 2019, 4:28 PM
Paul Grasser
Paul Grasser - avatar
10th Mar 2019, 4:45 PM
Paul Grasser
Paul Grasser - avatar
0
I did that too, look at my feed, put image there
10th Mar 2019, 4:59 PM
Paul Grasser
Paul Grasser - avatar
0
Wait, I'll post another picture
10th Mar 2019, 5:04 PM
Paul Grasser
Paul Grasser - avatar