How do I parse an email to extract text (password) and store in a variable
I have a system that sends an email to a user with a temporary password. I am using the following script to output the email but I am looking for a way to extract the password and store the value in a varia开发者_StackOverflowble. The body of the email will have the following text "Your temporary password is " I would like to parse the email body and extract . I assume that I could use a parse module to search for "Your temporary password is" and then store the next token to a variable but my knowledge of perl is not that great. Can anyone point me in the right direction?
#!/usr/bin/perl
use Net::POP3;
my $username="user";
my $password="password";
my $host="192.168.17.50";
# Constructors
my $pop = Net::POP3->new($host)||
die "Problem calling Net::PoP->new\n$!\n";
# $pop = Net::POP3->new($host, Timeout => 60);
if ($pop->login($username, $password) > 0) {
my $msgnums = $pop->list; # hashref of msgnum => size
foreach my $msgnum (keys %$msgnums) {
my $msg = $pop->get($msgnum);
my $password = extract_password($msg)||
die "Problem with extract_password\n$!\n";
print $password;
$pop->delete($msgnum);
}
}
$pop->quit;
Here's a function that receives the message text and returns the password, or undef
if it's not found.
sub extract_password {
my $text = shift;
if ($text =~ /Your temporary password is (\S+)/) {
return $1;
}
else {
return undef;
}
}
Then you can say my $password = extract_password($msg_text)
.
精彩评论