Created
February 14, 2012 12:22
-
-
Save mgallego/1826437 to your computer and use it in GitHub Desktop.
Picmnt Emailer Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import yaml | |
| import MySQLdb | |
| import smtplib | |
| from email.MIMEText import MIMEText | |
| site_url = 'http://localhost' | |
| def get_picmnt_config(): | |
| parameter_yaml = yaml.load(file('../Picmnt/app/config/parameters.yml','r')) | |
| return parameter_yaml["parameters"] | |
| config = get_picmnt_config() | |
| def get_db_connection(): | |
| return MySQLdb.connect(host=config["database_host"], user=config["database_user"], passwd=config["database_password"], db=config["database_name"]) | |
| def get_email_connection(): | |
| sender = smtplib.SMTP('smtp.gmail.com') | |
| sender.ehlo() | |
| sender.starttls() | |
| sender.login(config["mailer_user"], config["mailer_password"]) | |
| return sender | |
| def send_email(email, message): | |
| sender = get_email_connection() | |
| sender.sendmail(config["mailer_user"], email, message.as_string()) | |
| sender.close() | |
| def get_message(email, img_title, view_url, edit_url): | |
| message = MIMEText("Tiene comentarios pendientes para la imagen "+img_title+"\n\nPuede ver los comentarios accediendo a "+view_url+"\n\n\n\nPara dejar de recibir este tipo de correos desmarque la opcicion de recibir correos en las propiedades de la imagen en "+ edit_url +"\n\n\n\nEl equipo de Picmnt") | |
| message["from"] = config["mailer_user"] | |
| message["to"] = email | |
| message["subject"] = "Picmnt | Nuevos Comentarios" | |
| return message | |
| def get_email(): | |
| db = get_db_connection() | |
| cursor = db.cursor() | |
| sql = 'select distinct usr.email as email, img.title, usr.username, img.slug, img.id_image, img.user_id from Image img, Image_Comment com, User usr WHERE img.id_image = com.id_image AND img.user_id = usr.id AND com.email_notified = 0 and img.notify_email = 1' | |
| cursor.execute(sql) | |
| results = cursor.fetchall() | |
| db.close() | |
| return results | |
| def get_view_url(user, slug): | |
| return site_url +'/view/' + user + '/' + slug | |
| def get_edit_url(id_image): | |
| return site_url + '/img/edit/' + str(id_image) | |
| def mark_as_notified(id_image): | |
| db = get_db_connection() | |
| cursor = db.cursor() | |
| sql = 'UPDATE Image_Comment set email_notified = 1 WHERE id_image = ' + str(id_image) + ';' | |
| try: | |
| cursor.execute(sql) | |
| db.commit() | |
| except: | |
| db.rollback() | |
| db.close() | |
| def main(): | |
| for data in get_email(): | |
| email = data[0] | |
| img_title = data[1] | |
| user = data[2] | |
| slug = data[3] | |
| id_image = data[4] | |
| try: | |
| send_email(email, get_message(email, img_title, get_view_url(user, slug), get_edit_url(id_image))) | |
| except: | |
| print 'Error al enviar los correos' | |
| mark_as_notified(id_image) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment