but will receive the following error code occasionally:
[Errno -5] No address associated with hostname
Any idea why? My success rate of sending this email is probably 0-20%.
Is there a socket or timeout error? or??
Please advise. Many Thanks.
Code: Select all
#!/usr/bin/env python
import smtplib
from time import sleep
GMAIL_USER = 'XXX@gmail.com'
GMAIL_PASS = 'YYY'
SMTP_SERVER = 'smtp.gmail.com'
EMAIL_ADDRESS1 = 'ZZZ@gmail.com'
SMTP_PORT = 587
def send_email(recipient, subject, text):
try:
smtpserver = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(GMAIL_USER, GMAIL_PASS)
header = 'To:' + recipient + '\n' + 'From: ' + GMAIL_USER
header = header + '\n' + 'Subject:' + subject + '\n'
msg = header + '\n' + text + ' \n\n'
smtpserver.sendmail(GMAIL_USER, recipient, msg)
smtpserver.quit()
print ("Successfully SENT")
except Exception, msg:
print ("Failure: " + str(msg))
cnt = 0
while (cnt < 10):
send_email(EMAIL_ADDRESS1, 'Test Email', 'Thank You')
cnt = cnt + 1
sleep(5)