Your script seems like there it has some flawed monkey business going on. I'm too lazy to figure out exactly what that is. However, This works: Note that you will want to create an Application password for Windows in Gmail so that can send email from Windows machines. A lot of people get hung up on this part. Go here and follow the instructions. https://support.google.com/accounts/answer/185833?hl=en Done correctly, Google will give you something like: hjsjdhuhfkjcjfhfqq Use this application in place of YOURPASSWORD Don't forget the quotes around 'YOURPASSWORD'