package TripodMail; ################ # TripodMail.pm # ############################################################################# # TripodMail is a module that allows you to send out email messages with # your scripts. In order to use it you'll have to have a mail template in # your cgi-bin directory. The mail template will need to look something # like this: # To: $email # From: FredFlintstone@hotmail.com # Subject: YabbaDabbaDoo! # # Hello $name, # Congratulations! You're user number $number of this mail script! # You can add other email headers (Reply-To:, Errors-To:, etc), but To: # and From: are manditory. You can customize your email by adding variables # wherever you would like to fill something in on the fly. # The sendMail method requires 2 parameters- the location of the mail # template file, and a reference to a hash of variables. # The keys of the varaible hash should correlate with the variables in the # mail template. # Example of use: # require TripodMail; # $MAIL = new TripodMail; # $mail_template = "./flintstones_mail.txt"; # %variables = ('email' => 'Wilma@gurlmail.com', # 'name' => 'Wilma', # 'number' => '2'); # $MAIL->sendMail($mail_template, \%variables); # Note: In order to prevent spamming, you will be limited to sending out 240 # mails per day. ########################################################################### sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } sub sendMail { # example: $MAIL->sendMail($template_file, \%hash_o_variables) # requires: 1) name of a file that is a template for the mail # 2) a reference to hash of variables to fill out the template # does: writes a mail file to member's directory to be mailed later # by sendmail # returns: 1 on success, 0 on failure my ($self, $template_file, $hash_ref) = @_; my ($message, $key, $time, $file); # error checking if ((! $template_file)||(! $hash_ref)){ print STDERR "usage: sendMail(template_file, hash_reference)\n"; return 0; } if (! -s $template_file){ print STDERR "file does not exist or has a 0 size!\n"; return 0; } # read in template open (MESSAGE, "<$template_file") or die "can't open template file $template for reading\n"; undef $/; $message = ; close (MESSAGE); $/ = "\n"; # variable substitution foreach $key (keys (%{$hash_ref})){ $message =~ s/\$$key/$hash_ref->{$key}/eg; } # check final template format if ($message !~ /to:.*\w+@\w+\.\w+/i){ print STDERR "To: field missing or invalid recipient\n"; return 0; } if ($message !~ /from:.*\w+@\w+\.\w+/i){ print STDERR "From: field missing or invalid sender\n"; return 0; } # write template to file $time = time(); $file = 'mail.'.$time; open (FILE, ">$file") or die "can't open file: $file for writing\n"; print FILE $message; close (FILE); return 1; } 1;