lunes 30 de marzo de 2009

Almost 2 years later....

I have almost forgot about this and then a friend of mine remind me and I decided to give it another shot.

So, today i was just waiting for my lunch and I was reading an O'reilly article about how to send SMS with Skype using apple script so I took the idea remove most line change a few others and i got this:


tell application "Skype"
set SMS to (word 2 of (send command ("CREATE SMS OUTGOING " & phoneNumber) script name "Notifier") as text)
send command (("SET SMS " & SMS & " BODY " & SMSText) as string) script name "Notifier"
send command (("ALTER SMS " & SMS & " SEND") as string) script name "Notifier"

end tell


So i save the file as an script in my

~Library/Scripts/Applications/Skype

And then i got the 'Send SMS' from the Scripts menu when i use skype, i like it this way it is simpler to do when in need.

martes 24 de abril de 2007

Today is one of these "I told you so" days

Today i was in the need of something to delete most of the old accounts in my email server, most of these accounts wasn't use in like 1 year, but i also never got a list of people who left the company so i can keep it updated, and when you have like 100 email domains and almost 10,000 emails it is difficult to go and do it one by one.

So i was thinking of a simple shell script which you call like:

# script.sh domain.com

and it will first delete the accounts which never weren't used and with something like these

# script.sh domain.com 2005

It will delete the accounts which were last used in 2005

So here it is:

#!/bin/sh

REASON="last auth: Never logged in"

if [ -z $1 ]; then

echo "Please provide a valid domain: $0 emaildomain.com"
exit;
fi

if [ ! -z $2 ]; then

REASON=$2

fi

for email in *

do

if [ -d $email ] && [ $email != "postmaster" ]; then
LAST_LOGIN="`/usr/home/vpopmail/bin/vuserinfo -l $email@$1`"
NEVER=`echo $LAST_LOGIN | grep "$REASON"`

if [ ! -z "$NEVER" ]; then
echo $email >> $1"-deleted.txt"
echo ~vpopmail/bin/vdeluser $email@$1
fi
fi

done

==-=-=-=-===-=-=-=-===-=-=-=-===-=-=-=-===-=-=-=-=

In the end it will give you the command list to delete the accounts but not actually delete them, if you want to delete them you can just add a "| sh" after the command.

jueves 19 de abril de 2007

Today's crazy idea

Today i was asked by several people what does this bounce email means...

So i came up with this idea, this is a small script which goes in a .qmail-bps file, (bps stands for bounce parse system), so when an user is unsure about a bounce they just create a new message and send it to bps@mydomain.com and includes as many bounces messages as attachments and in a few seconds they got an answer of their meaning, that means more time for me to have tea in the morning :)

Here you go, enjoy and i will enjoy my tea

#!/usr/bin/perl

use strict;
use warnings;
use Email::MIME;
use File::Basename;
use LWP::Simple;
use Mail::Header;
use MIME::Parser;
use Data::Dumper;
use Net::SMTP_auth;
use Time::HiRes qw(time);
use Digest::MD5 qw(md5_hex);
use Mail::DeliveryStatus::BounceParser;

my $outDir = md5_hex(time());
my $tmpMail = '/tmp/mails/' . $outDir . '/';
mkdir($tmpMail);

sub sendMail{

my $to = shift;
my $body = shift;
my $smtp = Net::SMTP_auth->new('YOURSERVER', 'Debug' => '0');
$smtp->auth('LOGIN', 'SMTPUSER', 'PASSWORD') or die q(Cannot auth);
$smtp->mail('YOUREMAIL');
$smtp->to($to);
$smtp->data();
$smtp->datasend("Subject: Bounces\n");
$smtp->datasend('From: Bounce Parser System ' . "\n");
$smtp->datasend('To:' . $to . "\n");
$smtp->datasend("\n");
$smtp->datasend($body);
$smtp->dataend();
$smtp->quit;
}

my $finalMessage = "";
my $mail;

while (<>){
$mail .= $_;
}

my $emailArr = ();
@{$emailArr} = split /\n/, $mail;
my $header = new Mail::Header $emailArr , Modify => 0;
my $from = $header->get('From');
my $em = Email::MIME->new($mail);

for ( my @parts = $em->parts ) {
my $filename = basename( $_->filename || '' );
my $basefilename = $filename || 'UNNAMED';
my $cnt = 0;

while ( -e $tmpMail . "/$filename" ) {
my ( $d, $m, $y ) = (localtime)[ 3 .. 5 ];
$filename = sprintf( "%s_%04d%02d%02d_%04d", $basefilename, $y + 1900, $m + 1, $d, ++$cnt );
}

open my $fh, ">", $tmpMail . "$filename";
binmode $fh;
print $fh $_->body;

}

chdir($tmpMail);
opendir(DIR, $tmpMail) || die "can't opendir " . $tmpMail . " $!";
my @filelist = grep { /\.[eml]/ && -f "$_" } readdir(DIR);

foreach (@filelist){

open (MSG, $tmpMail .$_ ) or die ($!);
my $status = {};
my $message = join "", ;
close (MSG);

my $bounce = eval { Mail::DeliveryStatus::BounceParser->new($message ) };

if ($@) {
$finalMessage .= qq(couldn't parse.);
next;
}

next unless $bounce->is_bounce ;
my @report = $bounce->reports; # Mail::Header objects

if ( ! scalar(@report) ) {
next;
}

$status->{'rcpt'} = $report[0]->get('email');
$status->{'posible_cause'} = $report[0]->get('std_reason');

if ( ! $report[0]->get('reason') ){
if ( $report[0]->get('raw') =~ m/(http:.+)/ ) {
my $url = get($1);
$url =~ s/\n//gi;
$url =~ m/(<p><b>EXPLANATION:<\/b><\/p>(.+)<p><b>SOLUTION:<\/b><\/p>)/;
my $error = $2;
$error =~ s/<\/*p>//gi;
$error =~ s/\s{2,}/ /gi;
$status->{'reason'} = $error;
} elsif ($report[0]->get('raw') =~ m/Blacklisted/i) {
$status->{'reason'} = "We're blacklisted";
} elsif ($report[0]->get('raw') =~ m/establish an SMTP/i) {
$status->{'reason'} = "Our server couldn't open a connection to the remote one, maybe remote server down or incorrect";
} else {
$status->{'reason'} = 'Bad Email address or server reject email';
}
} else {
$status->{'reason'} = $report[0]->get('reason');
}

$finalMessage .= $_ . qq(\n) . "Recipent: " . $status->{'rcpt'} . qq(\nReason: ) . $status->{'reason'} . qq(\n\n\n);
}

&sendMail($from,$finalMessage);
unlink <*.*>;
chdir('/tmp');
rmdir($tmpMail);

martes 3 de abril de 2007

I need to forward emails from specific senders to another email account

This script will do the trick, it just needs a sender and a target and all the emails from that sender will be send to the target email address the rest will just flow into the Inbox

#!/usr/local/bin/bash
#
# Ronnie Alfaro H
#
# To use, just save it in /usr/local/bin/fwdSelect, give it execute
# permissions and place a .qmail file in the user directory:
#
# /home/vpopmail/domains/domain.com/user/Maildir/
#
# The .qmail file must contain
#
# |/usr/local/bin/fwdSelect
# /home/vpopmail/domains/domain.com/user/Maildir/
#
# You can also put it in a .qmail-something file in the domain folder
# I make the script static because for my needs it is ok, but you can just
# change it a little so it will accept the FROM and SENDTO params as $1 and $2
#
# We need to catch the email content
MAIL=`cat`

# Qmail Fwd utility to forward the mesasge
FORWARD=/var/qmail/bin/forward

# A nice utility from qmail creator to get information about email headers
# 822field is part of mess822: http://cr.yp.to/mess822.html
FIELD822="/usr/local/bin/822field"

# Where to fwd the message
SENDTO=another@account.com

# FROM var defines whom email to forward to another address, it can be a
# domain using FROM=domain.com or just from one people using his email
# address
FROM=someone@someplace.com

# I use APG to create a random name for a file to temporary store the email,
# I had to use a file because otherwise the email content broke
APG=`/usr/local/bin/apg -d`

# To store the email content in a file
printf "%s\n" "$MAIL" > /tmp/$APG

# Lets see if we got a match
MATCH=`cat /tmp/$APG | $FIELD822 From | awk '{ print match($0,'/$FROM/')}'`

# If we have a match we forward the email to the account in the SENDTO var,
# and then we exit with a 99 status so no more lines from the .qmail file
# are executed.
# If there is no match then we just exit with a 0 code so the next line in
# the .qmail file is read
# If you want a copy of all the emails which match sent and also a local
# copy just return a 0 code not a 99 code

if [ $MATCH -gt 0 ]; then
cat /tmp/$APG | $FORWARD $SENDTO
rm /tmp/$APG
exit 99
else
exit 0
fi