Perl EMAIL Dependencies - perl

I am trying to use test perl's email using the example listed from
http://learn.perl.org/examples/email.html. I am running this on Windows 7.
For some reason, Email::Sender::Simple is referencing other modules that are not loaded when Email::Sender is loaded.
How can one load these modules so that all dependencies get loaded without searching in each module for all referenced packages? Currently I am using ActiveState and ppm install.
use 5.14.2;
use strict;
use warnings;
# first, create your message
use Email::MIME;
my $message = Email::MIME->create(
header_str => [
From => 'you#example.com',
To => 'friend#example.com',
Subject => 'Happy birthday!',
],
attributes => {
encoding => 'quoted-printable',
charset => 'ISO-8859-1',
},
body_str => "Happy birthday to you!\n",
);
# send the message
use Email::Sender::Simple qw(sendmail);
sendmail($message);
Of course, I could dump the %INC but the output includes a ton of modules.
Ideally, I would like to load Email::MIME and Email::Sender::Simple and have this work.

I would recommend using Mail::Sender, you can install it in ActivePerl using ppm install Mail::Sender, in Linux install it with
sudo apt-get install libmail-sender-perl
This module is very versatile, supports multiple attachments, inlining, SMTP password authentication (even including NTLM - necessary to talk to Exchange servers).
This is an example to send HTML mail using Mail::Sender:
use Mail::Sender;
my $sender = new Mail::Sender({
# provider may require using port 587:
smtp => "smtp.example.com",
# auth parameters below are optional
# and depend on provider requirements
auth => "LOGIN",
authid => $username,
authpwd => $password,
from => "myself#example.com",
});
$sender->Open({
to => "recipient#example.com",
cc => "anotherguy#example.com",
subject => "Subject line",
ctype => "text/html",
encoding => "7bit",
}) or die ($Mail::Sender::Error, "\n");
my $html = "<html><body>Test HTML content</body></html>";
$sender->SendEx($html)
or die ($Mail::Sender::Error, "\n");
$sender->Close();
print "Test message has been sent\n";

Email::Sender::Simple and just Email::Sender are two different modules, so you have to do this:
From cmd:
C:\>cpan
C:\>install Email::Sender::Simple
That's all.

Related

How to implement https support for Perl's HTTP::DAV

I am trying to access a remote server via the WebDav protocol, and more specifically Perl's HTTP::DAV module.
According to its documentation the coupling to a remote directory takes place in the following manner:
use HTTP::DAV;
$d = HTTP::DAV->new();
$url = "http://host.org:8080/dav/";
$d->credentials(
-user => "pcollins",
-pass => "mypass",
-url => $url,
-realm => "DAV Realm"
);
$d->open( -url => $url )
or die("Couldn't open $url: " .$d->message . "\n");
I created a local webdav directory and can access it flawlessly over the http protocol.
According to HTTP::DAV's documentation, there should be and https support as well using the Crypt::SSLeay module.
The Crypt::SSLeay's documention offers us the following synopsys using inside the LWP::UserAgent module, thus providing for us web resource access over the https protocol:
use Net::SSL;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new(
ssl_opts => { verify_hostname => 0 },
);
my $response = $ua->get('https://www.example.com/');
print $response->content, "\n";
My question is:
How can I combine the HTTP::DAV and Crypt::SSLeay modules in order to have web resource access over the WebDav and https protocols?
Something like the following:
use HTTP::DAV;
$d = HTTP::DAV->new();
$url = "https://host.org:8080/dav/";
#...
This is untested, but from skimming the documentation, this should work:
$d->get_user_agent->ssl_opts( verify_hostname => 0 );
The documentation for HTTP::DAV says:
get_user_agent
Returns the clients' working HTTP::DAV::UserAgent object.
You may want to interact with the HTTP::DAV::UserAgent object to modify request headers or provide advanced authentication procedures.
HTTP::DAV::UserAgent isn't documented, but its source code shows it's a subclass of LWP::UserAgent. The documentation for LWP::UserAgent mentions the ssl_opts method for setting SSL options for the user agent object.

Perl - How to request HTTPS website

I am trying to access a HTTPS website, but it gives me error. I tried with this scripts:
Script 1:
use strict;
use warnings;
use LWP::UserAgent;
my $B = new LWP::UserAgent (agent => 'Mozilla/5.0', cookie_jar =>{});
my $GET = $B->get('https://moz.com')->content;
print $GET;
Script 2:
use strict;
use warnings;
use LWP::UserAgent;
use Mozilla::CA;
my $B = new LWP::UserAgent (agent => 'Mozilla/5.0', cookie_jar =>{});
$B->ssl_opts( SSL_ca_file => Mozilla::CA::SSL_ca_file() );
$B->ssl_opts( verify_hostname => 1 );
my $GET = $B->get('https://moz.com')->content;
print $GET;
I get this error with both:
Can't connect to moz.com:443
LWP::Protocol::https::Socket: SSL connect attempt failed with unknown error error:00000000:lib(0):func(0):reason(0) at C:/Perl/site/lib/LWP/Protocol/http.pm line 47.
I'm using ActivePerl 5.16.1 Build 1601 on Windows 7 Ultimate.
Any idea how to access a HTTPS website using Perl?
https calls will fail if you do not have the LWP::Protocol::https module installed.

IO/Pty error when compiling PERL script

It has been awhile since I have done any PERL programming and I am working on a project where I need to connect to a remote server and get a listing of files. The script is being developed on a Windows 2012 server using ActivePerl.
This is the script that I have put together, from other examples -
#! /usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use autodie;
use Net::SFTP::Foreign;
# US Server Configuration
use constant {
HOST => "samplehost",
REMOTE_DIR => "sample/remote",
LOCAL_DIR => "sample/local",
PORT => "3235",
USER_NAME => "username",
PASSWORD => "D%password",
DEBUG => "0",
};
my $stfp = Net::SFTP::Foreign->new (
HOST,
timeout => 240,
user => USER_NAME,
password => PASSWORD,
port => PORT,
autodie => 1,
);
#
# List remote directory contents
#
my $remotefiles;
$remotefiles = $stfp->ls(REMOTE_DIR);
#
# Loop through remote files and print each filename
#
foreach ($remotefiles){
my $file = $_;
my $filename = $file->{filename};
if($filename ne "." && $filename ne ".."){
print"the filename is $filename";
}
}
I am receiving an error when I compile it -
C:\development>perl ConvertPDFs.pl
password authentication not available, IO::Pty is not installed or failed to loa
d: Can't locate IO/Pty.pm in #INC (#INC contains: C:/Perl64/site/lib C:/Perl64/l
ib .) at C:/Perl64/site/lib/Net/SFTP/Foreign/Backend/Unix.pm line 256.
at ConvertPDFs.pl line 20.
Not sure what the problem is.
Pseudo-ttys are a unix construct. If a module requires IO::Pty, it won't run on Windows (though maybe on cygwin). On the plus side, the error message makes it sound like Net::SFTP::Foreign only uses IO::Pty for password-based authentication, so you could avoid the problem by switching to key-based authentication.

SNMP v3 with NET::SNMP working, but snmpwalk/snmpget not?

I have the following (working) perl script:
use Net::SNMP;
# create session to the host
my ($session, $error) = Net::SNMP->session(
-hostname => $hostname,
-version => 'snmpv3',
-username => 'my_user_name',
-authkey => 'my_authkey',#actually, here stands the real authkey as configured on the switch
-privkey => 'my_privkey',#same as on switch
-authprotocol => 'sha',
-privProtocol => 'des'
);
if (!defined($session)) {
print $error . "\n";
last;
}
# retrieve a table from the remote agent
my $result = $session->get_table(
-baseoid => $MAC_OID
);
if (!defined($result)) {
print $session->error . "\n";
$session->close;
last;
}
#print out the result of the snmp query
#....
Now I wanted to use snmpwalk or snmpget with the same keys. For that, I created a snmp.conf file in .snmp of my home directory with the following content:
defSecurityName my_user_name
defContext ""
defAuthType SHA
defSecurityLevel authPriv
defAuthPassphrase my_auth_key here
defVersion 3
defPrivPassphrase my_privkey here
defPrivType DES
As I see it, I use the same credentials in the script and for snmpget. Why do I get snmpget: Authentication failure (incorrect password, community or key) ?
That depends on the version of snmpget and snmpset you use. When I tested an older version of net-snmp against my C# based SNMP agent http://sharpsnmplib.codeplex.com I noticed that for SHA authen mode + DES privacy mode a bug prevented the net-snmp command line tools from generating the correct message bytes (the encryption is wrong so that no agent can decrypt it).
My suggestion is that you try to use Net::SNMP instead, as like you found out, it is not affected by the same bug.
Your problem is that you're using an authentication key for Net::SNMP and a password for the command-line net-snmp tools. Based on your Net::SNMP usage you're actually using 'localized' keys. Which means the right tokens for your snmp.conf file are:
defAuthLocalizedKey 0xHEXSTRING
defPrivLocalizedKey 0xHEXSTRING
See the snmp.conf manual page for further details.

Perl - Having trouble using MIME::Lite with authentication to send emails

I'm trying to send emails out using MIME::Lite with authentication.
Here's the code snippet I am trying:
#!/usr/bin/perl
use strict;
use DBI;
use lib '/theannealing.com/perl/';
use MIME::Lite;
use MIME::Base64;
use Authen::SASL;
my $recipient = 'recipient#email.com';
my $mailman = 'sender#email.com';
my $cc_recipient = 'ccrecipient#email.com';
my $subject = 'Subject';
my $email_message = "Message";
my ($user,$pass) = ("username","password");
MIME::Lite->send('smtp','smtp.server:port',AuthUser=>$user, AuthPass=>$pass);
my $email = new MIME::Lite(From => $mailman,To => $recipient,Cc => $cc_recipient,Subject => $subject,Data => "Data",Type => "multipart/mixed");
$email->attach(Type => 'TEXT', Data => "$email_message");
$email->send();
When I execute the script, I get this error message:
Cannot find a SASL Connection library at /usr/lib/perl5/5.8.8/Net/SMTP.pm line 143
I tried searching the error message and couldn't find any worth-while explanations and/or solutions to the problem with relevance to usage with MIME::Lite
Does anyone know what's wrong or what's producing that error message?
UPDATE
Emailing via php using the mail() function works fine from a web browser, but does not work from the command line
You need to reinstall Authen::SASL - it was installed incorrectly. Do this as root from command line:
cpan GBARR/Authen-SASL-2.15.tar.gz