This one is a puzzler....
What Do I Expect?
A program that runs perfectly in Python Anywhere console (or PyCharm for that matter) should run as a Python Anywhere task successfully.
What Is Happening?
When I run this same file as a task however I get the following error:
File "Birthday_Wisher/day_032_Birthday_Wisher.py", line 25 email_connection.sendmail(from_addr=MY_EMAIL, to_addrs=to_email, msg=f"Subject:Happy Birthday!\n\n{email_message}")
^ SyntaxError: invalid syntax
2021-05-25 22:10:58 -- Completed task, took 5.79 seconds, return code was 1.
The thing is, the above error does not make sense given that the code works in two different consoles with unchanged files or code. If this was a valid error it wouldn't run in PC or PA console. Something is off and this is not working as expected.
The function in question is as follows. Posting the entire program is moot as it is working with multiple files and works perfectly in two other environments, including the PA console. The code is not broken. Task automation is not playing nice with this code though...
def email_letter(email_message, to_email):
"""Receives the finalized email message and uses it to send the birthday email.
Error handling will return a 'somewhat' coherent error code and message."""
global birthday_list
global emails_sent
try:
with smtplib.SMTP(MY_SMTP, port=587) as email_connection:
email_connection.starttls()
email_connection.login(user=MY_EMAIL, password=MY_PASSWORD)
email_connection.sendmail(from_addr=MY_EMAIL, to_addrs=to_email, msg=f"Subject:Happy Birthday!\n\n{email_message}")
except smtplib.SMTPResponseException or smtplib.SMTPRecipientsRefused as error:
print(f"The automated birthday email to {to_email} failed to send.")
print(f"Error code: {error.smtp_code}. Error message: {error.smtp_error}")
else:
emails_sent += 1