nujersedvl has asked for the wisdom of the Perl Monks concerning the following question:

this is a script i have that sends an email to a list of people. what i need to know is the easiest and best way to grep the users IP address and insert it into the headers of the outgoing email. thanks for any help.
#!/usr/local/bin/perl use CGI; $foo = new CGI; $recips = $foo->param('recipient'); $realname = $foo->param('realname'); $realemail = $foo->param('realemail'); $mailprog = '/usr/sbin/sendmail'; @recipients = split("\, ", $recips); if(defined ($val = $foo->cookie(-name => "sent-mail"))) { print $foo->header(); print " <body bgcolor=\"#FFCC00\"> <p align=\"center\"><big><big><strong><font face=\"Arial\">Sorry $real +name, emails can only be sent once.</font></strong></big></big></p> </body>"; } else { foreach $person (@recipients) { ($pname, $pemail) = split("\<", $person); $pemail =~ s/\>.*//; $pname =~ s/\s+//; open(MAIL,"|$mailprog -t"); print MAIL "To: $pemail\n"; print MAIL "From: $realemail ($realname)\n"; print MAIL "Subject: $pname, interesting site\n\n"; print MAIL " $pname message....... $realname"; close (MAIL); } open(FILE, ">> addresses.txt"); @addresses = <FILE>; foreach $person (@recipients) { ($pname, $pemail) = split("\<", $person); $pemail =~ s/\>//; if($pemail !=~ /\s+\,\s+/) { print FILE "$pemail\n"; }} close(FILE); print $foo->header(); $cookie = $foo->cookie('-name' => "sent-mail", '-value' => "sent"); print " <body bgcolor=\"#FFCC00\"> <script language=javascript> alert(\"Thank you\"); </script> </body>"; }

Replies are listed 'Best First'.
Re: IP and mail header
by kschwab (Vicar) on Mar 24, 2001 at 21:14 UTC
    Sounds like you want get the ip address of the browser that is invoking the cgi script ?

    This doesn't really have much to do with perl, but I believe that most web servers put the remote browser's hostname and ip address in the REMOTE_HOST and REMOTE_ADDR environment variables:

    my $rhost = $ENV{"REMOTE_HOST"} || "unknown"; my $raddr = $ENV{"REMOTE_ADDR"} || "unknown"; # use mailer-friendly "x headers" print MAIL "X-REMOTE_HOST: $rhost\n"; print MAIL "X-REMOTE_ADDR: $raddr\n";