Gossamer Forum
Home : Products : Links 2.0 : Customization :

Adding Newsletter Support (Tough one?)

Quote Reply
Adding Newsletter Support (Tough one?)
The Problem - Getting this to run as part of add.cgi when a link is submitted

The Reason - Adding a user's e-mail address to a textfile my newsletter software uses to send my weekly newsletter. The goal is that when a person enters their information and doesn't uncheck the "doThis" checkmark on the add.html template's form (which when checked means they want to subscribe to the newsletter) they are added to address.txt if not already in it as a subscriber. I am absolutely sure that $in{'doThis'} DOES return the text "subscribe". I tested this already. But the fact is no matter where I put &subscribe; or if i take out the "sub subscribe {" and the matching "}" and leave this code at the end of add.cgi it just doesn't add their mail to address.txt nor does it send webmaster@websitegoodies.com any email, meaning to me that this portion of code (below) just isn't happening at all. Any help getting it to work would be appreciated, this is to automate subscriptions to my newsletter through links and other scripts on my site.

I tried changing

Code:
else { print NEWLIST "$recipient\n"; }

to

Code:
else { print NEWLIST "in{'Contact Email'}\n"; }

but this didn't work.

Code:
sub subscribe {

# Define a few important variables
$mailprogram = "/usr/lib/sendmail -t"; # path to sendmail with a modifier
$subscription = "/home/goodies/www/cgi-bin/address.txt"; # name of the file that stores the email list
$greetings = "../address.txt"; # welcome message to send to new subscribers
$sender = "newsletter\@websitegoodies.com"; # notice that all email @ signs are backslashed
$sendername = "WSG Webmaster News";
if ($ENV{"REQUEST_METHOD"} eq 'GET') { $buffer = $ENV{'QUERY_STRING'}; }
else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }

if ($in{'doThis'} eq "subscribe") {

# Now that you have the data, find out what you're telling
# the script to do and let it call the appropriate function
# (subroutine) to perform the task.



unless($in{'email'} =~ /.*\@.*\..*/) {
$validmail = "no";
exit;
}

open(OLDLIST, "$subscription");
@subscribers = <OLDLIST>;
close(OLDLIST);

#Now write over the original subscription list entry by entry
#Compare each entry with that of the new subscriber - if they are
#one and the same, print out a message saying that the email
#address is already on the list, otherwise, write the old entries
#into the file.

#At the end, don't forget to add the new entry to the end of the
#file - AFTER the foreach loop.
open(NEWLIST, ">$subscription");
foreach $recipient(@subscribers) {
chomp($recipient);
if ($recipient eq $in{'Contact Email'}) {
$subscribed = true;
}
else { print NEWLIST "$recipient\n"; }
}

if(!$subscribed) { print NEWLIST "$in{'Contact Email'}\n"; }
close(NEWLIST);

#Now that the email address has been successfully added, send
#a message to the new subscriber confirming the addition:

open (MAIL, "|$mailprogram") | | die "Can't open $mailprogram!\n";
print MAIL "To: webmaster\@websitegoodies.com\n";
print MAIL "From: $in{'Contact Email'} < $in{'Contact Name'} >\n";
print MAIL "Subject: Subscription Confirmed!\n";
print MAIL "Address Added ( $in{'Contact Email'} )";

close(MAIL);

print "Subscribed.";

}
}

Thanks to all who can help Smile
Quote Reply
Re: Adding Newsletter Support (Tough one?) In reply to
The following line is incorrect:

Code:
else { print NEWLIST "in{'Contact Email'}\n"; }

You need to put a $ sign before the in...so, the codes would look like the following:

Code:
else { print NEWLIST "$in{'Contact Email'}\n"; }

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
Anthro TECH, L.L.C
anthrotech.com
* Check Resource Center
* Search Forums
* Thinking out of the box (codes) is not only fun, but effective.


Quote Reply
Re: Adding Newsletter Support (Tough one?) In reply to
Still doesn't work even with that modification, I just don't see any results from my addition to the script. If I were to use it as a subroutine (like in the code of my first post) where would i call it? Any other ideas as to why it won't mail/write the textfile/run? Thanks for the reply.
Quote Reply
Re: Adding Newsletter Support (Tough one?) In reply to
You would need to put the sub in the add.cgi file.

And you would have to put something like:

Code:
if ($in{'doThis'} eq "subscribe") {
&subscribe;
}

in the sub main routine.

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
Anthro TECH, L.L.C
anthrotech.com
* Check Resource Center
* Search Forums
* Thinking out of the box (codes) is not only fun, but effective.


Quote Reply
Re: Adding Newsletter Support (Tough one?) In reply to
Did what you said and put the code you gave me (the if statement) right before the last } of the main subroutine (the one that ended it). Yet, this is why I'm so confused and needed help: It still isn't working. No mail from the new subroutine and I checked address.txt myself and it isn't writing the new email address (which isn't already in it). I'm only a newbie with perl so I could've written the subroutine wrong (basically rewrote bits and pieces of two subscription programs into a way that seemed to make sense - check if it's in the list, if not write it to the list).

Thanks for the try. Once again, any help is appreciated.
Quote Reply
Re: Adding Newsletter Support (Tough one?) In reply to
Well, try changing the if part of the codes I gave you to elsif and stick it after the first if conditional statement in the sub main...

So, it should look like the following:

Code:
elsif ($in{'doThis'} eq "subscribe") {
&subscribe;
}

Then delete the following codes in the sub you wrote:

Code:
if ($in{'doThis'} eq "subscribe") {

and delete the associated right bracket, }.

Then delete the unless codes for the format because this is already taken care of in the add.cgi script.

Also, with your mailing list...are there two fields (recipient and email address).

How is the email database printed? I think that you are missing a lot of codes to properly write into your mailing list database.

? Why not use the Newsletter that comes with LINKS?

And put a form in the Success Page that allows people to submit their name and email address to the LINKS Newsletter database. I have this in my site and it works fine.

Also...I provided codes to another user for integrating the LINKS Newsletter subs into the add.cgi script about five days ago in this forum.

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
Anthro TECH, L.L.C
anthrotech.com
* Check Resource Center
* Search Forums
* Thinking out of the box (codes) is not only fun, but effective.


Quote Reply
Re: Adding Newsletter Support (Tough one?) In reply to
First off, my newsletter was started before I started using Links so I want to stay with the current newsletter program & flat textfile database I use (it's simply one address per line). I've changed the contents of the sub to print "yes"; and it showed up on the add success page which means the sub IS running. So I went through it another 50 times and saw one problem:

Code:
unless($in{'email'} =~ /.*\@.*\..*/) {
$validmail = "no";
exit;
}

Problem is the variable is $in{'Contact Email'} not $in{'email'} so that one didn't work. I fixed it and ran through the script again in my browser - the link adds fine, the name isn't added to my newsletter, the confirmation mail of the newsletter subscription to me isn't sent. So, now that I know it's getting to the sub in the script and have fixed the mail validation, I'm looking through other subscription scripts for how they go through the same type of database to add a person so maybe I can borrow some snippets. At the bottom of this message I'll include a copy of the code as it is now (changing every few seconds at this pace). Any help would be appreciated, thanks for the tips (at least i've got the sub running now!)

Code:
#!/usr/bin/perl
# -------------
# Links
# -------------
# Links Manager
#
# File: add.cgi
# Description: Adds a record marked unvalidated to the database and
# optionally emails someone.
# Author: Alex Krohn
# Email: alex@gossamer-threads.com
# Web: http://www.gossamer-threads.com/
# Version: 2.0
#
# (c) 1998 Gossamer Threads Inc.
#
# This script is not freeware! Please read the README for full details
# on registration and terms of use.
# =====================================================================
#
# Setup Notes:
# Make sure the require statement below points to the config file.


# Required Librariers
# --------------------------------------------------------
eval {
($0 =~ m,(.*)/[^/]+,) && unshift (@INC, "$1"); # Get the script location: UNIX /
($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1"); # Get the script location: Windows \

require "admin/links.cfg"; # Change this to full path to links.cfg if you have problems.
require "$db_lib_path/db_utils.pl";
require "$db_lib_path/links.def";
$build_use_templates ?
require "$db_lib_path/site_html_templates.pl" :
require "$db_lib_path/site_html.pl";
};
if ($@) {
print "Content-type: text/plain\n\n";
print "Error including libraries: $@\n";
print "Make sure they exist, permissions are set properly, and paths are set correctly.";
exit;
}

# ========================================================


eval { &main; }; # Trap any fatal errors so the program hopefully
if ($@) { &cgierr("fatal error: $@"); } # never produces that nasty 500 server error page.
exit; # There are only two exit calls in the script, here and in in &cgierr.

sub main {
# --------------------------------------------------------
local (%in) = &parse_form;

# We are processing the form.
if (keys %in != 0) {
&process_form;
}
# Otherwise we are displaying the form (in site_html.pl).
else {
if ($db_single_category) {
my %is_valid = map { $_ => 1 } &category_list;
$ENV{'HTTP_REFERER'} =~ s,/[^/]+\.[^/]+$,,;
$ENV{'HTTP_REFERER'} =~ m,$build_root_url/(.+?)/?$,;
$is_valid{$1} ? &site_html_add_form ($1) : &site_html_add_form ();
}
else {
&site_html_add_form ();
}
}

if ($in{'doThis'} eq "subscribe") {
&subscribe;
}
}

sub process_form {
# --------------------------------------------------------
my ($key, $status, $line, $output);

# Check the referer.
if (@db_referers and $ENV{'HTTP_REFERER'}) {
$found = 0;
foreach (@db_referers) {
$ENV{'HTTP_REFERER'} =~ /$_/i and $found++ and last;
}
if (!$found) {
&site_html_add_failure ("Auto submission is not allowed in this directory. Please visit the site to add your entry.");
return;
}
}

# This will set system fields like Validated to their proper values.
foreach $key (keys %add_system_fields) {
$in{$key} = $add_system_fields{$key};
}

# Set date variable to today's date.
$in{$db_cols[$db_modified]} = &get_date;

open (ID, "<$db_links_id_file_name") or &cgierr("error in process_form. unable to open id file: $db_links_id_file_name. Reason: $!");
$in{$db_key} = <ID> + 1; # Get next ID number
close ID;

# Validate the form input..
$status = &validate_record(%in);
if ($status eq "ok") {

# Update the counter.
open (ID, ">$db_links_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_links_id_file_name. Reason: $!");
flock(ID, 2) unless (!$db_use_flock);
print ID $in{$db_key}; # update counter.
close ID; # automatically removes file lock

# Print out the validate input to a "validation database" where it is stored until
# the admin decides to add it into the real database.
open (VAL, ">>$db_valid_name") or &cgierr("error in add_record. unable to open validate file: $db_valid_name. Reason: $!");
flock(VAL, 2) unless (!$db_use_flock);
print VAL &join_encode(%in);
close VAL; # automatically removes file lock

# Send the admin an email message notifying of new addition.
&send_email;

# Send the visitor to the success page.
&site_html_add_success;
}
else {
&site_html_add_failure($status);
}
}

sub send_email {
# --------------------------------------------------------
# Sends an email to the admin, letting him know that there is
# a new link waiting to be validated. No error checking as we don't
# want users to see the informative &cgierr output.

# Check to make sure that there is an admin email address defined.
$db_admin_email or &cgierr("Admin Email Address Not Defined in config file!");

my $to = $db_admin_email;
my $from = $in{$db_cols[$db_contact_email]};
my $subject = "Addition to Database: $in{'Title'}\n";
my $msg = qq|
The following link is awaiting validation:

Title: $in{'Title'}
URL: $in{'URL'}
Category: $in{'Category'}
Description: $in{'Description'}
Contact Name: $in{'Contact Name'}
Contact Email: $in{'Contact Email'}
Subscription: $in{'doThis'}

Remote Host: $ENV{'REMOTE_HOST'}
Referer: $ENV{'HTTP_REFERER'}

To validate, please go to:
$db_script_url

Sincerely,

Links Manager.
|;

# Then mail it away!
require "$db_lib_path/Mailer.pm";
my $mailer = new Mailer ( { smtp => $db_smtp_server,
sendmail => $db_mail_path,
from => $from,
subject => $subject,
to => $to,
msg => $msg,
log => $db_mailer_log
} ) or return;
$mailer->send or return;

}


sub subscribe {

# Define a few important variables
$mailprogram = "/usr/lib/sendmail -t"; # path to sendmail with a modifier
$subscription = "/home/goodies/www/cgi-bin/address.txt"; # name of the file that stores the email list



unless($in{'Contact Email'} =~ /.*\@.*\..*/) {
$validmail = "no";
exit;
}


open(OLDLIST, "$subscription");
@subscribers = <OLDLIST>;
close(OLDLIST);

open(NEWLIST, ">$subscription");
foreach $recipient(@subscribers) {
chomp($recipient);
if ($recipient eq $in{'Contact Email'}) {
$subscribed = true;
}
else { print NEWLIST "$recipient\n"; }
}

if ($subscribed = "true") {
$variablehere = "null";
} else {
print NEWLIST "$in{'Contact Email'}\n";

close(NEWLIST);

open (MAIL, "|$mailprogram") &#0124; &#0124; die "Can't open $mailprogram!\n";
print MAIL "To: webmaster\@websitegoodies.com\n";
print MAIL "From: $in{'Contact Email'} < $in{'Contact Name'} >\n";
print MAIL "Subject: Subscription Confirmed!\n";
print MAIL "Address Added ( $in{'Contact Email'} )";
close(MAIL);



}
}

Thanks all.
Quote Reply
Re: Adding Newsletter Support (Tough one?) In reply to
I'm extremely happy to be able to end this thread. Success! I rewrote the code completely and based it on the method used by my other newsletter management software and the result was something that worked. Thanks to AnthroRules for getting me to the point where the sub would run (without that rewriting the code in the sub wouldn't matter)!

In case anybody wants to see it (I love looking at other peoples' code to see how they make their scripts work how they work) I'll include it at the end of this message. Once again, thanks!

http://www.websitegoodies.com - now you can subscribe to my email while adding your link!


Code:
sub subscribe {

# Define a few important variables
$mailprogram = "/usr/lib/sendmail -t"; # path to sendmail with a modifier
$subscription = "/home/goodies/www/cgi-bin/address.txt"; # name of the file that stores the email list


unless($in{'Contact Email'} =~ /.*\@.*\..*/) {
print "bad mail";
exit;
}

open (DAT,"/home/goodies/www/cgi-bin/address.txt");
if ($LOCK_EX){
flock(DAT, $LOCK_EX); #Locks the file
}
@database_array = <DAT>;
close(DAT);

foreach $lines(@database_array) {
chomp($lines);


&parseemail;

if ($lines =~ /$in{'Contact Email'}/i) {
exit;

}
}
open (DAT,">>/home/goodies/www/cgi-bin/address.txt") &#0124; &#0124; die print"Permissions Error - Can't open address.txt file.";
if ($LOCK_EX){
flock(DAT, $LOCK_EX); #Locks the file
}
print DAT "$in{'Contact Email'}\n" &#0124; &#0124; die print"Permissions Error - can't write to address.txt file.";
close (DAT);


open (MAIL, "|$mailprogram ")
&#0124; &#0124; print "Can't start mail program";
print MAIL "To: webmaster\@websitegoodies.com\n";
print MAIL "From: Newsletter\@websitegoodies.com\n";
print MAIL "Subject: New Subscriber\n\n";


print MAIL "This message is to confirm the addition of your\n";
print MAIL "email address: $in{'Contact Email'} to the newsletter\n";
print MAIL "mailing list. You should recieve the first issue \n";
print MAIL "on the next Wednesday.\n\n";
print MAIL "If you feel you have received this notice in error,\n";
print MAIL "please visit thenewsletter mailing list\n";
print MAIL "at our website: WSG\n";
print MAIL "to remove yourself automatically.\n\n";
print MAIL "Thank you,\n\n";
print MAIL "newsletter\n\n";

close (MAIL);

open (MAIL, "|$mailprogram ")
&#0124; &#0124; print "Can't start mail program";
print MAIL "To: $in{'Contact Email'}\n";
print MAIL "From: Newsletter\@websitegoodies.com\n";
print MAIL "Subject: You've Been Added!\n\n";
print MAIL "This message is to confirm the addition of your\n";
print MAIL "email address: $in{'Contact Email'} to the WSG Webmaster News\n";
print MAIL "mailing list. You should recieve the first issue \n";
print MAIL "on the next Wednesday.\n\n";
print MAIL "If you feel you have received this notice in error,\n";
print MAIL "please visit the WSG Webmaster News mailing list\n";
print MAIL "at our website: http://www.websitegoodies.com\n";
print MAIL "to remove yourself automatically.\n\n";
print MAIL "Thank you,\n\n";
print MAIL "WSG Webmaster News\n\n";

close (MAIL);

}


sub parseemail {

$email = $in{'Contact Email'};
$existing = $lines;
$existing =~ tr/A-Z/a-z/;
$email =~ tr/A-Z/a-z/;

}