Sending email using smtplib doesn't work anymore

2.7k views Asked by At

So yesterday I had this bit of code written out and it worked perfectly fine, but today it's not sending e-mails anymore. Can someone explain why?

import smtplib

SERVER = 'owa.server.com'
FROM = '[email protected]'
TO = ['[email protected]', '[email protected]']

name = 'Mr. Man'
SUBJECT = 'Recent Information for: %s' % (name)
TEXT = "Dear " +name+ ",\n\nHello.\n\nSincerely,\nOur Guys Here"

message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

server = smtplib.SMTP(SERVER, 587)
server.ehlo()
server.starttls()
server.ehlo
server.login('[email protected]', 'password')
server.sendmail(FROM, TO, message)
server.quit()
2

There are 2 answers

0
Ravaal On BEST ANSWER

This code is a working snippet. I wasn't getting the e-mails in my personal gmail account because gmail was sending it to the spam folder. I checked to see if it works at my office account, and it did just fine.

1
taesu On
import smtplib

# Specifying the from and to addresses

fromaddr = '[email protected]'
toaddrs  = '[email protected]'

# Writing the message (this message will appear in the email)

msg = 'Enter you message here'

# Gmail Login

username = 'username'
password = 'password'

# Sending the mail  

server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

Above standard smtp send works with gmail,
thus it must be your server(whatever you're using) configuration that is at fault.