[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

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.";
0
kch011099
Asked:
kch011099
  • 3
  • 3
  • 2
1 Solution
 
thoellriCommented:
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
0
 
maneshrCommented:
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/@/\\\@/;
.....
0
 
thoellriCommented:
Why would you need to escape them? I don't see an eval anywhere?

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kch011099Author Commented:
For some reason, it's not reading the notify file. Anyone got any ideas?
0
 
kch011099Author Commented:
I added a print "$item"; command in the foreach loop, but it didn't print anything.
0
 
maneshrCommented:
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

===========
0
 
thoellriCommented:
what does:

 open(NOTIFY, "<notify.txt") || die "Can't open address book - $!";

in the above script give you?

Tobias
0
 
kch011099Author Commented:
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now