Send mail with Sendmail from Bash command line

eMails are still the most important communication channel on internet. Most of the web applications you can find on the web require an eMail address to register because they will use it for important communications, to recover you password and so on. It’s still a reliable way to identify the users.

Well, today I was to write a monitor script who checks a server status and send an eMail to the admin if something wrong happens. For some reason I wanted to write it in Bash script and put it directly into the /etc/cron.daily/ folder. The check was pretty easy to write but then I had to send the eMail.

After some searching and trying I finally found out how to send emails directly from Bash with Sendmail and no need to write any file:

echo -e "To: "admin" <admin@example.com>nFrom: "The Server" <server@example.com>rnContent_Type: text/plainnSubject: [Server] Unexpected responsennResponse was wrongn" | sendmail -t

Now a little bit of explanation.

Basically I send all the needed headers and content to Sendmail with a pipe. Sendmail’s -t option forces to search the recipients addresses inside the header. Normally Sendmail expects this kind of addresses as a parameter, but this way everything has the same source, I mean the pipe.

The echo statement sends all the needed data to Sendmail. Remember the -e option to replace the backslashed special characters. This is very important because without it Sendmail will do not understand anything about you want to send.

You can add any kind of headers you need, such as the Cc and the Bcc fields or others, and check that after the last header and before the mail’s content there are two new line characters.

Remove “compatibility view” button in Internet Explorer

Old Internet Explorer version are often a big problem for web developers. Fortunately everyday the people using IE 7, IE6 or even IE5.5 became lesser and lesser. So usually we don’t have to spend to much time to fix everything in our CSS to make every page look perfect in all this old browsers because people using them are not so relevant.

Passing from IE7 to IE8 Microsoft  had the “brilliant” idea to help developers with this huge step forward, in particular enterprise sites was in past often strictly related to a browser version and the change could be a big problem for them. The solution was the “compatibility view” button, the button with a broken page icon, which switches your modern browser to an old IE7 mode. Holy crap! :-O

In real life the button is almost useless because Internet sites are already prepared to offer the content to the browser in a best way using different solutions (conditional comments, server side browser detection, javascript, CSS hacks, ignoring…) BUT sometimes users could press the stupid button while trying to press the “Reload Page” button. It’s like a nuke because instantly your browser start to render pages like the old IE7 and everything will look broken or wrong.

The “Compatibility View” is domain scoped and remembers the setting even after the browser was closed. So what can we do?

We can say to Internet Explorer: hey don’t worry, everything is okay! It’s pretty easy, we have just to use the X-UA-Compatible header and set this value: IE=edge. In a HTML page you can use a meta tag:

<meta http-equiv="X-UA-Compatible" content="IE=edge">

Or you can choose to add it to your web server configuration. What does it mean? It says to Explorer to use his last standard mode. So IE8 will render the pageas IE8, IE9 like IE9 and so one and the “Compatibility Button” will disappear. Gotcha!

HSL2RGB converter source code (Perl)

Cleaning up my web space I found an old code to convert HSL triplets (Hue Saturation Lightness) to RGB (Red Blue Green). I think the code comes from the Gimp source code and was ported to Perl, but it’s so simple I think you can port easily everywhere.

So, why do you need to convert HSL to RGB? HSL values are more human understandable, so there a good choice for a color picket or something similar. But most graphic application require RGB values which are mostly used by computers.

 

And here is the code:

hsl_to_rgb($h, $s, $l); # Values in 0-255 range

sub hsl_to_rgb {
 my $h = $_[0]/255;
 my $s = $_[1]/255;
 my $l = $_[2]/255;
 my $r, $g, $b;

 if ($h&lt;1/6) {
 $r = 1; $g = 6*$h; $b = 0;
 $g = $g*$s + 1 - $s; $b = $b*$s + 1 - $s;
 $r = $r*$l; $g = $g*$l; $b = $b*$l;
 } elsif ($h&lt;1/3) {
 $r = 1-6*($h - 1/6); $g = 1; $b = 0;
 $r = $r*$s + 1 - $s; $b = $b*$s + 1 - $s;
 $r = $r*$l; $g = $g*$l; $b = $b*$l;
 } elsif ($h&lt;1/2) {
 $r = 0; $g = 1; $b = 6*($h - 1/3);
 $r = $r*$s + 1 - $s; $b = $b*$s + 1 - $s;
 $r = $r*$l; $g = $g*$l; $b = $b*$l;
 } elsif ($h&lt;2/3) {
 $r = 0; $g = 1-6*($h - 1/2); $b = 1;
 $r = $r*$s + 1 - $s; $g = $g*$s + 1 - $s;
 $r = $r*$l; $g = $g*$l; $b = $b*$l;
 } elsif ($h&lt;5/6) {
 $r = 6*($h - 2/3); $g = 0; $b = 1;
 $r = $r*$s + 1 - $s; $g = $g*$s + 1 - $s;
 $r = $r*$l; $g = $g*$l; $b = $b*$l;
 } else {
 $r = 1; $g = 0; $b = 1-6*($h - 5/6);
 $g = $g*$s + 1 - $s; $b = $b*$s + 1 - $s;
 $r = $r*$l; $g = $g*$l; $b = $b*$l;
 }
 $r = int $r * 255;
 $g = int $g * 255;
 $b = int $b * 255;

 return {r =&gt; $r, g =&gt; $g, b =&gt; $b);
}

Enjoy!

Il Manifesto KDE

“Siamo una comunità di tecnici, designer, scrittori e avvocati che lavorano per garantire la libertà per tutti gli uomini tramite il nostro software.” recita così l’incipit del Manifesto KDE, un modo per mettere in chiaro cosa è KDE anche a chi non lo sa. Non si tratta però di una presentazione delle qualità del software, ma della filosofia che c’è dietro e che ha saputo conquistarsi l’interesse di migliaia di appassionati. Read more

Remove frameBorder on Internet Explorer in quirks mode

I know that the most of the internet pages run in standard mode, but sometimes it happens to work for some generic plugin and so you have to check your code in such a situation and especially with Internet Explorer 6, 7 and 8 there are a lot of bad surprises 🙁

A page runs in standard mode when a doctype is set. It’s common to use the simple HTML5 doctype on the first row of a HTML page: <DOCTYPE html> Very easy! Without such a definition your page runs in non-standard mode, also called Internet Explorer 6 mode, but everyone knows it as “quirks mode”.

What I want to talk about is a Internet Explorer “feature” in quirks mode: the iframe border. Microsoft introduced an attribute for this element called “frameBorder”. Basically it’s a inset border around an iframe that you can switch on (default) or off. Unfortunately it’s not possible to switch through Javascript. You actually change the property’s value, but nothing happens! That’s a big problem!!

But there is a workaround that looks like this:

var iframe = document.getElementById('myiframe');
iframe.frameBorder = '0';
iframe.outerHTML = iframe.outerHTML;

That’s awful, I know, but it works! The iframe is forced to be rendered again and this way IE updates the frame border. Yay! 😀

But pay attention. The iframe must be already inserted into the DOM. If you created an iframe using document.createElement you have first add it somewhere in your page and then apply the workaround, otherwise nothing can happen.

And the last one: I noticed that if you have a handler to your iframe and apply the outerHTML workaround the handler invalidates somehow, so it’s safer to get a new one using, for example, getElementById.

Gettext support for Smarty

Smarty is a great template engine for PHP because it’s very powerful and very easy to use. By separating the PHP logic from the presentation template your code becomes cleaner and easier to maintain, so it’s a good deal.

Anyway Smarty lacks of a native internationalization support, but it’s expandable nature permits to easy come around this problem. Gettext support can be added to Smarty using this plugin. It’s a revisited version of a previous plugin, with some improvement and fixes. Just put it in Smarty’s plugins folder and you are ready to use the added {t}…{/t} tag where you can put text that has to be localized.

With the plugin comes a tool  that extracts all the text string marked by the tag and saves them in a C source file which can be processed with the usual Gettext utility to obtain a classic POT file. You can add localizations and use them to localize the text in your templates!

Arriva KDE 4.9

E così siamo arrivati a KDE 4.9. Il nostro ambiente desktop preferito fa un altro passo in avanti diventando sempre più maturo e completo. Il team di sviluppo si è reso conto che la qualità è un aspetto che fondamentale curare nel modo corretto, motivo per cui recentemente il team di tester è stato riorganizzato in modo da non avere più in gruppo di persone che vaga a casaccio per il software sperando di incappare in un bug, ma si è deciso di fornire delle checklist in modo da focalizzare i test su aree specifiche. Read more

Nokia vuole vendere Qt?

Ci mancava solo questa. Mentre siamo tutti contenti dell’arrivo di Qt5 arriva un rumor circostanziato che afferma che Nokia ha tutta l’intenzione di vendere Qt! L’esperimento di Nokia nell’ambito dei framework è finito come è già finita l’esperienza nell’ambito dei sistemi operativi. Potrebbe essere una disgrazia per il progetto e non è una cosa che in realtà mi stupisce più di tanto. Cosa potrà succedere? Read more