Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Publicaciones sobre programming (publicaciones antiguas, página 32)

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.

Kid, wanna try a WM?

Read­ing a post on plan­etkde, I saw an ap­par­ent­ly out­-of-nowhere ref­er­ence to black­boxqt...

Not all that in­ter­est­ing, I sup­pose, but I am fond of ol'B­B,and this Qt4 re­make could be pret­ty cool (although I can't test it be­cause it does­n't build for me). Here's a link to the code.

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.


Contents © 2000-2020 Roberto Alsina