I’m trying to use a script in Jamf to send an email to a user. Specifically want to do this from a policy: policy installs a package, then emails that user some information.
I’m trying to use the Python script from this page as a starting point: https://community.jamf.com/t5/jamf-pro/sending-an-email-via-script/m-p/186819.
For testing purposes, I’m only running the script--I don’t have the package included in the policy so that I can focus on getting the script working.
I haven’t used Python before. I’m guessing this script is just a skeleton and I need to do some additional things to get it to run, but I don’t know what those things are. I’ve filled in the SMTP_SERVER, SMTP_PORT, and SMTP_FROM values and had our Exchange/Outlook person verify they were correct.
When I scope the policy to one of my test devices, the status in the log for the policy just says “Pending” and never changes. If I try running the policy by triggering it from Terminal (sudo jamf policy -trigger [name_of_policy]), Terminal tells me the script is executing and then running, but then it never appears to complete.
Not sure what else I need to do with the script to make it fully functional.
Here’s the script:
#!/usr/bin/python
########################################################################################
#
# ABOUT
#
# Script to send plain-text email:
# * Parameter 4: recipient
# * Parameter 5: subject
# * Parameter 6: message
#
########################################################################################
#
# HISTORY
#
# Version 1.0, 7-Dec-2015, Dan K. Snelson
#
########################################################################################
# Import commands
import commands, os, sys, smtplib, email
from email import encoders
SMTP_SERVER = 'smtp.company.com'
SMTP_PORT = 25
SMTP_FROM = 'noreply@company.com' # Must be a complete email address
SMTP_TO = str(sys.argv[4]) #Parameter 4: recipient
SUBJECT = str(sys.argv[3]) + ", " + str(sys.argv[2]) + ": " + str(sys.argv[5])
# Parameter 5: subject
MESSAGE = str(sys.argv[6]) #Parameter 6: message
msg = email.MIMEMultipart.MIMEMultipart()
body = email.MIMEText.MIMEText(MESSAGE)
msg.attach(body)
msg.add_header('From', SMTP_FROM)
msg.add_header('To', SMTP_TO)
msg.add_header('Subject', SUBJECT)
# Now send the message
mailer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mailer.sendmail(SMTP_FROM, [SMTP_TO], msg.as_string())
mailer.close()