Martin's Tex-Blog

Posts on programming and generally technical topics

Archive for January 2011

How to send email from your perl script?

leave a comment »


Suppose you have some script working as a cron job and you want to be informed of its status from time to time. The obvious thing is to use email notifications but as always its not that easy. Especially if you have SSL / TLS protected email provider like gmail. If you have configured local email server then its straightforward solution to just use it. Other wise you will want to use some of the extensions packages. I found that using packages that allow to use email servers like gmail is not easy – it probably requires recompilation of your perl distibution – I’am not going to dive into details here so if you want a quick solution then:

1. Use email provider that is not requiring SSL connection

2. Use Mail::SendEasy

as for 2. below is a simple routine to send email :

use Mail::SendEasy;

sub ReportEmail {
  my $body = shift;
  $status =1;
  $cnt=0;
  do { # in case of errors on the line...
    my $mail = new Mail::SendEasy(
      smtp => 'smtp.somwhere.com',
      port => 587,
      user => 'username' ,
      pass => 'password' ,
    ) ;
    $status = $mail->send(
      from    => 'martin@somewhere.com' ,
      from_title => 'script report' ,
      reply   => 'martin@somewhere.com' ,
      error   => 'martin@somewhere.com' ,
      to      => 'my_email@somewhere.com' ,
      subject => "WebSite was hijacked!!!!" ,
      msg     => $body,
      msgid   => "0101",
      );
    if (!$status) {
     # log errors here
     # print LOG "SendMailError (".$cnt.") ".$mail->error ;
      $cnt++;
    }
  } while($status == 0 && $cnt != 10);
}
Advertisement

Written by Marcin

January 6, 2011 at 7:26 pm

Posted in perl

TypeForwardedTo attribute in action

leave a comment »


If you will try to test this attribute as it is described in MCTS Self-Paced Training Kit (70-536) in chapter 1 on pg. 48 then you might get into trouble. I don’t mean it is wrong,  but it is missing some intricacies. One is that type being moved must be in the same namespace as the original one. Suppose you have initially:

1.

Application.cs

ClassLibrary1.cs

using System;
namespace ClassLibrary1
{
public class Class1
{
}
}

ClassLibrary1.cs makes into ClassLibrary1.dll and Application.cs into Application.exe. Application.exe references ClassLibrary1.dll and all is working well, until someone decides to clean up code and move ClassLibrary1.Class1 into ClassLibrary2.dll. With the help of TypeForwardedTo you dont have to recompile application.exe, but only ClassLibrary1.dll and ClassLibrary2.dll. Now we have to do below changes:

2.

Application.cs – No! changes here

ClassLibrary1.cs – remember to reference in ClassLibrary1.dll a new library : ClassLibrary2.dll

using System;
using System.Runtime.CompilerServices;
using ClassLibrary2;

[assembly: TypeForwardedTo(typeof(ClassLibrary1.Class1))]

namespace ClassLibrary1
{
public class Class1Ex /// there is no more Class1 type here
{
}
}

ClassLibrary2.cs – New class, this is where Class1 type is being forwarded

using System;

namespace ClassLibrary1 // forwarded type, same as in ClassLibrary1.dll
{
public class Class1
{
}
}

namespace ClassLibrary2
{
public class SomeClass // some dummy class
{
}
}

If you are seeing exceptions running Application.exe then this might mean that you have not defined correctly in ClassLibrary2 a ClassLibrary1.Class1 type.

Written by Marcin

January 3, 2011 at 10:54 pm

Posted in MCTS 70-536