Send automatic email message?

Discussion in 'Mac Programming' started by ArtOfWarfare, Jan 25, 2012.

  1. ArtOfWarfare macrumors G3


    Nov 26, 2007
    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?
  2. danwilliams, Jan 25, 2012
    Last edited: Jan 25, 2012

    danwilliams macrumors member

    Sep 15, 2008
    Python has the smtplib that can automate the process.

    import smtplib
    emailServer = smtplib.SMTP('')
    emailServer.sendmail("", [''], "Hello world")
    Your app could call your python script that emails your battery low warning.
  3. ArtOfWarfare thread starter macrumors G3


    Nov 26, 2007
    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?)
  4. phantax macrumors member

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

    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.

    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?

    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.
  5. jiminaus macrumors 65816


    Dec 16, 2010
    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.
  6. KnightWRX macrumors Pentium


    Jan 28, 2009
    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
  7. Jeremy1026 macrumors 68020


    Nov 3, 2007
    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.
  8. balamw Moderator


    Staff Member

    Aug 16, 2005
    New England
    Wouldn't this be a good place to try and use Growl?

  9. GorillaPaws macrumors 6502a


    Oct 26, 2003
    Richmond, VA
    Your interface should also include a "send a test message" button so the user can verify the system is working.

Share This Page