beanscake has asked for the wisdom of the Perl Monks concerning the following question:

i have been trying to use socket to connect to SMTP server on port: 465 using SMTP TLS/SSL yes, using Perl, i need urgent help on making this request successfuly using socket,thank you.

use Socket; use MIME::Base64; my $cr = pack('H*','0D'); my $lf = pack('H*','0A'); my($username) = '***@gmail.com'; my($password) = 'mypass'; my($from) = '*******@gmail.com'; my($from_name) = 'Beanscake'; my($to) = 'emailto'; my($to_name) = 'name'; my($subject) = 'Test'; my($body) = "Test Line One.\nTest Line Two.\n"; my($mailServer) = 'smtp.gmail.com'; $main::SIG{'INT'} = 'closeSocket'; my$proto = getprotobyname('tcp'); my($port) = 465; my($serverAddr) = (gethostbyname($mailServer))[4]; socket(SMTP, AF_INET(), SOCK_STREAM(), $proto) or die("socket: $!"); $packFormat = 'S n a4 x8'; # Windows 95, SunOs 4.1+ #$packFormat = 'S n c4 x8'; # SunOs 5.4+ (Solaris 2) connect(SMTP, pack($packFormat, AF_INET(), $port, $serverAddr)) or die("connect: $!"); select(SMTP); $| = 1; select(STDOUT); # use unbuffemiles i/o. sendSMTP(1, "HELO local $cr$lf"); sendSMTP(1,"AUTH LOGIN $cr$lf"); sendSMTP(1,encode_base64($username).$cr.$lf); sendSMTP(1,encode_base64($password).$cr.$lf); sendSMTP(1, "MAIL FROM: <$from>$cr$lf"); sendSMTP(1, "RCPT TO: <$to>$cr$lf"); sendSMTP(1, "DATA $cr$lf"); sendSMTP(1, "To: $to_name<$to>$cr$lf"); sendSMTP(1, "MIME-Version: 1.0\r\n"); sendSMTP(1,"Content-Type: text/plain; charset=utf-8\r\n"); sendSMTP(1,"From: $from_name<$from>$cr$lf"); sendSMTP(1,"Subject: $subject $cr$lf\r\n"); sendSMTP(1, "$body $cr$lf"); sendSMTP(1,".$cr$lf"); sendSMTP(1, "QUIT $cr$lf"); close(SMTP); sub closeSocket { # close smtp socket on error close(SMTP); die("SMTP socket closed due to SIGINT\n"); } sub sendSMTP { my($debug) = shift; my($buffer) = @_; print STDERR ("> $buffer") if $debug; send(SMTP, $buffer, 0); recv(SMTP, $buffer, 200, 0); print STDERR ("< $buffer") if $debug; return( (split(/ /, $buffer))[0] ); }

Replies are listed 'Best First'.
Re: help on socket to smtp on TLS/SSL
by Corion (Patriarch) on Jul 19, 2014 at 13:57 UTC

      well i was able to, using socket after searching on google thanks to google for the insight, am new to perl i was sure it's possible cos this happen in php

      #!/usr/bin/perl -w use strict; use IO::Socket::INET; use MIME::Base64; my $cr = pack('H*','0D'); my $lf = pack('H*','0A'); #following http://www.perlmonks.org/bare/?node_id=316023 # see RFC 821 http://www.ietf.org/rfc/rfc0821.txt my($username) = 'email'; my($password) = 'password'; my($from) = 'email'; my($from_name) = 'name'; my($to) = 'email to send to'; my($to_name) = 'name'; my($subject) = 'Test'; my($body) = "Test Line One.\nTest Line Two.\n"; my $server = 'smtp.server.ex'; # SMTP SERVER my $port = 587; # my $timeout = 10; my $sock = IO::Socket::INET->new( PeerAddr => $server, PeerPort => $port, Proto => 'tcp', Timeout => $timeout, ); unless ( $sock ) { die"ERR - Could not connect socket to $server on port $port"; } if ( my $server = <$sock>) { print "Got Handshake: $server"; dialog( $sock, "EHLO local $cr$lf"); dialog( $sock, "AUTH LOGIN $cr$lf"); dialog( $sock, encode_base64($username)); dialog( $sock, encode_base64($password)); dialog( $sock, "MAIL FROM: <$from>$cr$lf"); dialog( $sock, "RCPT TO: <$to>$cr$lf"); dialog( $sock, "DATA $cr$lf"); dialog( $sock, "To: $to_name<$to>\r\nMIME-Version: 1.0\r\nContent-Ty +pe: text/plain; charset=utf-8\n$cr$lf"); dialog( $sock, "From: $from_name<$from>$cr$lf"); dialog( $sock, "Subject: $subject $cr$lf\r\n"); dialog( $sock, "$body $cr$lf"); dialog( $sock, ".$cr$lf"); dialog( $sock, "QUIT $cr$lf"); } else { die "No handshake sent from SMTP server\n" } sub dialog { my ( $sock, $send ) = @_; print $sock $send; print "Sent: $send"; my $receive = <$sock>; die "Got no response to $send" unless $receive; print "Recv: $receive"; }