Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

CGI, cookies, sendmail, HTML attachments, searching txt files,

by dumbadam (Novice)
on Oct 26, 2001 at 11:16 UTC ( [id://121595]=perlquestion: print w/replies, xml ) Need Help??

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

Three questions, Sorry!

1) I have set up a form so that people can submit there email addresses to my mailing list.
Ok I have been messing around with an autoreply and need to be able to send an "HTML" file with the autoreply thru sendmail. This way you get a GREAT email message instead of yucky text-on-text

Does anyone know how to do this?

2)Does anyone have a snippet of code that will check a existing .txt file for the email address entered and return "already added" to the user if they already added there email address? I cant seem to get one right.

3)Anyone know why The cookie i set below for +1y seems to not be there after i close my browser? Is it the way im reading it? I cant seem to use HTTP::cookie so i use $q->cookie().

heres a sample:
#!/usr/local/bin/perl use CGI; $q = new CGI; $thefile = "email.txt"; $my_email = "webmaster\@itsmeagain.com"; $email_entered = $q->param('email'); #check for email valid $FORM = $q->param('email') . ","; if ($q->param('email')) { unless ($FORM =~ /\w+@\w+.\w+/) { print $q->header(-type=>'text/html', accept=>'*/*'); print $q->start_html(-title=>'Bad Email'); #print "Content-type: text/html\n\n"; print "<html><head><title>Bad E-mail</title></head>\n" +; print "<body><h1>Bad E-mail</h1><br>The e-mail address + that you've\n"; print "entered,<b><font color='red' size='+3'> $FORM</ +font></b> is invalid. Please click back and\n"; print "try again.\n"; print $q->end_html; exit; } #chk for cookie $JSCRIPT=<<END; function go(){ if (document.referer){ window.parent.location=document.referer; } else{ document.location= "http://www.mysite.com/index2.htm"; } } setTimeout("go()", 5000); END $chkcookie = $q->cookie( "email" ); if ($chkcookie eq $email_entered){ print $q->header(-type=>'text/html', accept=>'*/*'); print $q->start_html(-title=>'Already Entered This Email', script=>$JS +CRIPT); print "<h1><b>You just entered this email address! </h1>---<font size= ++1></b>Click back to continue or wait and I'll send you!"; print $q->end_html; } else { &filewritee; } } else { print $q->header(-type=>'text/html', accept=>'*/*'); print $q->start_html(-title=>'Nothing Entered'); print "<h1>Nothing Entered.</h1><br><br>\n"; print "You didn't enter anything, Pls go back and enter a valid email +address"; print $q->end_html; } sub filewritee{ open (EMAILADDRESS, ">> $thefile") or die "Can't open file: $!"; flock EMAILADDRESS, 2; print EMAILADDRESS $FORM; close EMAILADDRESS; &sendmail; } sub sendmail{ open (MAIL, "| /usr/vde/bin/sendmail -t") or die "couldnt open sendmail: $!"; print MAIL <<EO; To: $email_entered From: $my_email Subject: Weekly mail from me EO open (FILE, "< weekly.htm") or die "Cannot open file: $!"; while (<FILE>) { print MAIL; } close FILE; close (MAIL); &successs; } sub successs{ $JSCRIPTT=<<ENDD; document.cookie = "email=$email_entered", expires="+1y"; ENDD print $q->header(-type=>'text/html', accept=>'*/*'); print $q->start_html(-title=>'-Email Added Successfully!', -script=>$J +SCRIPTT); print "<h1><font color='#000080'>SUCCESS!</h1><br><br>\n"; print <<ENDOF; <p><b>Your Email address: </font><font color='red' size='+2'> \"$email +_entered\" </font><font color='#000080'>was added successfully!<br></ +b> You should be getting your first mail shortly!</p> ENDOF print $q->end_html; exit; }

Replies are listed 'Best First'.
Re: CGI, cookies, sendmail, HTML attachments, searching txt files,
by projekt21 (Friar) on Oct 26, 2001 at 13:17 UTC

    A couple of questions... I'll try to give some comments on the first one:

    That HTML mail might look pretty but I would like to encourage you to send plain ascii, but that is just my humble optinion.

    Maybe you you want to have a look at MIME::Entity and use it to create a RFC compatible email containing both, a text/plain MIME part and a text/html part for those mail clients that support it. MIME::Entity has a well understandable documentation showing you how you create a mail and finally give it to sendmail.

    If you prefer doing that all by yourself, do not forget to set a correct Content-Type in your mail header (look at the headers of some HTML mails you got).

    Hope this helps a bit.

    alex pleiner <alex@zeitform.de>
    zeitform Internet Dienste

Re: CGI, cookies, sendmail, HTML attachments, searching txt files,
by projekt21 (Friar) on Oct 26, 2001 at 13:20 UTC

    For question No. 3:

    I use something like that to read and write cookies:

    use CGI; my $q = new CGI; # read cookie my %cookies = $q->cookie(-name => "test_foo"); my $bar = $cookies{bar}; my $foo = $cookies{foo}; # write cookie my $cookie = $q->cookie(-name => "test_foo", -value => { foo => "foo", bar => "bar", }, -expires => '+10m'); print $q->header(-cookie=>$cookie, -expires => "now");

    alex pleiner <alex@zeitform.de>
    zeitform Internet Dienste

Re: CGI, cookies, sendmail, HTML attachments, searching txt files,
by projekt21 (Friar) on Oct 26, 2001 at 13:26 UTC

    Question 2 (code not tested):

    sub checkemail { my $email = shift; open IN, "/some/textfile.txt" or die "errormessage"; while (<IN>) { chomp; return "already added" if $email eq $_; # if textfile.txt has one email address per line # and nothing else } close IN; return; }

    Update (thanks to Hofmator for showing the obvious): dropped that error

    This can be improved, but it should work. Hope it helps.

    alex pleiner <alex@zeitform.de>
    zeitform Internet Dienste

      return "already added" if $email eq chomp($_);

      This has to be replaced by

      chomp; return "already added" if $email eq $_;
      as chomp returns the number of characters chomped. Common mistake ;-)

      -- Hofmator

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://121595]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found