Link to home
Start Free TrialLog in
Avatar of kch011099
kch011099

asked on

E-mail script using address book

I'm trying to read the e-mail addresses from a text file as if it were an address book. My e-mail script worked fine before, but since I've tried to add this feature, it won't work. Can any one help?
---------------------------------------
text file:

name@name.com
address@address.com
---------------------------------------
perl script:

#!/usr/bin/perl

    %Config = ('to','','subject','','from','','realname','','content','');


    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        @pairs = split(/&/, $ENV{'QUERY_STRING'});
    }
    elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 
        @pairs = split(/&/, $buffer);
    }
    else {
        &error('request_method');
    }

    foreach $pair (@pairs) {

        local($name, $value) = split(/=/, $pair);
 
        $name =~ tr/+/ /;
        $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

        $value =~ s/<!--(.|\n)*-->//g;

        if (defined($Config{$name})) {
            $Config{$name} = $value;
        }
        else {
            if ($Form{$name} && $value) {
                $Form{$name} = "$Form{$name}, $value";
            }
            elsif ($value) {
                push(@Field_Order,$name);
                $Form{$name} = $value;
            }
        }
    }

    $Config{'required'} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
    $Config{'required'} =~ s/(\s+)?\n+(\s+)?//g;
    $Config{'env_report'} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
    $Config{'env_report'} =~ s/(\s+)?\n+(\s+)?//g;
    $Config{'print_config'} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
    $Config{'print_config'} =~ s/(\s+)?\n+(\s+)?//g;

    @Required = split(/,/,$Config{'required'});
    @Env_Report = split(/,/,$Config{'env_report'});
    @Print_Config = split(/,/,$Config{'print_config'});


$mailprog = '/usr/sbin/sendmail';


# Send E-Mail
   
    open(MAIL, "|$mailprog -t");
    open(NOTIFY, "<notify.txt");

    @list = <NOTIFY>;
    foreach $item (@list){
    print MAIL "To: $item\n";
    print MAIL "From: name\@somewhere.com\n";

    if ($Config{'subject'}) { print MAIL "Subject: $Config{'subject'}\n\n" }
    else                    { print MAIL "Subject: No Subject\n\n" }

    print MAIL "$Config{'content'}\n";

    close (MAIL);
    close (NOTIFY);

print "Content-type:text/html\n\n";
print "All messages sent.";
Avatar of thoellri
thoellri

Have you tried rearranging the mail sending part?

                           # Send E-Mail
                           
                           open(NOTIFY, "<notify.txt");
                           @list = <NOTIFY>;
                           close(NOTIFY);
                           foreach $item (@list){
                             open(MAIL, "|$mailprog -t");
                             print MAIL "To: $item\n";
                             print MAIL "From: name\@somewhere.com\n";

                             if ($Config{'subject'}) {
                               print MAIL "Subject: $Config{'subject'}\n\n"
                             } else {
                               print MAIL "Subject: No Subject\n\n";
                             }
                             print MAIL "$Config{'content'}\n";
                             close (MAIL);
                           }
                           print "Content-type:text/html\n\n";
                           print "All messages sent.";


Hope this helps
  Tobias
i think you need to escape the individual $item's before you  send them (the way you use name\@somewhere.com)

so insert this line right after

foreach $item (@list){
  $item=~ s/@/\\\@/;
.....
Why would you need to escape them? I don't see an eval anywhere?

Avatar of kch011099

ASKER

For some reason, it's not reading the notify file. Anyone got any ideas?
I added a print "$item"; command in the foreach loop, but it didn't print anything.
check the permission for the notify.txt file

heres the code i used to test...

#!/usr/local/bin/perl

open(IN,"./mail.txt") || die $!;
@mails=<IN>;
close(IN);

foreach $to (@mails){
  $to=~ s/@/\\\@/;
  print $to,"\n";
}

==========mail.txt==========
mehere@hotmail.com
jitu7125@yahoo.com

===========
ASKER CERTIFIED SOLUTION
Avatar of thoellri
thoellri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I fixed my problem. I just moved the txt file out of the cgi-bin and into a new folder. Now it works, thanks for your help. I will still give out the points.