1

I'm new to Applescript and working on an app that runs in the background, and I want it to send email updates every so often WITHOUT mail.app.

I've tried googling this numerous times, and while this is the closest I've found, I don't know how to use a Python script. This app needs to work no matter what Mac OSX it is installed in, including Computers without Python. Is that possible?

Community
  • 1
  • 1
Vira
  • 509
  • 5
  • 8

3 Answers3

2

I have often wanted to do the same thing and struggled myself. I ultimately landed on a python option. Python is supported back to OS X 10.5 and comes preinstalled on many of the newer versions of OS X. Is there some reason you don't feel you can use it? Are you supporting OS 10.4.x or older machines?

If they python option is still a possible option, I've included an example of how to use it here.

property PyMail : "/Users/TBD/Documents/Sample_Python_Email/PyMail.py"                      -- PATH TO PYTHON MAIL SCRIPT

on run
    set emailTo to "youraddress@somedomain.com" -- MULTIPLE ADDRESS SHOULD BE A COMMA     DELIMITED STRING LIKE THIS -- "address1@gmail.com, address2@hotmail.com"
    set emailFrom to "Your Name <your.name@somedomain.com>"
    set subject to "Demo Email"
    set message to "Hi user,
            I hope things are going well"
    set pathToAttchment to "/Users/TBD/Desktop/prof.jpg" -- POSIX PATH TO FILE, LEAVE AS EMPTY STRING FOR NO ATTACHMENT
    set username to "smtpusername" -- MAY OR MAY NOT BE REQUIRED IN YOUR CASE

    sendPyMail(emailTo, emailFrom, subject, message, pathToAttchment, username)

end run



on sendPyMail(emailTo, emailFrom, subject, message, attachment, username)
    try
        do shell script "python " & quoted form of PyMail & " " & quoted form of emailTo & " " & quoted form of emailFrom & " " & quoted form of subject & " " & quoted form of message & " " & quoted form of attachment & " " & quoted form of username
        return true
    on error
        return false
    end try
end sendPyMail

Here is the python script (just copy and past it into a text editor and save as PyMail.py. You'll need to change the smtp server and possibly add the password that goes with the username you're supplying...

import sys

SMTP_TO = sys.argv[1]
SMTP_TO = SMTP_TO.split(',')
SMTP_FROM = sys.argv[2]
SUBJECT = sys.argv[3]

MESSAGE = sys.argv[4]

TEXT_FILENAME = sys.argv[5]

SMTP_USERNAME = sys.argv[6]



SMTP_SERVER = 'smtp.domainx.com'
SMTP_PORT = 25

SMTP_PASSWORD = ''


# now construct the message
import smtplib, email
from email import encoders
import os

msg = email.MIMEMultipart.MIMEMultipart()
body = email.MIMEText.MIMEText(MESSAGE)

if TEXT_FILENAME != "":
attachment = email.MIMEBase.MIMEBase('text', 'plain')
attachment.set_payload(open(TEXT_FILENAME).read())
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(TEXT_FILENAME))
encoders.encode_base64(attachment)

msg.attach(body)

if TEXT_FILENAME != "":
    msg.attach(attachment)

msg.add_header('From', SMTP_FROM)
msg.add_header('To', ';'.join(SMTP_TO))
msg.add_header('Subject', SUBJECT)

mailer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mailer.sendmail(SMTP_FROM, SMTP_TO, msg.as_string())
mailer.close()
ThrowBackDewd
  • 1,737
  • 2
  • 13
  • 16
  • Alright, so I tried this, but I get "false" when I run the script. Is there a way that I can see where the error occurs? I've changed the smtp server (to smtp.gmail.com), given the password for the email address, and changed the appropriate parts of the first half of the script. What am I missing? And thank you so much for the help! @ThrowBackDewd – Vira Aug 14 '14 at 18:25
1

Run this in Terminal

echo "this is the body" | mail -s "this is the subject" "to@address"

Note: This assumes you have a locally installed MTA

Your AppleScript would be:

do shell script "echo 'this is the body' | mail -s 'this is the subject' 'to@address'"
double_j
  • 1,636
  • 1
  • 18
  • 27
0

Knowing nothing about python, I used TextWrangler and cut and paste the two scripts above. The second one crashes at line 1, returning a syntax error, as follows:

line 1 property PyMail : "/Users/.../PyMail.py" -- PATH TO PYTHON MAIL SCRIPT ^ SyntaxError: invalid syntax

The carrot pointer is under the 'l' in 'PyMail'. I commented out line 1, and it returned the same message for line 3.