PDA

View Full Version : Send automatic email message?




ArtOfWarfare
Jan 25, 2012, 11:05 AM
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
Jan 25, 2012, 11:42 AM
Python has the smtplib (http://docs.python.org/library/email-examples.html) that can automate the process.


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.

ArtOfWarfare
Jan 25, 2012, 05:00 PM
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
Jan 25, 2012, 05:18 PM
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
Jan 26, 2012, 03:09 AM
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
Jan 26, 2012, 05:39 AM
Mailcore (https://github.com/mronge/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
Jan 26, 2012, 07:30 AM
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.

balamw
Jan 26, 2012, 05:41 PM
Wouldn't this be a good place to try and use Growl (http://growl.info/)?

B

GorillaPaws
Feb 5, 2012, 07:43 AM
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.