Skip to main content

Ralsina.Me — Roberto Alsina's website

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.

Discount: FAIL!

Sup­pose a Thing (T­M) costs $100. Now,y­ou are of­fered a 20% dis­coun­t. You ex­pect to pay $80 for it, right?

That means you ex­pect to pay, for X dis­coun­t, 100-X % of the cost.

That's why this pic­ture is full of FAIL.

discount

BTW: Re­al pic­ture from a shop in Mar del Plata, Ar­genti­na, a few days ago.

Living with eee: tips and tricks

I have used my eee PC for a few months al­ready, and here are a few tips I gath­ered along the way.

  • Re­­move Xan­­dros, get *Ubun­­tu on it

    Yes, it boots a bit slow­er, but ev­ery­thing still works (I ad­mit, with some man­u­al tweak­s), and you will use less disk space, and be gen­er­al­­ly hap­pi­er.

  • If you use KDE, check the ad­­vanced win­­dow menu.

    Did you know that any app can be fullscreen? Yes it can. How­ev­er, I pre­fer the "No Bor­der" op­­tion. Yes, your win­­dows will have no bor­der­s,bu­u­u­u­ut:

    • You are prob­a­bly not go­ing to use win­­­dows side by side on a 800x480 screen, are you?

    • You can still move and re­­­size win­­­dows: Al­t+LMB and Al­t+RMB are your friend­s.

  • Yes you can play games on the eee: get psx, the psx em­u­la­­tor. I had a bunch of CDs for my long-dead PS1. Need for Speed looks pret­­ty good, and Tekken 3 is a lot of fun :-D

  • There's enough stream­ing mu­sic to avoid car­ry­ing your MP3 around. Last.FM and all the shout­­cast ra­­dios on Amarok do the trick for me.

  • Get SD card­s.

  • The black mod­­el seems to be pret­­ty scratch re­­sis­­tan­t.

  • Yes, you can throw it on the so­­fa when you ar­rive at home. No, noth­ing will break (at least noth­ing broke on mine). SS­Ds are nice.

  • A 1yo boy with clean hands can slap at the key­board for a minute or two and it may live.


Contents © 2000-2020 Roberto Alsina