Sending email using SMTP in Perl
use Net::SMTP; $smtpconn = Net::SMTP->new('mail.example.com', hello => 'mycomputer.example.com'); $smtpconn->mail('mani@w3mentor.com'); $smtpconn->to('madhu@w3mentor.com'); $smtpconn->data(); $smtpconn->datasend("Hello,\n e-mail sample from me!\n"); $smtpconn->dataend(); $smtpconn->quit;
Display email subject from inbox using Mail::Box
To print subject lines, use the get() method.
#!/usr/bin/perl -w use strict; use Mail::Box::POP3; my $folder = Mail::Box::POP3->new(server_name => 'mail.example.com', password => 'password', username => 'user@example.com') or die "$!"; my @messages = $folder->messages; foreach my $message (@messages) { print "Subject: ", $message->get('Subject') or "<no subject>"; print "\n"; }
Display individual email from inbox using Perl Mail::Box
You can retrieve individual messages by calling the message by its index.
#!/usr/bin/perl -w use strict; use Mail::Box::POP3; my $folder = Mail::Box::POP3->new(server_name => 'mail.example.com', password => 'password', username => 'user@example.com') or die "$!"; $folder->message(2)->print;
Using Mail::Box to display the number of emails in Perl
Mail::Box is another package, available from CPAN, for working with e-mail in Perl. In many ways, Mail::Box provides a more elegant solution to working with e-mail in Perl than the Net::POP3 package.
#!/usr/bin/perl -w use strict; use Mail::Box::POP3; my $folder = Mail::Box::POP3->new(server_name => 'mail.example.com', password => 'password', username => 'user@example.com') or die "$!"; my $nummsgs = $folder->messages; print "There are $nummsgs messages waiting\n";
Delete emails in POP3 inbox using Perl
To mark a message for deletion, simply call the delete() method with the number of the message as the argument. When we close the connection using the quit() method, the messages that have been marked as deleted will be purged from the remote mail spool.
#!/usr/bin/perl -w use Net::POP3; use strict; my $username = "user\@example.com"; my $password = "password"; my $pop3conn = Net::POP3->new("mail.example.com", timeout => 30); my $nummsgs = $pop3conn->login($username,$password); $pop3conn->delete(1); //ID to be deleted $pop3conn->quit(); // complete deletion
Get number of email messages and the size of mailbox using Perl POP3
The popstat() method gives the number of messages along with the size of the mailbox.
#!/usr/bin/perl -w use Net::POP3; use strict; my $username = "user\@example.com"; my $password = "password"; my $pop3conn = Net::POP3->new("mail.example.com", timeout => 30); my $nummsgs = $pop3conn->login($username,$password); ($messages,$size) = $pop3conn->popstat(); print "There are $messages messages totaling $size bytes\n"; $pop3conn->quit();
Get Email subject lines using Perl POP3
#!/usr/bin/perl -w use Net::POP3; use strict; my $username = "user\@example.com"; my $password = "password"; my $pop3conn = Net::POP3->new("mail.example.com", timeout => 30); my $nummsgs = $pop3conn->login($username,$password); for (my $i=1;$i<=$nummsgs;$i++) { my $message = $pop3conn->top($i); print "Message $i: "; print grep (/^Subject:/, @{$message}); } $pop3conn->quit();
Print Email Message IDs using Perl POP3
#!/usr/bin/perl -w use Net::POP3; use strict; my $username = "user\@example.com"; my $password = "password"; my $pop3conn = Net::POP3->new("mail.example.com", timeout => 30); if ($pop3conn->login($username,$password) > 0) { my $messages = $pop3conn->uidl(); foreach my $msg (keys %{$messages}) { print "Message $msg is ID: $messages->{$msg}\n"; } } $pop3conn->quit();
Get and display POP3 Messages in Perl
We can retrieve the email message with the get() method, in Perl, called with the message number as an argument.
#!/usr/bin/perl -w use strict; use Net::POP3; my $username = "user\@example.com"; my $password = "password"; my $pop3conn = Net::POP3->new("mail.example.com", timeout => 30); my $nummsgs = $pop3conn->login($username,$password); if ($nummsgs > 0) { print "There are $nummsgs messages waiting\n"; my $message = $pop3conn->get(12); print "@{$message}"; } $pop3conn->quit();
List POP3 Messages from mail server in Perl
#!/usr/bin/perl -w use strict; use Net::POP3; my $username = "user\@example.com"; my $password = "password"; my $pop3conn = Net::POP3->new("mail.example.com", timeout => 30); if ($pop3conn->login($username,$password) > 0) { print "You've Got Mail!\n"; my $messages = $pop3conn->list(); foreach my $msg (keys %{$messages}) { print "Message $msg is $messages->{$msg} bytes\n"; } } $pop3conn->quit();
Using LWP and HTML::FormatText to retrieve text from a page in Perl
!/usr/bin/perl -w
use strict;
use HTML::TreeBuilder;
use HTML::FormatText;
use LWP::Simple;
my $webpage = get(“http://www.w3mentor.com”);
my $htmltree = HTML::TreeBuilder->new->parse($webpage);
my $output = HTML::FormatText->new();
print $output->format($htmltree);
Post a form programatically using Perl LWP
#!/usr/bin/perl -w use LWP; use strict; my $browser = LWP::UserAgent->new(agent => 'Firefox'); $result = $browser->post('http://www.w3mentor.com/form.cgi', [ 'name' => 'mani', 'email' => 'mani@w3mentor.com', 'zip' => '54481' ]); print $result->content;
Retrieve a web page by setting user agent manually using Perl LWP
#!/usr/bin/perl -w use LWP; use strict; my $browser = LWP::UserAgent->new(agent => 'Mozilla Firefox 3.2'); my $result = $browser->get("http://www.w3mentor.com"); die "An error occurred: ", $result->status_line( ) unless $result->is_success; #Do something more meaningful with the content than this! print $result->content;
Retrieving a Web Page with get() in Perl LWP
The get() function from LWP::Simple enables you to quickly and easily retrieve a web page using the GET method.
#!/usr/bin/perl -w use LWP::Simple; use strict; my $webpage = get("http://www.w3mentor.com/"); if (($webpage) && (grep {/tutorials/} $webpage)) { print "I found the text\n"; }
Using the mirror() Function with Perl LWP
The mirror() function works in much the same was as the getstore() function, but also includes a check to compare the modification time of the local file and the modification time of the remote resource, based on the If-Modified-Since response header.
#!/usr/bin/perl -w use LWP::Simple; use strict; my $url = "http://www.braingia.org/"; my $file = "/tmp/braingiamirrorweb"; my $status = mirror($url,$file); die "Cannot retrieve $url" unless is_success($status);
Using is_success() with getstore() in Perl LWP
The getstore() function takes the output of a web page and automatically stores it in an external file. The getstore() function also returns the status of the GET method and sets is_success() if the status is in the 200 range. It sets is_error() if the status is in the 400 or 500 range.
#!/usr/bin/perl -w use LWP::Simple; use strict; my $status = getstore("http://www.braingia.org/","/tmp/w3file.txt"); unless (is_success($status)) { die "Couldn't retrieve page: $status"; } open (PAGE, "/tmp/w3file.txt") or die "$!"; while (<PAGE>) { print(); } close(PAGE);
Retrieve a webpage using LWP in Perl
#!/usr/bin/perl -w use LWP; use strict; my $browser = LWP::UserAgent->new(agent => 'Mozilla Firefox 3.2'); my $result = $browser->get("http://www.w3mentor.com"); die "An error occurred: ", $result->status_line( ) unless \ $result->is_success; #Do something more meaningful with the content than this! print $result->content;
Iterate over environment variables in Perl
foreach $key (keys %ENV) { print "Environment key $key is $ENV{$key}\n"; }
Check for Acceptable File Types during upload in Perl
When a file with a Content-Type that isn’t text/html is uploaded to this CGI script, its output indicates that only HTML types are allowed.
#!/usr/bin/perl use strict; use CGI qw/:standard/; my $q = new CGI; my $filename = $q->param('uploaded_file'); my $contenttype = $q->uploadInfo($filename)->{'Content-Type'}; print header; print start_html; if ($contenttype !~ /^text\/html$/) { print "Only HTML is allowed<P>"; print end_html; exit; } else { print "Type is $contenttype<P>"; } print end_html;
Display the Content-Type of an Uploaded File in Perl
#!/usr/bin/perl use strict; use CGI qw/:standard/; my $q = new CGI; my $filename = $q->param('uploaded_file'); my $contenttype = $q->uploadInfo($filename)->{'Content-Type'}; print header; print start_html; print "Type is $contenttype<P>"; print end_html;
