Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,560
6,059
Hi, I just released my first Mac OS X app which keeps track of the batteries in your wireless devices and offers an estimate of when they'll die. A user claims to be from the IT department of a school and he'd like to install the app on all the school's computers. He'd like it if the app would automatically send him an email when it estimates the batteries will die within a week.

I know that I can open a URL to start mail and have a message and address already filled out, but how can I go a bit further and have it compose the message, address it, and send it automatically?
 

danwilliams

macrumors member
Sep 15, 2008
46
0
Python has the smtplib that can automate the process.

Code:
import smtplib
emailServer = smtplib.SMTP('my.mail.server.address.com')
emailServer.sendmail("me@foo.com", ['you@foo.com'], "Hello world")
emailServer.quit()

Your app could call your python script that emails your battery low warning.
 
Last edited:

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,560
6,059
I don't know anything about Python... how do I use that code?

I guess to be more specific my questions are:

1 - How do I add that code to my app? All of my files right now are written in C and/or Obj-C, how would I go about adding Python code to some of my files?

2 - What would be the best way to minimize how much the user has to set up? Ideally, the user should just get several conditions under which they want to receive emails, and to set where the email should be sent. My understanding is that I'll also need to have a server or something from which to send the mail and an address for it to be sent from. That kind of crap is too complicated to set up... I don't want to deal with it, and I don't want my users to have to deal with it. Is there some way I can either
a - Find an account already set up to send mail on the computer and send mail from that or
b - Set up a free Yahoo! or Google mail account and just have my program send mail from that account (the greatest idea from a security standpoint, maybe not, but it would work and be easy to set up, correct?)
 

phantax

macrumors member
Feb 2, 2009
72
0
1 - How do I add that code to my app? All of my files right now are written in C and/or Obj-C, how would I go about adding Python code to some of my files?

Using that python code is still going to require the user to input their mail server, username, password, etc.

2 - What would be the best way to minimize how much the user has to set up? Ideally, the user should just get several conditions under which they want to receive emails, and to set where the email should be sent. My understanding is that I'll also need to have a server or something from which to send the mail and an address for it to be sent from. That kind of crap is too complicated to set up... I don't want to deal with it, and I don't want my users to have to deal with it.

If I were doing this, I would have a server or shared hosting account with a WebService setup. The app could send an email request to the WebService, and the mail would come from the server.

a - Find an account already set up to send mail on the computer and send mail from that or

You can certainly send an email through the users default mail app, but I don't think you will be able to pull the credentials out. Also, what happens if those workstations don't have email accounts setup?

b - Set up a free Yahoo! or Google mail account and just have my program send mail from that account (the greatest idea from a security standpoint, maybe not, but it would work and be easy to set up, correct?)

You could certainly do that, but you will likely expose your email account login credentials inadvertently. Try looking into the "Yahoo Mail Web Service" for a better solution.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
If you, for example, send all emails to the same SMTP server (say the same gmail server) you would need the gmail username and password stored in the app. This has huge security implications.

I would think for IT guys who want email notifications, allow them to set an SMPT server, a From address, a To address, and possibly a subject. That would mean you could code your email to be sent to whatever SMTP server the IT guy wants, and having the ability to set the To, From and Subject headers would allow the the IT guy to craft whatever rules work for the IT guy's workflow.

In regards to actually sending an email, AFAIK, the Cocoa Objective-C APIs can't help you with this. If you want to you use a Python script as previously posted, you could include the Python script in your app's bundle and then use the popen function (see man 3 popen) to invoke the script, perhaps passing the actual email via standard in into the python script.

SORRY: I didn't see phantax's reply, some 10 hours before mine. My excuse, it's Australia Day, and I'm significantly altered appropriately.
 

KnightWRX

macrumors Pentium
Jan 28, 2009
15,046
4
Quebec, Canada
Mailcore is an Objective-C framework for iOS or OS X to send e-mail amongst other thing. This could better be integrated to your application than some Python script (or Perl script, or bash script using the system's mailx application, or whatever other external method you find).

I will also say you should simply provide the user with e-mail configuration options :

- Username/password for authenticated SMTP
- SMTP server
- Display name
- From e-mail
- multiple To recipients
 

Jeremy1026

macrumors 68020
Nov 3, 2007
2,215
1,029
Set up a PHP script that gets the email to send to via POST. Send the POST method using your application containing the address to send to. Then use PHPs mail() function.
 

GorillaPaws

macrumors 6502a
Oct 26, 2003
932
8
Richmond, VA
I will also say you should simply provide the user with e-mail configuration options :

- Username/password for authenticated SMTP
- SMTP server
- Display name
- From e-mail
- multiple To recipients

Your interface should also include a "send a test message" button so the user can verify the system is working.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.