Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.
Showing posts with label gmail. Show all posts
Showing posts with label gmail. Show all posts

2009/07/07

Send Gmail With Perl

I don't want to run an MTA on my desktop. MTAs are known vectors for hackers looking for buffer overflows, so leave it the the mail admin or Google to handle it.

But you want the ability to send mail from the command line. It's just so convenient!

cat code.pl | mail -s "The script in question" target@someplace.edu

Beyond that, well, Google has that GMail thing down. Down so well, it's out of Beta. Finally! Large quotas, high availability, POP and IMAP (IMAP is the best!)

Perl to the rescue!

I saw a blog post about sending Gmail email with Perl, using Net::SMTP::TLS. I then decided to write a program that behaves nearly like the mail command line program. (I added a -t flag for Getopt::Long completeness, rather than trying to fish an email address out of @ARGV.)


#!/usr/bin/perl

# usage: cat foo | mail.pl -subject "subject line" -to mail@mail.com

use Modern::Perl ;
use Net::SMTP::TLS ;
use Getopt::Long ;
use IO::Interactive qw{ interactive } ;
use subs qw{ send_mail get_credentials } ;

my %msg ;
my $to = '' ;
my $cc = '' ;
my $bcc = '' ;
my $header = '' ;
my $subject = '' ;
my $body = '' ;

GetOptions( 'header=s' => \$header,
'subject=s' => \$subject,
'to=s' => \$to,
'cc=s' => \$cc,
'bcc=s' => \$bcc,
) ;

while ( my $line = <STDIN> ) { $body .= $line ; }

$msg{ body } = $body ;
$msg{ subject } = $subject ;
$msg{ to } = $to ;
$msg{ bcc } = $bcc ;
$msg{ cc } = $cc ;
$msg{ header } = $header ;

send_mail %msg ;

exit ;

sub get_credentials {
#this is the point where my setup sucks
my %creds ;
$creds{ username } = 'NO' ;
$creds{ password } = 'A Thousand Times NO' ;
return %creds ;
}

sub send_mail {
my %msg = @_ ;
my %creds = get_credentials ;
my $mailer = new Net::SMTP::TLS(
'smtp.gmail.com',
Hello => 'smtp.gmail.com',
Port => 587,
User => $creds{ username } ,
Password=> $creds{ password } ,
);
$mailer->mail('jacoby.david@gmail.com');
$mailer->to( $msg{to} ) ;
$mailer->cc( $msg{cc} ) if $msg{cc} =~ /\w/ ;
$mailer->bcc( $msg{bcc} ) if $msg{bcc} =~ /\w/ ;
$mailer->data;
$mailer->datasend( "Subject: $msg{subject} \n" );
$mailer->datasend( "\n" );
$mailer->datasend( $msg{body} );
$mailer->dataend;
$mailer->quit;
}


I still do not have the store username and password in a secured config file part done. That's the part waiting for the Google Talk script, the Google status script, the Twitter stuff, etc. If you have ideas, I'd be glad to hear 'em.

2009/03/26

Setting your GTalk Status with Perl

When confronted with a problem, I used to go to Google and type perl problem-domain , but I stopped that, because that, at best, sent me to a module page on CPAN, which, unfortunately, doesn't always tell you how to use the module.

I have started to use perl problem-domain site:blogspot.com instead, as it is likely that someone hit the same problem and found a solution. When I started looking into the problem of changing the status for Google Talk, I did this, and found this page, ending with the line "Worked? Or Crap?" I could not help responding "Worked AND Crap."

"Worked" is nothing to sneeze at. The IETF motto is "rough consensus and running code", and when you have code that does what you ask it to, what more do you want? But when you use the same variable name three times for three different things, you have to wonder if you could do a better job with it.


#!/usr/bin/perl
# http://digitalpbk.blogspot.com/2009/02/perl-change-google-talk-status.html
#usage : perl stat.pl

use Modern::Perl ;
use Net::XMPP ;
use subs qw{ get_status set_status } ;

my $status = get_status ;
if ( defined $status && length $status > 1 ) {
set_status $status ;
}
exit ;

sub get_credentials {
#this is the point where my setup sucks
my %creds ;
$creds{ username } = 'NO' ;
$creds{ password } = 'A Thousand Times NO' ;
return %creds ;
}

sub get_status {
return join ' ', @ARGV ;
}

sub set_status {
my $status = shift ;
my %cred = get_credentials ;
my $username = $cred{ username } ;
my $password = $cred{ password } ;
my $hostname = 'talk.google.com' ;
my $port = 5222 ;
my $componentname = 'gmail.com' ;
my $connectiontype = 'tcpip' ;
my $tls = 1 ;
my $Con = new Net::XMPP::Client( debuglevel => 0 ) ;
my $con_status = $Con->Connect( hostname => $hostname,
port => $port,
componentname => $componentname,
connectiontype => $connectiontype,
tls => $tls,
timeout => 10 ) ;

if ( !( defined( $con_status ) ) ) {
exit( 0 ) ;
}
my $sid = $Con->{ SESSION }->{ id } ;
$Con->{ STREAM }->{ SIDS }->{ $sid }->{ hostname } = $componentname ;
my @result = $Con->AuthSend( username => $username,
password => $password,
resource => "neuron" ) ;
$Con->Send(
"<iq type='get' to='gmail.com'>"
) ;
$Con->Process() ;
my $iq = $Con->SendAndReceiveWithID(
"<iq type='get' to='$username\@gmail.com'>"
) ;
my ( $cur_status, $statuslist, $show ) = ( "", "", "" ) ;
$cur_status = $1 if ( $iq->GetXML() =~ m/(.*?)<\/status>/ ) ;
$statuslist = $1
if ( $iq->GetXML() =~ m/(<status-list(.*)<\/status-list>)/ ) ;
$show = $1 if ( $iq->GetXML() =~ m/<show>(.*?)<\/show>/ ) ;
##Change status
$Con->Send(
"<iq type='set' to='$username\@gmail.com'>
<query xmlns='google:shared-status'>
<status>$status

<show>$show
</query></iq>" ) ;
$Con->Process() ;
$Con->Disconnect() ;
return $status ;
}


I'll admit that set_status is fairly cargo-cult. But I think it is a little less crap that it was before. Thanks to digitalpbk for the code.

2007/10/24

Telling the world ....

From the first time I tried using an external mail-reader, I became convinced of one thing.

POP3 sucks. IMAP rules.

Well, rules enough. In comparison. I still preferred pine and a shell.

Then GMail came. And I tried it, and it was good. But it was Web and POP3, and POP3 sucks.

I did the web interface for a while, building rules, building folders. The POP interface ignores those rules, ignores those folders, so, in Thunderbird, I had to recreate all the rules and folders I had built in GMail. Which sucks. Beyond, of course, the inherent suckiness of POP.

Now, GMail is offering an IMAP interface.

I don't know if it'll do everything I want it to. Strictly speaking, if I make a DrDobbs folder in GMail and a rule to put the Dr. Dobbs mail in that folder, I want whatever mail interface I have to respect that. So, we'll see.

Of course, nothing beats having Procmail or MH. (I prefer the rules language for MH, but SpamAssassin works with Procmail, so I use Procmail.) But if your mail host disallow executing abstract code triggered by email, then some sorting and IMAP's the next best thing.