Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Publicaciones sobre python (publicaciones antiguas, página 31)

Creating and sending nice HTML+Text mails from python

I de­cid­ed I need­ed an au­to­mat­ic re­port of some things on my email ev­ery day, and I want­ed it to look nice both in plain text and HTM­L. Here's what I came up with.

Let's as­sume you cre­at­ed the HTML ver­sion us­ing what­ev­er mech­a­nism you wish, and have it in a vari­able called "re­port".

Here's the im­ports we will use:

import smtplib,email,os,tempfile
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.Charset import Charset

And here's the code:

# Create a HTML mail part
hpart=MIMEText(reporte, _subtype='html', _charset='utf-8')

# Create a plain text mail part
# Ugly and requires links, but makes for a great-looking plain text version ;-)
tpart=MIMEText(os.popen('links -dump %s'%tf,'r').read(), _subtype='plain', _charset='utf-8')

# Create the message with both parts attached

# Standard headers (add all you need, for example, date)
msg['Subject'] = 'Report'
msg['From']    = ''
msg['To']      = ''

#If you need to use SMTP authentication, change accordingly

Adding MSN notifications to Argus

I am a us­er of Ar­gus as a mon­i­tor­ing soft­ware. Since it's very flex­i­ble and easy to ex­tend, I want­ed to add MSN alert­s, the same way I had added SMS alerts a while ago. It was eas­i­er than I thought!

  1. In­­stall msnlib

  2. In­­stall the ex­am­­ple msnbot, mod­­i­­fied so do_­­work is like this:

    def   do_­work():       """      Here you do your stuff and send mes­sages us­ing m.sendms­g()      This is the on­ly place your code lives      """         # wait a bit for ev­ery­thing to set­tle down (sync tak­ing efect       # ba­si­cal­ly)       time.sleep(15)       for   d   in   sys.argv[3].split('+'):               print   m.sendmsg(d,msg)           # give time to send the mes­sages       time.sleep(30)         # and then quit       quit()  
  3. De­fine a cus­tom no­ti­fi­ca­tion in ar­gus like this:

    Method "msn" {
       command: /usr/local/bin/msnbot alerts@mycompany mypass %R >/tmp/XXX 2>&1
       send: %M\n
  4. Wher­ev­er you want MSN no­ti­fi­ca­tion­s, add this (on no­ti­fy or es­ca­late di­rec­tives, us­ing as many guys MSN ad­dress­es as you need):

That's it.

My first impressions of Google App Engine

Since I got my in­vi­ta­tion and am tired of Haloscan not be­ing reach­able from home (not their fault, prob­a­bly), I de­cid­ed that my first project would be a com­ment host­ing ap­p.

In oth­er word­s, some­thing a bit HaloScan-­like.

Since I have very lim­it­ed re­sources, it will prob­a­bly not be use­ful for many peo­ple, but I am learn­ing about App En­gine, and at the same time prob­a­bly mak­ing my blog a wee bit more com­fort­able.

Some ran­dom thought­s:

  • Can I put Google ads in app en­gine ap­p­s?

  • Does any­one else need this kind of ap­p? I in­­­tend to make it open, so any­one can reg­is­ter its blog in it and use it. 500MB (the max DB size) are a lot of com­­ments. Like a mil­lion of them.

  • I in­­­tend to use Ya­hoo's YUI RTE for ed­it­ing. So my app will be host­ed in Ya­hoo and Google. Cool :-D

  • It's ba­si­­cal­­ly just Djan­­go. Sure, no UNIQUE, no CRUD (ok, there is Google's, which is kin­­da lame... hire one of the Djan­­go guys and mke him work on it ;-), but it's the same thing, give or take a few bytes, spe­­cial­­ly us­ing djan­­go­­for­m­s.

  • we­bapp is... ok, it's rather ug­­ly. Rout­ing the re­quests is an­noy­ing, you can't do things like pass­ing parts of the URL as pa­ram­e­ter­s...

  • The User/­­Data­S­­tore APIs are ok, they feel a bit lim­it­ed but they have a lot of scope in oth­­er ways (as in, there are a few mil­lion reg­is­tered users and many TB of da­­ta stored ;-)

All things con­sid­ered, a nice thing to use, spe­cial­ly at the cost.

Linux ayuda a Windows: Mandar SMS

Es­toy se­gu­ro que tie­ne que ha­ber al­gu­na ma­ne­ra de ha­cer­lo an­da­r. Por otro la­do, ya lo te­nía an­dan­do en Li­nu­x... así que uno pue­de sim­ple­men­te usar es­to en un Li­nux ami­go, y man­dar men­sa­jes SMS ac­ce­dien­do a una URL es­pe­cia­l:

#!/usr/bin/env python
from colubrid import BaseApplication, HttpResponse, execute
import os

class SMSApplication(BaseApplication):

  def process_request(self):
      numero = self.request.args.get('numero')
      mensaje = self.request.args.get('mensaje')
      [entrada,salida]=os.popen4('/usr/bin/gnokii --sendsms %s'%numero,mode='rw')
      response = HttpResponse(msg)
      response['Content-Type'] = 'text/plain'
      return response

if __name__ == '__main__':
  execute(SMSApplication,debug=True, hostname='mybox.domain.internal', port=8080,reload=True)

Si alguien abre http://­m­y­bo­x.­do­mai­­ter­na­l:8080/?­nu­me­ro­=1234?­men­sa­je=ho­la%20­mun­do manda "hola mundo" al numero 1234.

Su­pon­go que po­dría de­cir que es un ser­vi­cio web de te­le­fo­nía, pe­ro es la so­lu­ción de 5 mi­nu­tos que se me ocu­rrió.

Usa una co­sa que se lla­ma co­lu­brid que no es real­men­te un fra­mewo­rk web y no al­go co­no­ci­do por­que que­ría man­te­ner­lo sim­ple, y no se po­ne mu­cho más sim­ple que es­to.

Contents © 2000-2024 Roberto Alsina