Saturday, December 15, 2012

How to send email from command line using Gmail (or any other provider)

If you want to send email from command line using a Gmail account (or any other provider), you can use SendEmail.

SendEmail is a lightweight, completely command line based, SMTP email agent. It was designed to be used in bash scripts, Perl programs, and web sites, but it is also quite useful in many other contexts. SendEmail is written in Perl.

1) Install SendEmail
 In debian 
#apt-get install libio-socket-ssl-perl libnet-ssleay-perl perl
#apt-get install sendemail

For other distributions:
Refer to the official site http://caspian.dotconf.net/menu/Software/SendEmail/


2) Run it
Here is an example:
sendEmail -f user@gmail.com -t myfriend@gmail.com \
-u "this is the test subject" -m "this is a test message" \
-s "smtp.gmail.com" \
-o tls=yes \
-xu user -xp password



3) Problems
If you get the following error:
"invalid SSL_version specified at /usr/share/perl5/IO/Socket/SSL.pm line 332"

It is a known bug: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=679911

A temporary workaround is to edit the file /usr/bin/sendemail on line 1907 by changing 'SSLv3 TLSv1'in  to 'SSLv3',


1903     ## Start TLS if possible
1904     if ($conf{'tls_server'} == 1 and $conf{'tls_client'} == 1 and $opt{'tls'} =~ /^(yes|auto)$/) {
1905         printmsg("DEBUG => Starting TLS", 2);
1906         if (SMTPchat('STARTTLS')) { quit($conf{'error'}, 1); }
1907         if (! IO::Socket::SSL->start_SSL($SERVER, SSL_version => 'SSLv3 TLSv1')) {
1908             quit("ERROR => TLS setup failed: " . IO::Socket::SSL::errstr(), 1);
1909         }
1910         printmsg("DEBUG => TLS: Using cipher: ". $SERVER->get_cipher(), 3);
1911         printmsg("DEBUG => TLS session initialized :)", 1);
1912 
1913         ## Restart our SMTP session
1914         if (SMTPchat('EHLO ' . $opt{'fqdn'})) { quit($conf{'error'}, 1); }
1915     }
1916     elsif ($opt{'tls'} eq 'yes' and $conf{'tls_server'} == 0) {
1917         quit("ERROR => TLS not possible! Remote SMTP server, $conf{'server'},  does not support it.", 1);
1918     }


5 comments:

  1. this is very helpful for me, thanks bro, now I can sleep well :-)

    ReplyDelete
  2. You save my live...tu me sauves la vie ! ;-))

    ReplyDelete
  3. Oh my God! You save my ass, man! I LOVE YOU! Thank you so much!!!

    ReplyDelete
  4. Perfecto, funciona OK.

    Muchas Gracias.

    ReplyDelete
  5. Perferct! Thanks a lot!

    ReplyDelete