0

I have python code that looks like below. It works fine when executed manually. But when executed via a cronjob, the email is not being sent. Here is the code:

msg = MIMEMultipart()

msg['From'] = sender
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(message))

mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(login, password)
print mailServer.sendmail(login, to, msg.as_string())
sth
  • 222,467
  • 53
  • 283
  • 367

2 Answers2

0

Maybe your environment in Unix is different when executed manually/from cron.Do a "env >file" in cron and compare to env when running interactively

Mikpa
  • 1,912
  • 15
  • 20
0

In my case, it was os.getenv inside mailbody that didn't work.

Maybe the error occurs because cron script doesn't by default have any shell environment variables.

jhonkola
  • 3,385
  • 1
  • 17
  • 32
Logan
  • 1