0

I have the following code to send mails through python to an email address. It's not working when I'm putting my work email address in the 'toaddrs' variable. It doesn't give an error, but I don't get any mails.But I'm able to send mails from and to the same address from the gmail through the browser. Can anyone help me figure out why it's not working?

import smtplib


fromaddr = 'my_address@gmail.com'
toaddrs = 'my_work_address@workdomain.com'
msg = 'Hello!'

username = 'my_address@gmail.com'
password = 'mypassword'

server = smtplib.SMTP('smtp.gmail.com:25')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr,toaddrs,msg)
server.quit()
Vatsal Mishra
  • 109
  • 2
  • 10

1 Answers1

0

Check if you can send and retrieve the email when you send it to your own gmail address, i.e. toaddrs = fromaddr.

Your emails might get rejected by your work server because the header is missing, have a look at https://docs.python.org/3.5/library/email-examples.html.

Try changing msg to:

msg = ("From: %s\r\nTo: %s\r\nSubject: StackOverflow\r\n"
       % (fromaddr, toaddrs))
msg += 'Hello\n'
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99