Sending email via script - can't get it to work

TheCrusher4Real
New Contributor III

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()

 

 

 

4 REPLIES 4

jamf-42
Valued Contributor II

the script is almost a decade old.. and Im sure Dan will be along to comment. but this is all filed under 'a very bad idea' 

want to tell a user something has happened.. look at swiftdialog, otherwise, send comms to the people this is being installed on.. automating thing via JAMF is not the solution 

AJPinto
Honored Contributor III

What are you hoping to accomplish with this? There is a lot of data the script would need for exchange to actually do the thing, and none of that information I would want to put in a script that would be deployed by Jamf. The days of insecure email servers are long gone.

 

As far as the script itself, its outdated and needs to be rebuilt from the ground up. The shebang would need /usr/bin/python3 to have any hopes of running on any macOS released in the last 2 years as Python 2 has long been retired by Apple.

TheCrusher4Real
New Contributor III

Thanks for the info, everybody. I'll look to do something different. What I'm trying to accomplish is to get some licensing info for an application to the end users who install it. Only used by maybe a couple dozen folks in our organization, and the license info is in plain text when you look at it in the application, so maybe I can do just something as simple as using Jamf Helper to throw up a notification w/ the license info after the install runs.

AJPinto
Honored Contributor III

If the license is the same for all users, you may be able to use apple script with expect to enter the text into the popup when it comes on the screen.