Python Forum

Full Version: Problem in sending email using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import re
import smtplib
from email.mime.text import MIMEText
import cvs

fp = open('message.txt', 'rb')
msg = MIMEText(fp.read())
fp.close()
msg['Subject'] = 'Subject goes here'
msg['From'] = '[email protected]'

server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login('[email protected]', 'password')
email_data = csv.reader(open('email.csv', 'rb'))
email_pattern= re.compile("^.+@.+\..+$")
for row in email_data:
if( email_pattern.search(row[1]) ?
del msg['To']
msg['To'] = row[1]
try:
server.sendmail('[email protected]', [row[1]], msg.as_string())
except SMTPException:
print "An error occured."
server.quit()
I just did some amendment in this code and it is working but could you tell me how I set interval between each email sent because I don't want to send it once instead want to keep interval of 5 to 10 min in each email.

PS: If you could set email rotation then I would highly appreciate. Email rotation means suppose; I have one email format " Email format " and want to send it via email a , b,c ,d i mean just want a rotation first email sent by [email protected] then [email protected] again [email protected] .... then again a@gmail i mean like rotation of email id so that i can send more email with different email ids in same time.
Hello there, firstly i am random guy from internet trying to help
because of missing intents in source i assumed the code as following one =>
import time
...
for row in email_data:
    if( email_pattern.search(row[1]) ? del msg['To']:
       msg['To'] = row[1]
       try:
           server.sendmail('[email protected]', [row[1]], msg.as_string())
       except SMTPException:
           print "An error occured."
       time.sleep(600)
server.quit()
Line 1,10 came to my mind as simplest solution to problem but i am not sure if google will keep connection alive for 10 minutes.

Speaking of more reliable solution i would recommend a cron job that runs script every 5-10 minutes Cron job
And for user indexing i would use simple .ini or json configuration with list of users and index variable python configparser