Python - Sendmail script



#!/usr/bin/python
import os
import smtplib
import sys
from email.Utils import formatdate
import urllib2
import json
import datetime
 
def latest_php_releases():
    release_info = {}
    try:
        response = urllib2.urlopen('http://us3.php.net/releases/active.php')
        json_string = response.read()
        release_info = json.loads(json_string)
    except:
        print "Unable to connect/parse json"
        pass
    return release_info
 
def determine_unknown_versions( php_version_info ):
    output = []
    for version_major in php_version_info:
        #print "Version Major %s " % version_major
        for version_minor in php_version_info[version_major]:
            #print "* Version Minor %s " % version_minor
            version_info = php_version_info[version_major][version_minor]
            #print "* Version: %s" % version_info["version"]
            version = version_info["version"]
            if not is_known_version( version ):
                output.append( version )
            record_known_version( version )
    return output
 
def is_known_version(php_version):
    directory = "%s/.phpversions" % os.path.expanduser("~")
    path = "%s/%s" % (directory,php_version)
    return os.path.isfile(path)
 
def record_known_version(php_version):
    directory = "%s/.phpversions" % os.path.expanduser("~")
    if not os.path.exists(directory):
        os.makedirs(directory)
    path = "%s/%s" % (directory,php_version)
    f = open(path,'w')
    f.write(str(datetime.datetime.now()))
    f.write("\n")
    f.close()
    return
 
def mkdir(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)
 
def main():
    php_version_info = latest_php_releases()
    unknown_versions = determine_unknown_versions( php_version_info )
    result = unknown_versions
    result = filter(len, result)
    if len(result) == 0:
        return 0
    result.insert(0,'--------------')
    result.insert(0,'New PHP Versions Detected on php.net')
    result.insert(0,'--------------')
 
    body = "\n".join(result)
    subject = "[php version checker] [automated]"
    to_emails = [
       'john.doe@example.com',
    ]
    from_email = 'no-reply@example.com'
 
    to_list = ",".join(to_emails)
    rfc822date = formatdate()
    msg = (
        "To: %(to_list)s\r\n"
        "From: %(from_email)s\r\n"
        "Reply-To: %(from_email)s\r\n"
        "Subject: %(subject)s\r\n"
        "Date: %(rfc822date)s\r\n"
        "Content-type: text/plain\r\n"
        "\r\n"
        "%(body)s\r\n")
 
    msg = msg % locals()
    #print(msg)
    #server = smtplib.SMTP_SSL("mailserver.example.com", 465)
    #server.login("user", "mailpass")
    server = smtplib.SMTP("postfix.example.com", 25)
    server.sendmail(from_email, to_emails, msg)
    server.quit()
 
    return 0
 
if __name__ == "__main__":
    import sys
    #os.chdir(sys.path[0])
    sys.exit(main())
code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)