CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Using script arguments with Net::SMTP

 



alejit0
New User

Feb 26, 2008, 6:21 AM

Post #1 of 2 (657 views)
Using script arguments with Net::SMTP Can't Post

Hello. I am trying to send email notification from script. I get email address through --mail argument of script, using Getopt::Long. After this I change variable that contain email address adding escape character before @, so it looks like "somename\@domain.org". After this I try to use this variable in subroutine and send mail. My sub looks so:

Code
sub SendMail { 
my ($smtp, $data, $email);
$email = shift @_;
$data = shift @_;
$smtp = Net::SMTP->new('mail.spb.domain.org');

$smtp->mail($email);
$smtp->to($email);

$smtp->data();
$smtp->datasend("To: Somebody\n");
$smtp->datasend("\n");
$smtp->datasend($data);
$smtp->dataend();



$smtp->quit;

}

and I call this sub so: SendMail($mail, $data);
But I am can't send email using my variable with email address.
When I try to replace variable that I getting like script argument with simple variable that store email address, like this

Code
sub SendMail { 
my ($smtp, $data, $email);
#$email = shift @_;
$email = "somebody\@domain.org"; #!!!!!!!!!!!!!!!!!
$data = shift @_;
$smtp = Net::SMTP->new('mail.spb.domain.org');

$smtp->mail($email);
$smtp->to($email);

$smtp->data();
$smtp->datasend("To: Somebody\n");
$smtp->datasend("\n");
$smtp->datasend($data);
$smtp->dataend();



$smtp->quit;

It works well! I think there is something like special characters at the end of the string where I get mail through command line argument, but I do not know how to delete this. I am trying to delete all space characters with $email =~ s/\s+$//; but it is not useful.
Thank you.


eWish
User

Feb 26, 2008, 5:40 PM

Post #2 of 2 (653 views)
Re: [alejit0] Using script arguments with Net::SMTP [In reply to] Can't Post

I hope I understand your problem correctly.

You can use single quotes around the email address and then you will not have to escape the @ symbol.

Code
my $email = 'somebody@domain.org';


When you want to send a sub routine arguments than the sub routine get's the arguments into the @_ array. The order in which you pass the arguments to a subroutine matters.

Code
&some_sub($email, $SMTP, $data); 

sub some_sub {

# These must be listed in the same order they were sent in.
my ($email, $SMTP, $data) = @_;
...rest of code here...
}


Only way to get around this is to use 'named arguments';

--Kevin

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives