Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about python (old posts, page 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 ;-)
tf=tempfile.mkstemp()
t=open(tf,'w')
t.write(report)
t.close()
tpart=MIMEText(os.popen('links -dump %s'%tf,'r').read(), _subtype='plain', _charset='utf-8')
os.unlink(tf)

# Create the message with both parts attached
msg=MIMEMultipart('alternative')
msg.attach(hpart)
msg.attach(tpart)

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

#If you need to use SMTP authentication, change accordingly
smtp=smtplib.SMTP('mail.yourcompany.com'')
smtp.sendmail('support@yourcompany.com','you@yourcompany.com',msg.as_string())

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 messages using m.sendmsg()
       This is the only place your code lives
       """
    
       # wait a bit for everything to settle down (sync taking efect
       # basically)
       time.sleep(15)
    
       msg=sys.stdin.read()
       for d in sys.argv[3].split('+'):
               print m.sendmsg(d,msg)
    
    
       # give time to send the messages
       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):

    msn:admin1@hotmai1.com+admin2@hotmai1.com

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 as a windows crutch: Sending SMS

Sup­pose you want to send SMS mes­sages from win­dows through a blue­tooth con­nec­tion to a phone.

I am sure you can make it work. On the oth­er hand, I al­ready had it work­ing on Lin­ux... so you can just use this on a friend­ly Lin­ux box, and send SMS mes­sages by ac­cess­ing a spe­cial URL:

#!/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')
      entrada.write(mensaje)
      entrada.flush()
      entrada.close()
      msg=salida.read()
      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)

If someone opens http://my­box.­do­main.in­ter­nal:8080/?nu­mero=1234?­men­saje=ho­la%20­mun­do it sends "hola mundo" to the 1234 number.

I sup­pose I could call this a web tele­pho­ny ser­vice or some­such, but it's ac­tu­al­ly just the 5'­so­lu­tion that came to mind.

It us­es a sil­ly lit­tle not-a-we­b-frame­work called col­u­brid in­stead of some­thing you may know, be­cause I want­ed to keep it sim­ple, and it does­n't get much sim­pler than this.


Contents © 2000-2020 Roberto Alsina