PDA

View Full Version : Help w/ Perl code and MailTools to manipulate emails



amaterasu2915
12-07-2004, 07:53 PM
I believe I posted long ago w/ a technical question about operating a Linux server. People were really, really helpful (as I am a total Linux newbie), and I hope you can help me once again.

I am building a development server in preparation for the creation of a web site. The server will receive emails and identify the sender and recipient email addresses in each message, storing them in a database among other things.

I have (with the help of this community) arranged to have emails run through a Perl script by manipulating virtusertable and aliases (a titanic accomplishment for me.) I have installed the Perl module MailTools, and can use Mail::Internet to identify email addresses in the following complete form, for example:

Bob Smith <bob@att.net>

But I need the program to isolate the e-mail address alone, in other words:

bob@att.net

I have fought with my (very poor) Perl programming skills, but to no avail. The code (which I have been using) is something like this:

#! /usr/bin/perl
# /usr/local/bin/remailer.pl

use Mail::Internet;
use Mail::Address;

$mail = Mail::Internet->new( \*STDIN );

$sender=$mail->get('Reply-To') ||
$mail->get('From');

$sender=(Mail::Address->parse($sender))[0];

open (FH, '>/tmp/output.txt');
print FH @to;
print FH @from;
print FH $sender;
close (FH);

This code bit results in an error, saying: can't call method "address" on undefined value. The address method (I believe, but I could be wrong) should isolate the email address. It's meant to be a very simple test code: it receives an email text through standard input, identifies the full email addresses for recipient and sender, and then creates a file called output.txt that lists those full email addresses, as well as the email address of the sender, stripped of comments, name or greater and lesser signs.

As you can see, I am fairly new to programming, Unix and Linux. Any recommendations as to books, web sites or resources that would better answer these questions would be welcome. Some code ideas would be great too. I have searched hard for MailTools resources (cpan.org, googling MailTools, Mail::Internet, so on) but to no avail.

Thanks so much for helping an enthusiastic but often clueless Linux newbie. Please let me know if such questions are not relevant to this community too.

wildjokerdesign
12-07-2004, 10:06 PM
Just about any question is relevant when it comes to trying to work with mail it seems. :) I have to say I'm not sure at all but one guess would be that $sender has no value in it. Have you tried to do a check on $sender to see if it was set before calling the address method. Maybe an "if" statment or simply stopping the script and printing out the value of $sender at that point to see what it is. It has been quite awhile since I have worked with perl but that is what comes to mind.

jalal
12-08-2004, 01:44 AM
Could you show us the exact error?
I don't see any 'address method' in the script you are using.

You can also add the lines:

use warnings;
use strict;
at the start of the script to catch any warnings and maybe get further information.

HTH

SJP
12-08-2004, 11:40 PM
A couple things. I recall that FH has some special use and is not to be used as a file handle name. The other:

$sender=$mail->get('Reply-To') ||
$mail->get('From');

This is not a valid assignment. I see what you are trying to accomplish. Maybe this would be acceptable?

$sender=$mail->get('Reply-To') unless ($sender=$mail->get('From'));

The way you had it $sender could go unitialized if there was no reply-to. Other than that I have no experience working with those modules. IMO and experience you don't need them.

SJP