1

I wrote a script that sends an email with the daily holiday. It works when run independently. I also wrote a script that sets a cronjob for it to run automatically, which correctly sets a cronjob, but the emails never send. The cronjob does work for other test scripts though. So both pieces work independently, just not for the email script. I checked the terminal mail and here's the error that arises which I don't know how to fix:

/anaconda3/lib/python3.7/getpass.py:91: GetPassWarning: Can not control echo on the terminal.
  passwd = fallback_getpass(prompt, stream)
Warning: Password input may be echoed.
Password for <bggoldberg33@gmail.com>: Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/getpass.py", line 69, in unix_getpass
    old = termios.tcgetattr(fd)     # a copy to save
termios.error: (25, 'Inappropriate ioctl for device')

I'm fairly new to Python so can anyone help explain what this means and how to resolve it?

Python version 3.7.3 and using command line to run on MacOS 10.13.4

Here are the email and cron scripts for reference:

import yagmail
import pandas as pd
from datetime import datetime

# create variables
df = pd.read_excel('/Users/bgoldberg/PythonScripts/FunStuff/holidays.xlsx')
df['DateCheck'] = df['Date'] == datetime.today().strftime('%m/%d')
df = df.loc[df['DateCheck'] == True]
df.reset_index(inplace=True)
if len(df) == 1:
    holiday1 = df.at[0, 'Holiday']
    link1 = df.at[0, 'Link']
    content = f'Happy {holiday1}! {link1}'
elif len(df) > 1:
    holiday1 = df.at[0, 'Holiday']
    link1 = df.at[0, 'Link']
    holiday2 = df.at[1, 'Holiday']
    link2 = df.at[1, 'Link']
    content = f'Happy {holiday1}! {link1} \nHappy {holiday2}! {link2}'
else:
    pass

weekday = datetime.today().strftime('%A')

# create email
receiver = 'bggoldberg33@gmail.com'
body = f'{content}'

yag = yagmail.SMTP('bggoldberg33@gmail.com')
yag.send(
        to = receiver,
        subject = 'Fun Daily Holidays',
        contents = body
)

Cronjob:

from crontab import CronTab

my_cron = CronTab(user='bgoldberg')
job = my_cron.new(command='/anaconda3/bin/python3 /Users/bgoldberg/PythonScripts/FunStuff/emailTest.py')
job.minute.every(1)
my_cron.write()
BenG
  • 116
  • 2
  • 11

1 Answers1

0

I've just come across this problem myself and managed to find the answer - hopefully it's still of use to you.

The problem is that the yagmail keychain entry you've created is in the login keychain, but cron cannot access the login keychain because it is not run from an interactive session. Cron can only access entries from the System keychain.

What you need to do is open the Keychain Access app and copy the yagmail entry from login to System. If you now try to run your cron job it should work.

This answer was of use in finding the solution.

Note that in theory you should also be able to do this from the command line using the macOS security command to export and import keychain entries, but I haven't tested this.

Alsy
  • 3
  • 3