I recently found a code to send emails using python. It was only for a single user so I modified it to take emails from a txt file which stores the email on every line and then send them mails. However what I found is that the mails end up in the spam folder(in case of Gmail) or the Junk folder (in case of hotmail or live). Is it possible to change the code so that the message lands in the inbox instead of being filtered as spam? Did I get something wrong?
import smtplib,sys
server = 'smtp.gmail.com'
port = 587
sender = 'my-username@gmail.com'
subject = 'Gmail SMTP Test'
body = 'blah blah blah'
"Sends an e-mail to the specified recipient."
session = smtplib.SMTP(server, port)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, 'my-password!')
f = open('emails.txt')
for line in f:
recipient = line
print recipient
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient]
headers = "\r\n".join(headers)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
f.close()
session.quit()
That's a very difficult question, because the spam classification is not done by you. (Obviously! If anyone could make their messages "not spam" then of course the spammers would do that too.)
There are various things you should do if you are seriously thinking about sending large-scale email, involving authenticating servers etc. Unless you are an expert, you should engage the services of a mailing company to do them.