Archive

Archive for the ‘Perl/mod_perl’ Category

Send email from Perl routed through Google Apps SMTP

July 3rd, 2009 No comments

I use Google Apps to manage the few email addresses I have for the listcentral.me domain. Google Apps is amazing! Very easy to use, and super powerful. For today’s small business, there is no better service for managing email, doc shares, and online calendars, specially considering the price!

Unfortunately, I have encountered a problem with using Google Apps. Fortunately there is also a solution to my problem!

The Problem

I have a few, on demand, features on List Central that sends emails to the users, for instance, there is an “Email this list” feature. This requires that I send emails with the from address as lists@listcentral.me from my webserver to the varied email addresses of my future users. Because Google knows all about my domain listcentral.me, if any email I send from my webserver arrives at a google managed email address, google knows that the email didn’t come from it’s system, and it assumes that it must be dealing with a spammer, or other such scam.  This is not good for business, as I cannot have the emails sent from the List Central system being marked as spam!

The Solution

It is possible to route the emails sent from the List Central system through the Google Apps SMTP (Simple Mail Transfer Protocol) system, which has the same effect as actually sending the email through the Google Mail interface. A copy is even saved in the Sent folder!

Using the instructions detailed by Robert Maldon, I was able to get my emails sending through Google’s SMTP server, and passing though the spam checks without too much hassle!

There was one sticking point! For those on Debian, one of the perl packages that you much install, Net::SMTP::SSL, is available in a Debian package only for lenny. I opted to upgrade from etch for this!

The Implementation

I added a little bit to Maldon’s solution to include the multipart/alternative option in order to send both plain text and html versions of the email and allow the email viewer to choose which to display. The code is as follows:

   my $fromEmail = 'lists@listcentral.me';
   my $emailPassword = 'emailpassword';
   my $toEmail = 'someone@somewhere.com';

   my $text = 'Some email message without fancy formatting!';
   my $html = '<b>Some email message with fancy html formatting!!</b>';

   my $smtp;
   if (not $smtp = Net::SMTP::SSL->new('smtp.gmail.com',
                                        Port => 465,
                                        Debug => 1)) { 
      die "Could not connect to mail servern";
   }
   
   $smtp->auth($fromEmail, $emailPassword) || die "Authentication failed!n";
   
   $smtp->mail($fromEmail . "n");
   my @recepients = split(/,/, $toEmail);
   foreach my $recp (@recepients) {
       $smtp->to($recp . "n");
   }

   $smtp->data();
   $smtp->datasend("From: " . $fromEmail . "n");
   $smtp->datasend("To: " . $toEmail . "n");
   $smtp->datasend("Subject: " . $subject . "n");
   $smtp->datasend("MIME-Version: 1.0n");
   $smtp->datasend("Content-Type: multipart/alternative; boundary="$boundary"n");
   $smtp->datasend("n--$boundaryn");
   $smtp->datasend("Content-Type: text/plain; charset=iso-8859-1n");
   $smtp->datasend("Content-Transfer-Encoding: quoted-printablen");
   $smtp->datasend($text . "nn");
   $smtp->datasend("n--$boundaryn");
   $smtp->datasend("Content-Type: text/html; charset=iso-8859-1n");
   $smtp->datasend("Content-Transfer-Encoding: quoted-printablen");
   $smtp->datasend($html . "n");
   $smtp->dataend();
   $smtp->quit;   

In my figuring this out, I nearly fell into a couple of traps. Here’s the lessons learned:

  • Google Email only allows SSL Authentication. There is no way around it.
  • This means a Debian upgrade to Lenny if you want to use the pre-built Debian package libnet-smtp-ssl-perl
  • You do not have to upgrade to Google Apps Enterprise to make this happen. There is some Google documentation about Email Routing that can lead you to think you might need to upgrade for this feature, but this is a completely different feature!

A Brief History of Perl

June 5th, 2009 2 comments

Perl is older than dirt. Well, actually, that’s not true at all. Perl is older than Linux though. Perl 1.0 was released in 1987, 4 years prior to the release of the first Linux distribution. The creator of Perl, Larry Wall, got a graduate degree in Linguistics from UC Berkeley, and had grand plans to find an unwritten language with his wife Gloria, and invent a writing scheme for it. These plans fell through, to the great advantage of computer science field!

Back in the day, Larry Wall programmed on Unix with the usual suspects, C, sed, awk, shell. He grew frustrated with the options available to him. So he set out to write his own language that took the best from what is avaialble, and left the rest behind. The creation of the first Perl interpreter took the better part of 1987. Wall released the first version on December 17th 1987. Through out the late 80′s and early 90′s Wall released improved versions of Perl untile 1994, when he released Perl 5, that included a complete overhaul on the Perl interpreter. Perl 5 is the version that most current Perl programemrs are accustomed to. It has had several sub-versions released through out the 90′s and the current decade. The current version is 5.10.0.

The Perl community has been eagerly waiting for the next major release of Perl for what feels like forever, but it’s actually only been 9 years. Work on Perl 6 began in 2000. We are still waiting on a proper release of Perl 6. Larry Wall and his team aren’t rushing it. They want to build a worthy successor to Perl 5, and they are taking their time in order to make sure they are doing it right.  Perl 6 has many differences from Perl 5, here is a select few:

  • Optional data typing system – You won’t have specify your types, just as with Perl 5, but in Perl 6, you will have the option to specify your data types
  • Formal subroutine parameter list – The days of ‘shift’ and @_ will end in Perl 6, as C/Java style subroutines will be the way in Perl 6
  • Consistency with arrays and hashes – Where in Perl 5 we declare an array as @myarray, and reference an element in an array as $myarray[0], Perl 6 will maintain the @ symbol for both the declaration and the reference. To access and array element in Perl 6 will be done by: @myarray[0]. The same goes for hashes. $myhash{‘item1′} will become %myhash{‘item1′}
  • Formal object-oriented programming – For those of you stickler who say Perl 5 isn’t object-oriented enough, Perl 6 introduces more formalized object orientation with the use of the world ‘Class’ and everything!

Perl 6 will not be back compatible with Perl 5, though there is supposed to be some sort of compatibility mode included.

Perl stands for Practical Extraction and Report Language, though this  acronym was created as an after thought. Larry Wall wanted a short word for the name of the programming language, one that had positive connotations. He settled on Pearl, but laster found out that there was a real-time focused programming language called PEARL, so he changed the spelling, and it has been called Perl ever since.

Companies that Use Perl

May 22nd, 2009 4 comments

perl

The Planet Perl Iron Man Blogging Competition has come about because many people in the Perl community are getting concerned about the state of Perl. It is feared that the popularity of Perl is waning, and that it will become increasingly difficult to find gainful employment as a Perl programmer. I certainly hope that this isn’t going to be the case. Perl is an excellent language that can get the job done! Not to mention that my marketability will be severely hampered is Perl were to become obsolete. The general sentiment within the Perl community is that it is our responsibility to promote Perl, and increase its longevity together. I’m all for this, and am happy to do my part by blogging :)

As it stands right now, Perl is far from dead! There is a very active Perl community out there, and there always has been. I expect though, that it’s always been a community that isn’t to keen on touting their own horns. Let alone get in to promotion or marketing… yikes!

To prove that Perl is long from dead, I thought I’d list some of the big companies out there that are using Perl* in some capacity or another:

Do you know any other companies that are using Perl? Help the community out, and add them to the Perl 5 Wiki!

* Sourced from the Perl Jobs list, and the Perl 5 Wiki.

Categories: Perl/mod_perl Tags:

How Perl Became My #1

May 15th, 2009 No comments

As a part of the Planet Perl Iron Man Blogging Competition, Graeme Lawton asks the question: How did you get hooked? What better time to tell the story of my love affair with Perl, than now!

To be totally truthful, I didn’t pick Perl, Perl picked me! In university, I took one class where we learned a little Perl, but the rest of the time it was heavy Java, some C/C++, and some Lisp of all things! It wasn’t until I got out into the working word that Perl got its hold on me.

I struggled to get that first job out of university. My resume was rather un-standard, with far more soft skills gained from my experience than technical skills, and a Philosophy degree to throw everyone for a loop. I was so grateful to get the job that I finally got: Web Programmer at a leads generation and sales company. The main language used there was perl, under mod_perl, so I had to get crackin’ and learn on the job. A year an a half later I’d left that company, but I couldn’t part with my Perl! I found another Perl programming job, and then another. I also started up this little thing called List Central using Perl under mod_perl.

Reasons I love Perl:

  • !,@,$,%,&
    • Unlike the general sentiment of the ruby/python people, I love the funny symbols used in Perl. Once you get to know what they signify all quickly convey their meaning, and it’s easy to speed up the rate of development, because you know what you are dealing with.
  • Flexibility
    • The flexibility of Perl is huge for me! I’m not big on the Perl one-liners that Perl is famous for. I like that I don’t have to be all cutesie and smartie pants showing off how few lines I can complete a task in. I like to be verbose and include all of my braces, and with Perl I can! And those that like the one-liners, they can enjoy their way too!
  • CPAN
    • CPAN is super useful. One of the most comprehensive and well documented libraries available in any language. And it’s all open source. A beautiful example of the power of open source!
  • Objects or Scripts

    • Again, this is part of the whole flexibility thing. Perl is capable of quick and dirty scripts as well as massive projects designed with proper object orientation. With Perl, the developer gets to decide!

How did you get into Perl? Why do you love it?

Categories: Perl/mod_perl Tags:

Changing Pixel Colors With ImageMagick

May 8th, 2009 2 comments
ImageMagick

ImageMagick

On List Central, users will be able to choose from a selection of themes for their main page. The themes available only vary by color. To make this happen I have several sets of CSS files; when the user logs in, the system checks which theme s/he selected, and serves the appropriate CSS files. These sets of CSS files are automatically created from a base set of CSS files when a them is created in the List Central Administration.

Aside from the CSS files, there are several images on the users’ main page that vary depending on what theme is selected. In the name of saving time in the future, I set out to figure out a way to create these images automatically as well. I really don’t like busy work, and I’m willing to go to great lengths to find a way around having to do any!

My general approach was, for each image, go through each and every pixel in the image, and see if it is one of the colors I want to change. If it is, then I change it, else, I don’t do anything with it. I’m assuming that the image will only have the colors I want to change in it. The code can easily be modified to handle this case differently.

On with the code!

my @BaseColors = (‘#ffffff’, ‘#0000ff’, ‘#00ff00′, ‘#ff0000′, ‘#000000′);
my @NewColors = (‘#ffffff’, ‘#121d52′, ‘#1d687a’, ‘#bdeaff’, ‘#f8f8ff’);

my $imSrcImg = new Image::Magick;
my $imagefile = ‘source_image.png’;
my $warn = $imSrcImg->Read($imagefile);
print ‘READ WARN: $warn’;

my ($width, $height) = $imSrcImg->Get(‘width’, ‘height’);
print ‘GET WARN: $warn’;

my $size = $width . ‘x’ . $height;
my $iMagickNew = new Image::Magick(size=>$size);
$warn = $iMagickNew->ReadImage(‘NULL:white’);
print ‘ReadImage WARN: $warn’);

# Iterate over every pixel in the image and change
for my $y (0..($height-1)){
   for my $x (0..($width-1)){

      my (@pixel) = split(/,/, $imSrcImg->Get(‘pixel[$x,$y]‘));

      my $red = DecToHex($pixel[0]);
      my $green = DecToHex($pixel[1]);
      my $blue = DecToHex($pixel[2]);

      my $color = ‘#’ . $red . $green . $blue;

      # Check all of the colors we are going to be changing
       for(my $i = 0; $i < @BaseColors; $i++ ) {

         # Change pixel color if the pixel is the color of one
         # of our base colors but not if it's the color we
         # want to change it to already to save a fraction
         # of a moment
         if($color eq $BaseColors[$i] && $color ne $NewColors[$i]){

               # Pull out the red, green, & blue components
               my $r; my $g; my $b;
               my $changeToColor = $NewColors[$i];
               if($changeToColor =~ m/#([wd]{2})([wd]{2})([wd]{2})/){
                  $r = $1; $g = $2; $b = $3;
               }

               # Convert to hex
               my $red = HexToDec($r);
               my $green = HexToDec($g);
               my $blue = HexToDec($b);

               my $toColorHex = '#'.$r.$g.$b;
               my $toColorDec = '$red, $green, $blue';

               my $x2 = $x + 1; my $y2 = $y + 1;
               my $warn = $iMagickNew->Draw(fill=>$toColorHex, primitive=>’point’, points=>’$x,$y’);
               print ‘Changing pixl [$x,$y] from $color to $toColorHex ($toColorDec)nWARN: $warn’;
            }
         }
      }
}

# Write the new image, then we are done
my $newImage = ‘new_image.png’;
my $warn = $iMagickNew->Write($newImage);
print ‘WARN: $warn’;

# Converts a decimal number to a hexidecimal number
sub DecToHex {
   my $dec = shift;

   my $hex = sprintf(‘%X’, $dec);
   if($hex =~ m/^([wd]{2})/){
      $hex = $1;
   }

   $hex = lc($hex);
   return $hex;
}

# Converts a hexidecimal number to a decimal number
sub HexToDec {
   my $hex = shift;

   my $dec = hex($hex);

   return $dec;
}

WARNING: This code will not work as is. There are apostrophes that should be double apostrophes and other such aesthetic changes.

I’ve played around with ImageMagick in the past, so it was the logical choice for my image manipulation package.

I had one major sticking point in trying to get my pixels to change color, which all stemmed from the lack of quality documentation available for the ImageMagick perl package. I love PerlMagick! It is super powerful! It has always baffled me how poor the documentation is. Hopefully my sharing this tidbit will help some other poor soul out there who is struggling with Perl Magick.

Here’s the crux of it: You cannot get a pixel to display in a certain color with:

$iMagick->Set(‘pixel[49,49]‘=>’red’);

as you might assume from the documentation. You have to use the ‘Draw’ subroutine like this:

$iMagick->Draw(fill=>red, primitive=>’point’, points=>’49,49);

It took me hours to figure that out!

I hope my future users like the whole theme selection business on List Central after all of this effort! It was fun to make it happen though! Its a win win!