Archive

Archive for April, 2012

How-to: Lower html email spam score using swift mailer

April 19, 2012 Leave a comment

Swift mailer is yet another great PHP emailing library. Working as a web developer, I am faced quite often with the tasks of sending out html email. One particular area I always pay attention to is try to lower the emails’ spam score so they won’t end up in recipient’s junk or spam box. Here’s the ‘magic’ code that does the trick:

<?php
        require_once 'lib/swift_required.php'; // or absolute path if swift mailer library is not under current directory
        define('mailhost', 'localhost'); // or external smtp server
        $to='some_recipient@email.address';
        $subject='HTML msg sending test';
        $body='This is a test.';
        $body_html='<p>This is a <strong>test</strong>.</p>';
        $transport=Swift_SmtpTransport::newInstance(mailhost, 25);
        $mailer=Swift_Mailer::newInstance($transport);
        $message=Swift_Message::newInstance($subject)
            ->setFrom( array('noreply@yourcompany.com'=>'Your Company Name') )
            ->setTo( array($to) )
            ->setBody($body)
            ->addPart($body_html, 'text/html');
        $result = $mailer->send($message);

In real-life examples, I’ve seen score dropping by as much as 2.1 by sending html message along with its plain-text counterpart. The reasoning behind this is well explained in this stackoverflow question:
http://stackoverflow.com/questions/5787185/is-it-sensible-to-send-html-only-email-these-days.

Advertisements
Categories: php, spam score, swift_mailer

Use external smtp server to send nagios alerts

April 18, 2012 7 comments

By default Nagios uses localhost to send out email, this would be a problem if the nagios server’s IP is not designated to send emails. A quick solution is to use utilities that support sending emails through external smtp server, here’s a nice one:

http://caspian.dotconf.net/menu/Software/SendEmail/

Steps (Assuming Nagios version is 3):
1) Download the zip file from http://caspian.dotconf.net/menu/Software/SendEmail/#download, unzip and copy sendEmail.pl to /usr/local/bin

2) Modify commands.cfg so the notify-host-by-email and notify-service-by-email are similar to the followings:
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/sendEmail -f nagios_notification@your_domain.com -s smtp.server.ip_or_hostname -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -t $CONTACTEMAIL$
}

define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/local/bin/sendEmail -f nagios_notification@your_domain.com -s smtp.server.ip_or_hostname -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -t $CONTACTEMAIL$
}

change nagios_notification@your_domain.com and smtp.server.ip_or_hostname to suit your need of course. Consult http://caspian.dotconf.net/menu/Software/SendEmail/#download for more command line options if the smtp server requires authentication, for example.

3) Make sure the new configuration is syntax-error free:
/etc/init.d/nagios checkconfig

4) Restart nagios
/etc/init.d/nagios restart

Notes:
If for some reason nagios doesn’t send out email, please do the followings:
1) enable debugging in /usr/local/bin/sendEmail.pl by setting “logFile” to, say, “/var/log/sendEmail”, change its mode to 666
2) check if nagios contacts contain valid email address. I found out sendEmail doesn’t work with records like
systemadmin: my_real@email.address in /etc/aliases.

Categories: nagios, sendEmail

backbone.js learning note [1]

April 5, 2012 Leave a comment

Keep track of state change example:

    Stock = Backbone.Model.extend({
        defaults: {
            price: null,
        },
        initialize: function(){
            this.bind('change:price', function() {
                var px=this.get('price');
                console.log('price changed to: '+px);
            });
        },
        price_change: function(newprice) {
            this.set({price:newprice});
        }
    });

    var stock=new Stock({price:1234.55});
    stock.price_change(456);
    // the console output from the last line:
    // price changed to: 456

References:
http://backbonetutorials.com/what-is-a-model/

Categories: backbone.js, javascript