Email newslinks at given times

I have a service at our site where the readers can get todays headlines mailed to them. You may try it at:
http://www.gp.se/dagl/frames/cgi/grab/grab.html
It's in Swedish, so you might have to figure it out.
What i really would like is a script that allowed the readers to self-register to get these headlines sent to them every day, much in the manner one can at my newsletter service:
http://www.gp.se/cgi-bin/dumtida.cgi
So: first you register with your e-mail address, then you get the headlines sent to you, say at 9 am every morning.
Should be rather simple, but it's over my head.
400 points for a working script.
Algabatz
ps Script to date at:
http://www.gp.se/dagl/frames/cgi/tony.txt
LVL 1
algabatzAsked:
Who is Participating?
 
guadalupeConnect With a Mentor Commented:
Ok here is part one.  It takes a form submission, gets the email checks if the email is already registered and then put it in if not and returns a sucess page.  The second part will take a little longer but you can play with this in the meantime and polish it:


#!/usr/local/bin/perl


#Parse input
&parseInput;

#Set variables

$email_list = "emails.txt";

#Transfer email to scaler for use in regex
$email = $fields{'email'};

#Check if email exists
open (LIST, "$email_list");

while (<LIST>)
{
      if (/$email/)
      {
            &email_exists;
            exit(0);
      }
}

close(LIST);


#Add to list

open (LIST, ">>$email_list");

print LIST $email;

close(LIST);

&sucess;



sub sucess()
{
#print header to browser
print <<EOF;
Content-type: text/html\n\n
<HTML>
<HEAD>
<TITLE> Success </TITLE>
<HEAD>
<BODY>
Success
<PRE>
EOF


}


sub email_exists()
{

#print header to browser
print <<EOF;
Content-type: text/html\n\n
<HTML>
<HEAD>
<TITLE> Email Exists </TITLE>
<HEAD>
<BODY>
Email exists!
<PRE>
EOF
}




#---------------------------------Parse Input---------------------------------------#
#                                                                                                                              #
#                                    Standard Form/Query String Parsing                                          #
#-----------------------------------------------------------------------------------#
sub parseInput()
{


      #Parse form variables
      if ($ENV{'REQUEST_METHOD'} eq "POST")
      {
            read(STDIN, $temp, $ENV{'CONTENT_LENGTH'});
      }

      elsif ($ENV{'REQUEST_METHOD'} eq "GET" )
      {
            $temp = $ENV{'QUERY_STRING'};
      }

      @pairs=split(/&/,$temp);
      
      foreach $item(@pairs)
      {
            ($key,$content)=split (/=/,$item,2);
            $content=~tr/+/ /;
            $content=~ s/%(..)/pack("c",hex($1))/ge;
            $fields{$key}=$content;
      }

}
0
 
guadalupeCommented:
Question, how doy you want to store the list or registrees?  Or maybe the real question is what options are available?  A txt list is always an option but could quickly become very large and inefficient especially if some day you want to do something else with it other than just line by line get the email and send out the headlines.  Do you have any database services installed?
0
 
algabatzAuthor Commented:
Edited text of question.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
algabatzAuthor Commented:
A txt-file is OK for now. Later i might incorporating it in the supscriptions database, but for a test period a txtfile would be sufficient.
/A
0
 
guadalupeCommented:
Is this Unix or NT?  Do yoiu have access to cron or can you get an event into cron that will run every x minutes or at a certain time every day?  This obiously for the mailer portion of the script...
0
 
guadalupeCommented:
Oh yeah one more question.  Where will the headlines be?  Will you arlready have a txt doc which can be read and placed in the body of a mail or what?
0
 
algabatzAuthor Commented:
Yes, the present script (see link above) picks up the following files:
http://www.gp.se/gpmobil/rubinrfr.htm
http://www.gp.se/gpmobil/rubutrfr.htm
http://www.gp.se/gpmobil/rubekofr.htm
http://www.gp.se/gpmobil/rubvsvfr.htm
Which are database produced.
/Al
0
 
algabatzAuthor Commented:
Regarding Cron, yes i have access, and it's Unix.
0
 
guadalupeCommented:
OK I can work with that just one more thing do you want to send the the html content as an attached or as an HTML formatted mail?
0
 
algabatzAuthor Commented:
HTML formatted. It would be fine if it could lure the HTML-stripping functions at f.i. Hotmail so that it could be presented correctly there too.
I really appreciate this.
/Al
0
 
guadalupeCommented:
Ok i was kecking out the page and it has to be rewritten if you want to sen it as an html mail... let me know and I'll show you how.
0
 
guadalupeCommented:
OK I need a little more info and were pretty much done.

The 4 links you sent.  Are they all to be sent as one email or as multiple emails?
0
 
algabatzAuthor Commented:
Excellent. I've raised the points to 500. You've earned them but i'll keep it open so that others might comment. I'd like a form, like the page at:
http://www.gp.se/dagl/frames/cgi/grab/grab.html
where one can choose 1,2,3,4 or all. But always a single e-mail.
/Al
0
 
guadalupeCommented:
Okay to clarify do you want to send the contents of the page grab.htm as the email?
0
 
algabatzAuthor Commented:
No, i want the user to be able to choose the content in a form, like at grab.html, choose the content of the e-mail.

Like this: The user registers his e-mail address and then comes to a form where he can choose which headlines

http://www.gp.se/gpmobil/rubinrfr.htm 
http://www.gp.se/gpmobil/rubutrfr.htm 
http://www.gp.se/gpmobil/rubekofr.htm 
http://www.gp.se/gpmobil/rubvsvfr.htm 

(or all of the above),
he wants to be mailed to him at certain intervals. He should also be able to come back to terminate his registration if he so wishes.
If this is too complicated i'd be satisfied with the registration without the headline-choosing form. That is: that all the headlines will be sent to all the registered users at a certain time every day.
/Al
0
 
guadalupeCommented:
Well lets start with a simple one, get it working and then well add on... I should have a simple example in an hour or two... That is if things stay calm here in the office.
0
 
algabatzAuthor Commented:
Fine. You may try entering your e-mail at
http://www.gp.se/dagl/frames/cgi/grab/grab.html 
to see what i mean.

/Al
0
 
guadalupeCommented:
OK I've got the basic working model.  The part about choosing headlines, and unsubscribing will be esay as soon as we get this running, but this could be tricky depending on the modules you have installed.  The script requires two moduals LWP::Simple and Net::SMTP.

If these modules are installed it shoul;d be simple if not we can porbably get around it pretty easily so here is the script:

#!/usr/local/bin/perl

BEGIN { push(@INC,"./"); }


use LWP::Simple;
use Net::SMTP;

&set_vars;

&get_headlines;

&read_email_list;



sub set_vars()
{
            #Set email list file use full path if not in same dir
            $email_list = "emails.txt";

            #Set error log (include full path if desired
            $LOG_FILE = "errors_mailer.txt";

            #Set mail host name!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Very important
            $hostname = 'xxx';

            #Set the from/return address
            $from_address = 'xxx';

            #Name that will appear in From:
            $from_name = "Headlines";

            #Subject of mail
            $subject = "Headlines";



            #Both the headre and footer I have included is the bare minimum.
            #As you can see its the skeleton for an HTML page
            #You can add if you like just becare full no to subtract.
            #To be safe, if you add images make there URL a complete URI
            #EX:  http://www.yoursite.com/images/yourgif.gif

            #HTML that will appear above the links in the mail
            $header = <<EOF;
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
            <HTML>
            <HEAD>
            <TITLE> New Document </TITLE>

            </HEAD>

            <BODY>
EOF


            #HTML that will appear below the links in the mail
            $footer = <<EOF;
            </BODY>
            </HTML>
EOF

            #Change "=" To  "=3D" for HTML formatted mail
            $header =~ s/=/=3D/g;

            #Change "=" To  "=3D" for HTML formatted mail
            $footer =~ s/=/=3D/g;


}


sub get_headlines()
{


            #Get Headlines
            $html = get("http://www.gp.se/gpmobil/rubvsvfr.htm");
            #$html = get("http://www.gp.se/dagl/frames/cgi/grab/grab.html");

            #Change "=" To  "=3D" for HTML formatted mail
            $html =~ s/=/=3D/g;

}


sub read_email_list()
{

      open(EMAILS, "$email_list");

      while (<EMAILS>)
      {
            chomp($address = $_);

            $body = "$header $html $footer";

            &send_email($hostname,$address,$from_name,$from_address,$subject,$body);
            
            #print "&send_email($hostname,$address,$from_name,$from_address,$subject,$body)";

      }



}





sub send_email($$$$$)
{

my($hostname,$address,$from_name,$from_address,$subject,$body) = @_;
#
# Connect to SMTP server.
#
$mailserv = new Net::SMTP($hostname, Debug => 1,);

unless ( $mailserv ) {
      &ErrorLog( "Could not connect to mail server $hostname; $!\n", $LOG_FILE );
}


#
# Format message.
#
$data = <<END;
From: "$from_name" <$from_address>
To: <$address>
Subject: $subject
MIME-Version: 1.0
Content-Type: multipart/alternative;
      boundary="----=_NextPart_000_000F_01BF381C.C17D0920"
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3

This is a multi-part message in MIME format.

------=_NextPart_000_000F_01BF381C.C17D0920
Content-Type: text/plain;
      charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Plain Text

------=_NextPart_000_000F_01BF381C.C17D0920
Content-Type: text/html;
      charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

$body

END


#
# Send the message.
#
$mailserv->mail( $from_address );
$mailserv->to( $address );
$mailserv->data( $data );


#
# Close connection.
$mailserv->quit();

# smtp.pl
}



#-------------------------------#
# Generate error LOG                  #
#-------------------------------#
sub ErrorLog($$)
{
   #--- $_[0] = Error line             ---#
   #--- $_[1] = Error Log             ---#

      my( $line, $file ) = @_;
   
      open LOG, ">>$archivo";

      print "Error de ape $_[1]: $!";

      print LOG $line;

      close (LOG);

      return 1;
}




Be very careful in the set vars sub to really set the vars to what you need.  Then create a emails.txt (or what ever you want to call it) or use the one being generated by the other cgi if it is functioning well.  Then run this scrpt from the command line.  You should see it mail once per email in the txt file.  If all this goes well we should be in the money!!!!!  If we have problems mabe we should use ICQ a little.
0
 
ozoCommented:
null
0
 
guadalupeCommented:
So, how did we do?
0
 
algabatzAuthor Commented:
Adjusted points to 600
0
 
algabatzAuthor Commented:
Sorry, i have had trouble with the modules so i haven't been able to run it. If you can find a workaround it would be great, but i feel that you have earned your points.
Otherwise I'll be in touch if i can't get it rolling.
Best,
Al Gabatz
0
 
guadalupeCommented:
Ok I have some ideas.  First can you get the modules from www.perl.com (Do you know the drill?  If not let me know.  Then place the Simple.pm and SMTP.pm files in the same dir as the cgi script and give it a try.  If this doesn't work try isolating the problem.  Make a stupid script that requires the two pm (while they are in the same dir as the script) an make sure to put this line before the use lines:

BEGIN { push(@INC,"./"); }


This tells perl to include the current directory when it looks for installed modules. Run this dummy script from the command line and see if it fails because of the moduals...

Let me know as we still arn't done with this and thanks for the points!!!

0
 
guadalupeCommented:
Sorry one more thing.  If you put the pm's in the same dir then you must change:

use LWP::Simple;
use Net::SMTP;

to

use Simple;
use SMTP;

Sorry. Let me know how it goes.
0
 
guadalupeCommented:
Hey, never heard how we finished up, don't want you to think I just dissapear when I get my points. Did it work out?
0
All Courses

From novice to tech pro — start learning today.