thatbradguy

My Script

Nov 17th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.62 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use MIME::Lite::TT::HTML;
  3. use LWP::Simple;
  4. use Text::CSV_XS;
  5. use Date::Format;
  6. use DBI;
  7.  
  8. require "/home/user/emailservice/functions.pl";
  9.  
  10. my $ccemailcount = 0;
  11. my $ccemaillist;
  12. my $emailcount = 0;
  13. my $emaillist;
  14. # constant contact variables
  15. my $list_id = 1;
  16. ####################################
  17. my $today = time2str("%Y-%m-%d",time);
  18. my $timebetweensends = 7 * 24 * 60 * 60;
  19. my $timecheck = time();
  20. my $url = 'http://mail.domain.com:8000/asptest.asp';
  21. my $file = '/home/user/emailservice/log_thankyou/thankyou'.$today.'.txt';
  22. my $content = getstore($url,$file);
  23. die "Couldn't get it!" unless defined $content;
  24. my $table = "log";
  25. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  26.  
  27. # choose email template
  28. $textemailtemplate = "default.txt.tt";
  29. $htmlemailtemplate = "default.html.tt";
  30.  
  31. $dbh = DBI->connect ("DBI:CSV:f_dir=/home/user/emailservice");
  32.  
  33. # open csv file that holds today's content
  34. my $csv = Text::CSV_XS->new ({ binary => 1, sep_char => "|" });
  35. open my $fh, "<", $file or die "$file: $!";
  36.         while (my $row = $csv->getline ($fh)) { #read a line from the csv file
  37.                 my ($firstname, $lastname) = FixNames(@$row[0]);
  38.                 my $emailaddress = @$row[1];
  39.                 my $lastin = @$row[3];
  40.                 my $subject = "Thank You ".$firstname;
  41.                 ($ccemailcount, $ccemaillist) = ConstantContact($emailaddress, $firstname, $lastname, $list_id, $ccemailcount, $ccemaillist);
  42.                 # check the log to see if this person has ben emailed in the last 7 days
  43.                 my $query = "SELECT email, timestamp, lastin FROM $table WHERE email = ". $dbh->quote ($emailaddress)." AND lastin = ". $dbh->quote ($lastin);
  44. my $sth = $dbh->prepare ($query);
  45.                 $sth->execute ();
  46.                 while (my $query_row = $sth->fetchrow_hashref) {
  47.                         if ( ($query_row->{timestamp} > 0) ) { #do nothing
  48.                         } else {
  49.                                 my %params;
  50.                                 $params{first_name} = $firstname;
  51.                                 $params{last_name}  = $lastname;
  52.                                 my %options;
  53.                                 $options{INCLUDE_PATH} = '/home/user/emailservice/emailtemplates';
  54. $emailaddress = "email\@domain.biz";
  55.                                 my $msg = MIME::Lite::TT::HTML->new(
  56.                                         From  => 'User <[email protected]>',
  57.                                         To => $emailaddress,
  58.                                         Subject => $subject,
  59.                                         Template => {
  60.                                                 text => $textemailtemplate,
  61.                                                 html => $htmlemailtemplate,
  62.                                         },
  63.                                         TmplOptions =>  \%options,
  64.                                         TmplParams  =>  \%params,
  65.                                 );
  66.                                 $msg->send;
  67.                                 $emailcount ++;
  68.                                 $emaillist .= $firstname." ".$lastname."\n";
  69.                                 $dbh->do ("INSERT INTO $table VALUES (". $dbh->quote (time()) .",". $dbh->quote ($firstname).",". $dbh->quote ($lastname). ",". $dbh->quote ($emailaddress) .",". $dbh->quote ($today) .",". $dbh->quote ($lastin).")");
  70.                                 my $msg = MIME::Lite::TT::HTML->new(
  71.                                         From  => 'User <[email protected]>',
  72.                                         To => '[email protected]',
  73.                                         Subject => $subject,
  74.                                         Template => {
  75.                                                 text => $textemailtemplate,
  76.                                                 html => $htmlemailtemplate,
  77.                                         },
  78.                                         TmplOptions =>  \%options,
  79.                                         TmplParams  =>  \%params,
  80.                                 );
  81.                         #       $msg->send;
  82.                         }
  83.                 }
  84.                 $sth->finish ();
  85.         }
  86. csv->eof or $csv->error_diag;
  87. close $fh or die "file.csv: $!";
  88.  
  89. printf "%d Thank You Email(s) were sent, and %d addresses were added to Constant Contact\n",$emailcount, $ccemailcount;
  90. if ($emailcount > 0) { printf "Emails were sent to:\n================================\n%s", $emaillist; }
  91. if ($ccemailcount > 0) { printf "Constant Contact Additions:\n==========================\n%s",$ccemaillist; }
  92.  
  93. exit;
Advertisement
Add Comment
Please, Sign In to add comment