Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Thanks Philip Pearson!

This blog was host­ed for a long time in http://lat­er­al.pyc­s.net ... un­til the site kin­da died. And there are hun­dreds of links around, all point­ing to URLs in pyc­s.net for the old­er sto­ries. Now they all work again!

So, thanks Philip!

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 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)         msg=sys.stdin.read()       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):

    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.

Descuento: MAL!

Eso es por­que es­pe­rás pa­ga­r, pa­ra un des­cuen­to X, 100-­X% del cos­to.

Por eso es­ta fo­to es­tá MA­L!.

discount

Fo­to real de una tien­da de ro­pa en Mar del Pla­ta, Ar­gen­ti­na, ha­ce unos día­s.


Contents © 2000-2024 Roberto Alsina