Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4c2e069858
Fetching contributors…

Cannot retrieve contributors at this time

executable file 1005 lines (889 sloc) 31.673 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
#!/usr/bin/perl

#
# Command line SMTP client with SSL, STARTTLS, SMTP-AUTH and IPv6 support.
# Michal Ludvig <michal@logix.cz>, 2003-2012
# See http://smtp-cli.logix.cz for details.
# Thanks to all contributors for ideas and fixes!
#

my $version = "3.3";

#
# ChangeLog:
# * Version 3.3 (2012-07-30)
# - Moved from File::Type to File::LibMagic
# (File::Type is no longer maintained and not available
# in EPEL for RHEL 6)
#
# * Version 3.2 (2012-06-26)
# - Fixed syntax error
#
# * Version 3.1 (2012-06-25)
# - New --add-header, --replace-header and --remove-header options.
# - Improved compatibility with new IO::Socket::SSL releases.
#
# * Version 3.0 (2012-01-24)
# - Support for server SSL verification agains CA root cert.
# - Use "Content-Disposition: attachment" for all attachments
# unless --attach-inline was used.
# - No longer default to --server=localhost
# - Support for --charset=<charset> affecting all text/* parts.
# - Ensure "To: undisclosed-recipients:;" if sending only to Bcc.
#
# * Version 2.9 (2011-09-02)
# - Fixed problem when using IPv6 addresses with --server.
# For example with --server 2001:db8::123 it was connecting
# to server 2001:db8:: port 123. Fixed now.
#
# * Version 2.8 (2011-01-05)
# - Added --ssl to support for SSMTP (SMTP over SSL). This is
# turned on by default when --port=465.
#
# * Version 2.7 (2010-09-08)
# - Added support for Cc header (--cc=...)
# - Addressess (From, To, Cc) can now contain a "display name",
# for example --from="Michal Ludvig <michal@logix.cz>"
# - Support for --mail-from and --rcpt-to addresses independent
# on --from, --to, --cc and --bcc
# - Fixed warnings in Perl 5.12
#
# * Version 2.6 (2009-08-05)
# - Message building fixed for plaintext+attachment case.
# - Auto-enable AUTH as soon as --user parameter is used.
# (previously --enable-auth or --auth-plain had to be used
# together with --user, that was confusing).
# - New --print-only parameter for displaying the composed
# MIME message without sending.
# - All(?) non-standard modules are now optional.
# - Displays local and remote address on successfull connect.
#
# * Version 2.5 (2009-07-21)
# - IPv6 support provided the required modules are
# available.
#
# * Version 2.1 (2008-12-08)
# - Make the MIME modules optional. Simply disable
# the required functionality if they're not available.
#
# * Version 2.0 (2008-11-18)
# - Support for message building through MIME::Lite,
# including attachments, multipart, etc.
#
# * Version 1.1 (2006-08-26)
# - STARTTLS and AUTH support
#
# * Version 1.0
# - First public version
#
# This program is licensed under GNU Public License v3 (GPLv3)
#

## Require Perl 5.8 or higher -> we need open(.., .., \$variable) construct
require 5.008;

use strict;
use IO::Socket::INET;
use MIME::Base64 qw(encode_base64 decode_base64);
use Getopt::Long;
use Socket qw(:DEFAULT :crlf);

my ($user, $pass, $host, $port, $addr_family,
    $use_login, $use_plain, $use_cram_md5,
    $ehlo_ok, $auth_ok, $starttls_ok, $ssl, $verbose,
    $hello_host, $datasrc,
    $mail_from, @rcpt_to, $from, @to, @cc, @bcc,
    $missing_modules_ok, $missing_modules_count,
    $subject, $body_plain, $body_html, $charset, $print_only,
    @attachments, @attachments_inline,
    @add_headers, @replace_headers, @remove_headers,
    $ssl_ca_file, $ssl_ca_path,
    $sock, $built_message);

$host = undef;
$port = 'smtp(25)';
$addr_family = AF_UNSPEC;
$hello_host = 'localhost';
$verbose = 0;
$use_login = 0;
$use_plain = 0;
$use_cram_md5 = 0;
$starttls_ok = 1;
$ssl = undef;
$auth_ok = 0;
$ehlo_ok = 1;
$missing_modules_ok = 0;
$missing_modules_count = 0;
$charset = undef;
$print_only = 0;

# Get command line options.
GetOptions (
'host|server=s' => \$host,
'port=i' => \$port,
'4|ipv4' => sub { $addr_family = AF_INET; },
'6|ipv6' => sub { $addr_family = AF_INET6; },
'user=s' => \$user, 'password=s' => \$pass,
'auth-login' => \$use_login,
'auth-plain' => \$use_plain,
'auth-cram-md5' => \$use_cram_md5,
'disable-ehlo' => sub { $ehlo_ok = 0; },
'force-ehlo' => sub { $ehlo_ok = 2; },
'hello-host|ehlo-host|helo-host=s' => \$hello_host,
'auth|enable-auth' => \$auth_ok,
'disable-starttls|disable-tls|disable-ssl' =>
sub { $starttls_ok = 0; },
'ssl' => sub { $ssl = 1 },
'disable-ssl' => sub { $ssl = 0 },
'mail-from=s' => \$mail_from,
'rcpt-to=s' => \@rcpt_to,
'from=s' => \$from,
'to=s' => \@to,
'cc=s' => \@cc,
'bcc=s' => \@bcc,
'data=s' => \$datasrc,
'subject=s' => \$subject,
'body|body-plain=s' => \$body_plain,
'body-html=s' => \$body_html,
'charset=s' => \$charset,
'attachment|attach=s' => \@attachments,
'attachment-inline|attach-inline=s' => \@attachments_inline,
'add-header=s' => \@add_headers,
'replace-header=s' => \@replace_headers,
'remove-header=s' => \@remove_headers,
'print-only' => \$print_only,
'missing-modules-ok' => \$missing_modules_ok,
'ssl-ca-file=s' => \$ssl_ca_file,
'ssl-ca-path=s' => \$ssl_ca_path,
'v|verbose+' => \$verbose,
'version' => sub { &version() },
'help' => sub { &usage() } );

#### Try to load optional modules

## IO::Socket::SSL and Net::SSLeay are optional
my $have_ssl = eval { require IO::Socket::SSL; require Net::SSLeay; 1; };
if (not $have_ssl and not $missing_modules_ok) {
warn("!!! IO::Socket::SSL and/or Net::SSLeay modules are not found\n");
warn("!!! These modules are required for SSL and STARTTLS support\n");
$missing_modules_count += 2;
}

## IO::Socket::INET6 and Socket6 are optional
my $socket6 = eval { require IO::Socket::INET6; require Socket6; 1; };
if (not $socket6) {
if ($addr_family == AF_INET6) {
die("!!! IO::Socket::INET6 and Socket6 modules are not found\nIPv6 support is not available\n");
}
if (not $missing_modules_ok) {
warn("!!! IO::Socket::INET6 -- optional module not found\n");
warn("!!! Socket6 -- optional module not found\n");
warn("!!! These modules are required for IPv6 support\n\n");
$missing_modules_count += 2;
}
}

## MIME::Lite dependency is optional
my $mime_lite = eval { require MIME::Lite; 1; };
if (not $mime_lite and not $missing_modules_ok) {
warn("!!! MIME::Lite -- optional module not found\n");
warn("!!! Used for composing messages from --subject, --body, --attachment, etc.\n\n");
$missing_modules_count++;
}

## File::LibMagic dependency is optional
my $file_libmagic = eval { require File::LibMagic; File::LibMagic->new(); };
if (not $file_libmagic and not $missing_modules_ok) {
warn("!!! File::LibMagic -- optional module not found\n");
warn("!!! Used for guessing MIME types of attachments\n\n");
$missing_modules_count++;
}

## Term::ReadKey dependency is optional
my $have_term_readkey = eval { require Term::ReadKey; 1; };
if (not $have_term_readkey and not $missing_modules_ok) {
warn("!!! Term::ReadKey -- optional module not found\n");
warn("!!! Used for hidden reading SMTP password from the terminal\n\n");
$missing_modules_count++;
}

my $have_hmac_md5 = eval { require Digest::HMAC_MD5; 1; };
if (not $have_hmac_md5 and not $missing_modules_ok) {
if ($use_cram_md5) {
die("!!! CRAM-MD5 authentication is not available because Digest::HMAC_MD5 module is missing\n");
}
warn("!!! Digest::HMAC_MD5 -- optional module missing\n");
warn("!!! Used for CRAM-MD5 authentication method\n");
$missing_modules_count++;
}

## Advise about --missing-modules-ok parameter
if ($missing_modules_count) {
warn("!!! Use --missing-modules-ok if you don't need the above listed modules\n");
warn("!!! and don't want to see this message again.\n\n");
}

## Make sure we've got a server name to connect to
if (not defined($host)) {
if (not $print_only) {
die("Error: Specify the SMTP server with --server=hostname[:port]\n");
} else {
# We're printing to stdout only, let's assign just about any
# hostname to satisfy the next few tests.
$host = "localhost";
}
}

## Accept hostname with port number as host:port
## Either it's a hostname:port or 1.2.3.4:port or [2001:db8::1]:port.
## Don't parse 2001:db8::1 as $host=2001:db8:: and $port=1!
if (($host =~ /^([^:]+):([:alnum:]+)$/) or
    ($host =~ /^\[([[:xdigit:]:]+)\]:([:alnum:]+)$/))
{
$host = $1;
$port = $2;
}

## Automatically start in SSL mode if port == 465 (SSMTP)
if (not defined($ssl)) {
$ssl = ($port == 465);
}

# Extract $mail_from address from $from
if (not defined($mail_from) and defined($from)) {
$mail_from = &find_email_addr($from) or
die ("The --from string does not contain a valid email address: $from\n");
}

# Extract @rcpt_to list from @to, @cc and @bcc
if (not @rcpt_to) {
foreach my $rcpt (@to, @cc, @bcc) {
my $rcpt_addr = &find_email_addr($rcpt);
if (not defined($rcpt_addr)) {
warn("No valid email address found in: $rcpt\n");
next;
}
push(@rcpt_to, $rcpt_addr);
}
}

# Ensure "To: undisclosed-recipients:;" when sending only to Bcc's
if (not @to and not @cc) {
push(@to, "undisclosed-recipients:;");
}

# Build the MIME message if required
if (defined($subject) or defined($body_plain) or defined($body_html) or
@attachments or @attachments_inline) {
if (not $mime_lite) {
die("Module MIME::Lite is not available. Unable to build the message, sorry.\n".
"Use --data and provide a complete email payload including headers instead.\n");
}
if (defined($datasrc)) {
die("Requested building a message and at the same time used --data parameter.\n".
"That's not possible, sorry.\n");
}
if (defined($body_plain) and -f $body_plain) {
local $/=undef;
open(FILE, $body_plain);
$body_plain = <FILE>;
close(FILE);
}
if (defined($body_html) and -f $body_html) {
local $/=undef;
open(FILE, $body_html);
$body_html = <FILE>;
close(FILE);
}
my $message = &build_message();

open(BUILT_MESSAGE, "+>", \$built_message);
$datasrc = "///built_message";
if ($print_only) {
$message->print();
exit(0);
} else {
$message->print(\*BUILT_MESSAGE);
}
seek(BUILT_MESSAGE, 0, 0);
}

# Username was given -> enable AUTH
if ($user)
{ $auth_ok = 1; }

# If at least one --auth-* option was given, enable AUTH.
if ($use_login + $use_plain + $use_cram_md5 > 0)
{ $auth_ok = 1; }

# If --enable-auth was given, enable all AUTH methods.
elsif ($auth_ok && ($use_login + $use_plain + $use_cram_md5 == 0))
{
$use_login = 1;
$use_plain = 1;
$use_cram_md5 = 1 if ($have_hmac_md5);
}

# Exit if user haven't specified username for AUTH.
if ($auth_ok && !defined ($user))
{ die ("SMTP AUTH support requested without --user\n"); }

# Ask for password if it wasn't supplied on the command line.
if ($auth_ok && defined ($user) && !defined ($pass))
{
if ($have_term_readkey) {
# Set echo off.
Term::ReadKey::ReadMode (2);
} else {
warn ("Module Term::ReadKey not available - password WILL NOT be hidden!!!\n");
}
printf ("Enter password for %s@%s : ", $user, $host);
$pass = <>;
if ($have_term_readkey) {
# Restore echo.
Term::ReadKey::ReadMode (0);
printf ("\n");
}
exit if (! defined ($pass));
chop ($pass);
}

# Connect to the SMTP server.
my %connect_args = (
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp',
Timeout => 5);
if ($socket6) {
$connect_args{'Domain'} = $addr_family;
$sock = IO::Socket::INET6->new(%connect_args) or die ("Connect failed: $@\n");
} else {
$sock = IO::Socket::INET->new(%connect_args) or die ("Connect failed: $@\n");
}

if ($verbose >= 1) {
my $addr_fmt = "%s";
$addr_fmt = "[%s]" if ($sock->sockhost() =~ /:/); ## IPv6 connection

printf ("Connection from $addr_fmt:%s to $addr_fmt:%s\n",
$sock->sockhost(), $sock->sockport(),
$sock->peerhost(), $sock->peerport());
}

if ($ssl) {
printf ("Starting SMTP/SSL...\n") if ($verbose >= 1);
&socket_to_ssl($sock);
}

my ($code, $text);
my (%features);

# Wait for the welcome message of the server.
($code, $text) = &get_line ($sock);
die ("Unknown welcome string: '$code $text'\n") if ($code != 220);
$ehlo_ok-- if ($text !~ /ESMTP/);

# Send EHLO
&say_hello ($sock, $ehlo_ok, $hello_host, \%features) or exit (1);

# Run the SMTP session
&run_smtp ();

# Good bye...
&send_line ($sock, "QUIT\n");
($code, $text) = &get_line ($sock);
die ("Unknown QUIT response '$code'.\n") if ($code != 221);

exit 0;

# This is the main SMTP "engine".
sub run_smtp
{
# See if we could start encryption
if ((defined ($features{'STARTTLS'}) || defined ($features{'TLS'})) && $starttls_ok && !$have_ssl)
{
warn ("Module IO::Socket::SSL is missing - STARTTLS support disabled.\n");
warn ("Use --disable-starttls or install the modules to avoid this warning.\n");
undef ($features{'STARTTLS'});
undef ($features{'TLS'});
}

if ((defined ($features{'STARTTLS'}) || defined ($features{'TLS'})) && $starttls_ok)
{
printf ("Starting TLS...\n") if ($verbose >= 1);

&send_line ($sock, "STARTTLS\n");
($code, $text) = &get_line ($sock);
die ("Unknown STARTTLS response '$code'.\n") if ($code != 220);

&socket_to_ssl($sock);

# Send EHLO again (required by the SMTP standard).
&say_hello ($sock, $ehlo_ok, $hello_host, \%features) or return 0;
}

# See if we should authenticate ourself
if (defined ($features{'AUTH'}) && $auth_ok)
{
printf ("AUTH method (%s): ", $features{'AUTH'}) if ($verbose >= 1);

## Try DIGEST-MD5 first
# Actually we won't. It never worked reliably here.
# After all DIGEST-MD5 is on a way to deprecation
# see this thread: http://www.imc.org/ietf-sasl/mail-archive/msg02996.html

# Instead use CRAM-MD5 if supported by the server
if ($features{'AUTH'} =~ /CRAM-MD5/i && $use_cram_md5)
{
printf ("using CRAM-MD5\n") if ($verbose >= 1);
&send_line ($sock, "AUTH CRAM-MD5\n");
($code, $text) = &get_line ($sock);
if ($code != 334)
{ die ("AUTH CRAM-MD5 failed: $code $text\n"); }

my $response = &encode_cram_md5 ($text, $user, $pass);
&send_line ($sock, "%s\n", $response);
($code, $text) = &get_line ($sock);
if ($code != 235)
{ die ("AUTH CRAM-MD5 failed: $code $text\n"); }
}
# Eventually try LOGIN method
elsif ($features{'AUTH'} =~ /LOGIN/i && $use_login)
{
printf ("using LOGIN\n") if ($verbose >= 1);
&send_line ($sock, "AUTH LOGIN\n");
($code, $text) = &get_line ($sock);
if ($code != 334)
{ die ("AUTH LOGIN failed: $code $text\n"); }

&send_line ($sock, "%s\n", encode_base64 ($user, ""));

($code, $text) = &get_line ($sock);
if ($code != 334)
{ die ("AUTH LOGIN failed: $code $text\n"); }

&send_line ($sock, "%s\n", encode_base64 ($pass, ""));

($code, $text) = &get_line ($sock);
if ($code != 235)
{ die ("AUTH LOGIN failed: $code $text\n"); }
}
# Or finally PLAIN if nothing else was supported.
elsif ($features{'AUTH'} =~ /PLAIN/i && $use_plain)
{
printf ("using PLAIN\n") if ($verbose >= 1);
&send_line ($sock, "AUTH PLAIN %s\n",
encode_base64 ("$user\0$user\0$pass", ""));
($code, $text) = &get_line ($sock);
if ($code != 235)
{ die ("AUTH PLAIN failed: $code $text\n"); }
}
# Complain otherwise.
else
{
warn ("No supported authentication method\n".
"advertised by the server.\n");
return 0;
}

printf ("Authentication of $user\@$host succeeded\n") if ($verbose >= 1);
}

# We can do a relay-test now if a recipient was set.
if ($#rcpt_to >= 0)
{
if (!defined ($mail_from))
{
warn ("From: address not set. Using empty one.\n");
$mail_from = "";
}
&send_line ($sock, "MAIL FROM: <%s>\n", $mail_from);
($code, $text) = &get_line ($sock);
if ($code != 250)
{
warn ("MAIL FROM <$mail_from> failed: '$code $text'\n");
return 0;
}

my $i;
for ($i=0; $i <= $#rcpt_to; $i++)
{
&send_line ($sock, "RCPT TO: <%s>\n", $rcpt_to[$i]);
($code, $text) = &get_line ($sock);
if ($code != 250)
{
warn ("RCPT TO <".$rcpt_to[$i]."> ".
"failed: '$code $text'\n");
return 0;
}
}
}

# Wow, we should even send something!
if (defined ($datasrc))
{
if ($datasrc eq "///built_message")
{
*MAIL = *BUILT_MESSAGE;
}
elsif ($datasrc eq "-")
{
*MAIL = *STDIN;
}
elsif (!open (MAIL, $datasrc))
{
warn ("Can't open file '$datasrc'\n");
return 0;
}

&send_line ($sock, "DATA\n");
($code, $text) = &get_line ($sock);
if ($code != 354)
{
warn ("DATA failed: '$code $text'\n");
return 0;
}

while (<MAIL>)
{
my $line = $_;
$line =~ s/^\.$CRLF$/\. $CRLF/;
$line =~ s/^\.\n$/\. $CRLF/;
$sock->print ($line);
}

close (MAIL);

$sock->printf ("$CRLF.$CRLF");

($code, $text) = &get_line ($sock);
if ($code != 250)
{
warn ("DATA not send: '$code $text'\n");
return 0;
}
}

# Perfect. Everything succeeded!
return 1;
}

# Get one line of response from the server.
sub get_one_line ($)
{
my $sock = shift;
my ($code, $sep, $text) = ($sock->getline() =~ /(\d+)(.)([^\r]*)/);
my $more;
$more = ($sep eq "-");
if ($verbose)
{ printf ("[%d] '%s'\n", $code, $text); }
return ($code, $text, $more);
}

# Get concatenated lines of response from the server.
sub get_line ($)
{
my $sock = shift;
my ($code, $text, $more) = &get_one_line ($sock);
while ($more) {
my ($code2, $line);
($code2, $line, $more) = &get_one_line ($sock);
$text .= " $line";
die ("Error code changed from $code to $code2. That's illegal.\n") if ($code ne $code2);
}
return ($code, $text);
}

# Send one line back to the server
sub send_line ($@)
{
my $socket = shift;
my @args = @_;

if ($verbose)
{ printf ("> "); printf (@args); }
$args[0] =~ s/\n/$CRLF/g;
$socket->printf (@args);
}

sub socket_to_ssl($)
{
if (!$have_ssl) {
die ("SSL/TLS support is not available due to missing modules. Sorry.\n");
}

# Do Net::SSLeay initialization
Net::SSLeay::load_error_strings();
Net::SSLeay::SSLeay_add_ssl_algorithms();
Net::SSLeay::randomize();

if (! IO::Socket::SSL->start_SSL($sock, {
SSL_ca_file => $ssl_ca_file,
SSL_ca_path => $ssl_ca_path,
SSL_verify_mode => (defined($ssl_ca_file) or defined($ssl_ca_path)) ? 0x01 : 0x00,
}))
{
die ("SSL/TLS: ".IO::Socket::SSL::errstr()."\n");
}

if ($verbose >= 1)
{
printf ("Using cipher: %s\n", $sock->get_cipher ());
printf ("%s", $sock->dump_peer_certificate());
}
}

# Helper function to encode CRAM-MD5 challenge
sub encode_cram_md5 ($$$)
{
my ($ticket64, $username, $password) = @_;
my $ticket = decode_base64($ticket64) or
die ("Unable to decode Base64 encoded string '$ticket64'\n");

print "Decoded CRAM-MD5 challenge: $ticket\n" if ($verbose > 1);
my $password_md5 = Digest::HMAC_MD5::hmac_md5_hex($ticket, $password);
return encode_base64 ("$username $password_md5", "");
}

# Store all server's ESMTP features to a hash.
sub say_hello ($$$$)
{
my ($sock, $ehlo_ok, $hello_host, $featref) = @_;
my ($feat, $param);
my $hello_cmd = $ehlo_ok > 0 ? "EHLO" : "HELO";

&send_line ($sock, "$hello_cmd $hello_host\n");
my ($code, $text, $more) = &get_one_line ($sock);

if ($code != 250)
{
warn ("$hello_cmd failed: '$code $text'\n");
return 0;
}

# Empty the hash
%{$featref} = ();

($feat, $param) = ($text =~ /^(\w+)[= ]*(.*)$/);
$featref->{$feat} = $param;

# Load all features presented by the server into the hash
while ($more == 1)
{
($code, $text, $more) = &get_one_line ($sock);
($feat, $param) = ($text =~ /^(\w+)[= ]*(.*)$/);
$featref->{$feat} = $param;
}

return 1;
}

sub find_email_addr($)
{
my $addr = shift;
if ($addr =~ /\b([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]+)\b/i) {
return $1;
}
return undef;
}

sub guess_mime_type($)
{
my $filename = shift;
if (defined($file_libmagic)) {
## Use File::LibMagic if possible
return $file_libmagic->checktype_filename($filename);
} else {
## Module File::LibMagic is not available
## Still recognise some common extensions
return "image/jpeg" if ($filename =~ /\.jpe?g/i);
return "image/gif" if ($filename =~ /\.gif/i);
return "image/png" if ($filename =~ /\.png/i);
return "text/plain" if ($filename =~ /\.txt/i);
return "application/zip" if ($filename =~ /\.zip/i);
return "application/x-gzip" if ($filename =~ /\.t?gz/i);
return "application/x-bzip" if ($filename =~ /\.t?bz2?/i);
}
return "application/octet-stream";
}

sub basename($)
{
my $path = shift;
my @parts = split(/\//, $path);
return $parts[$#parts];
}

sub prepare_attachment($)
{
my $attachment = shift;
my ($path, $mime_type);

if (-f $attachment) {
$path = $attachment;
$mime_type = guess_mime_type($attachment);
} elsif ($attachment =~ /(.*)@([^@]*)$/ and -f $1) {
$path = $1;
$mime_type = $2;
}
return ($path, $mime_type);
}

sub attach_attachments($$@)
{
my $message = shift;
my $disposition = shift;
my @attachments = @_;

foreach my $attachment (@attachments) {
my ($path, $mime_type) = prepare_attachment($attachment);
if (not defined($path)) {
warn("$attachment: File not found. Ignoring.\n");
next;
}
$message->attach(
Type => $mime_type,
Path => $path,
Id => basename($path),
Disposition => $disposition,
);
}
}

sub safe_attach($$)
{
my ($message, $part) = @_;
## Remove some headers when $part is becoming a subpart of $message
$part->delete("Date");
$part->delete("X-Mailer");
$part->attr("MIME-Version" => undef);
$message->attach($part);
return $message;
}

sub mime_message($$)
{
my ($type, $data) = @_;

## Set QP encoding for text/* types, let MIME::Lite decide for all other types.
my $encoding = $type =~ /^text\// ? "quoted-printable" : undef;
my $message = MIME::Lite->new(
Type => $type,
Encoding=> $encoding,
Data => $data);
$message->attr('content-type.charset' => $charset) if (($type =~ /^text\//i) and defined($charset));
return $message;
}

sub build_message
{
my ($part_plain, $part_html, $part_body, $message);

if (@attachments_inline) {
if (not defined($body_html)) {
die("Inline attachments (--attach-inline) must be used with --body-html\n");
}
$part_html = MIME::Lite->new(Type => 'multipart/related');
$part_html->attach(Type => 'text/html', Data => $body_html);
attach_attachments($part_html, "inline", @attachments_inline);
$message = $part_html;
# undefine $body_html to prevent confusion in the next if()
undef($body_html);
}

if (defined($body_html)) {
$part_html = mime_message('text/html', $body_html);
$message = $part_html;
}

if (defined($body_plain)) {
$part_plain = mime_message('text/plain', $body_plain);
$message = $part_plain;
}

if (defined($part_plain) and defined($part_html)) {
$part_body = mime_message("multipart/alternative", undef);
safe_attach($part_body, $part_plain);
safe_attach($part_body, $part_html);
$message = $part_body;
}

if (@attachments) {
if (defined($message)) {
# We already have some plaintext and/or html content built
# => make it the first part of multipart/mixed
my $message_body = $message;
$message = mime_message("multipart/mixed", undef);
safe_attach($message, $message_body);
attach_attachments($message, "attachment", @attachments);
} elsif ($#attachments == 0) {
# Only one single attachment - let it be the body
my ($path, $mime_type) = prepare_attachment($attachments[0]);
if (not defined($path)) {
die($attachments[0].": File not found. No other message parts defined. Aborting.\n");
}
$message = MIME::Lite->new(
Type => $mime_type,
Path => $path);
} else {
# Message consisting only of attachments
$message = mime_message("multipart/mixed", undef);
attach_attachments($message, "attachment", @attachments);
}
}

# Last resort - empty plaintext message
if (!defined($message)) {
$message = mime_message("TEXT", "");
}

$message->replace("From" => $from);
$message->replace("To" => join(", ", @to));
$message->replace("Cc" => join(", ", @cc));
$message->replace("Subject" => $subject);
$message->replace("X-Mailer" => "smtp-cli $version, see http://smtp-cli.logix.cz");
$message->replace("Message-ID" => time()."-".int(rand(999999))."\@smtp-cli");

for my $header (@add_headers) {
my ($hdr, $val) = ($header =~ /^([^:]+):\s*(.*)$/);
die("Not a valid header format: ${header}\n") if (not $hdr or not $val);
$message->add($hdr => $val);
}
for my $header (@replace_headers) {
my ($hdr, $val) = ($header =~ /^([^:]+):\s*(.*)$/);
die("Not a valid header format: ${header}\n") if (not $hdr or not $val);
$message->replace($hdr => $val);
}
for my $header (@remove_headers) {
my ($hdr) = ($header =~ /^([^:\s]+)/);
$message->replace($header => "");
}

return $message;
}

sub version ()
{
print "smtp-cli version $version\n";
exit (0);
}

sub usage ()
{
printf (
"Simple SMTP client written in Perl that supports advanced
features like STARTTLS and SMTP-AUTH and IPv6. It can also
create messages from components (files, text snippets) and
attach files.

Version: smtp-cli v$version

Author: Michal Ludvig <mludvig\@logix.net.nz> (c) 2003-2011
http://smtp-cli.logix.cz

Usage: smtp-cli [--options]

--server=<hostname>[:<port>]
Host name or IP address of the SMTP server.
May include the port after colon, alternatively
use --port.
--port=<number> Port where the SMTP server is listening.
(default: 25)

-4 or --ipv4 Use standard IP (IPv4) protocol.
-6 or --ipv6 Use IPv6 protocol. For hosts that have
both IPv6 and IPv4 addresses the IPv6
connection is tried first.
--hello-host=<string> String to use in the EHLO/HELO command.
--disable-ehlo Don't use ESMTP EHLO command, only HELO.
--force-ehlo Use EHLO even if server doesn't say ESMTP.
Transport encryption (TLS)
--disable-starttls Don't use encryption even if the remote
host offers it.
--ssl Start in SMTP/SSL mode (aka SSMTP).
Default when --port=465
--disable-ssl Don't start SSMTP even if --port=465
--ssl-ca-file=<filename>
Verify the server's SSL certificate against
a trusted CA root certificate file.
--ssl-ca-path=<dirname> Similar to --ssl-ca-file but will look for
the appropriate root certificate file in
the given directory. The certificates must
must be stored one per file with hash-links
generated by, for example, c_rehash script
from OpenSSL.

Authentication options (AUTH)
--user=<username> Username for SMTP authentication.
--pass=<password> Corresponding password.
--auth-login Enable only AUTH LOGIN method.
--auth-plain Enable only AUTH PLAIN method.
--auth-cram-md5 Enable only AUTH CRAM-MD5 method.
--auth Enable all supported methods. This is
normally not needed, --user enables
everything as well.

Sender / recipient
--from=\"Display Name <add\@re.ss>\"
Sender's name address (or address only).
--to=\"Display Name <add\@re.ss>\"
--cc=\"Display Name <add\@re.ss>\"
--bcc=\"Display Name <add\@re.ss>\"
Message recipients. Each parameter can be
used multiple times.
The --bcc addresses won't apprear in
the composed message.

SMTP Envelope sender / recipient
(rarely needed, use --from, --to, --cc and --bcc instead)
--mail-from=<address> Address to use in MAIL FROM command.
Use --from instead, unless you want
a different address in the envelope and
in the headers.
--rcpt-to=<address> Address to use in RCPT TO command. Can be
used multiple times. Normally not needed,
use --to, --cc and --bcc instead.
If set the --to, --cc and --bcc will only
be used for composing the message body and
not for delivering the messages.

Send a complete RFC822-compliant email message:
--data=<filename> Name of file to send after DATA command.
With \"--data=-\" the script will read
standard input (useful e.g. for pipes).

Alternatively build email a message from provided components:
--subject=<subject> Subject of the message
--body-plain=<text|filename>
--body-html=<text|filename>
Plaintext and/or HTML body of the message
If both are provided the message is sent
as multipart.
--charset=<charset> Character set used for Subject and Body,
for example UTF-8, ISO-8859-2, KOI8-R, etc.
--attach=<filename>[\@<MIME/Type>]
Attach a given filename.
MIME-Type of the attachment is guessed
by default guessed but can optionally
be specified after '\@' delimiter.
For instance: --attach mail.log\@text/plain
Parameter can be used multiple times.
--attach-inline=<filename>[\@<MIME/Type>]
Attach a given filename (typically a picture)
as a 'related' part to the above 'body-html'.
Refer to these pictures as <img src='cid:filename'>
in the 'body-html' contents.
See --attach for details about MIME-Type.
Can be used multiple times.
--add-header=\"Header: value\"
--replace-header=\"Header: value\"
--remove-header=\"Header\"
Add, Replace or Remove pretty much any header
in the email. For example to set a different
Mailer use --replace-header=\"X-Mailer: Blah\",
to remove it altogether --remove-header=X-Mailer
or to add a completely custom header use
--add-header=\"X-Something: foo bar\".
--print-only Dump the composed MIME message to standard
output. This is useful mainly for debugging
or in the case you need to run the message
through some filter before sending.

Other options
--verbose[=<number>] Be more verbose, print the SMTP session.
--missing-modules-ok Don't complain about missing optional modules.
--version Print: smtp-cli version $version
--help Guess what is this option for ;-)

PayPal donations: http://smtp-cli.logix.cz/donate
Thanks in advance for your support!

");
exit (0);
}
Something went wrong with that request. Please try again.