CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
A few glitches in email script

 



Scot
Novice

Apr 19, 2007, 1:16 PM

Post #1 of 7 (875 views)
A few glitches in email script Can't Post

Hi,

I have a file with some data like so:


Scot|admin@link-exchangers.com|Thu Apr 19 03:39:19 EDT 2007
|

Scot|support@webwizsoftware.com|Thu Apr 19 03:41:05 EDT 2007
|

Scot|support@link-exchangers.com|Thu Apr 19 14:17:14 EDT 2007
|

The program used to create this writes 2 newlines \n\n after the 2007|

I wrote a program which reads in this data, and also reads in data from a text file (below) and the program is supposed to send an email to everyone on the list. This is my optin email program. Right now if you run the program, it says it is emailing all three addresses, because I made it print "Email sent to: " and then the name of the email address in the sendmail sub.

If you run the script http://www.cash-for-structured-settlements.net/cgi-bin/eratesemail.cgi
you'll see what I mean.

I guess my loop isn't working since it's only sending an email to the one of the first line. The other 2 records aren't getting email. I don't understand why after sending the first email, the output says it sent to nobody two more times with no email address before sending to the next email address. I understand that there are 3 items per record and then it hits a newline character. I'm confused about this.

Here is the output right now:
Email sent to : admin@link-exchangers.com Email sent to : Email sent to : Email sent to : support@webwizsoftware.com Email sent to : Email sent to : Email sent to : support@link-exchangers.com Email sent to : Email sent to :


Code
#!/usr/bin/perl 
my $email='';
my $name='';
my $subject="Interest Rate Updates from Pacific West Capital";
my $i=0;
# get rates
open(FILE,"rates.txt") || die ("Can't open file!");
my @rates=<FILE>;
close FILE;
my $r30c=$rates[1];
my $r15c=$rates[3];
my $r51c=$rates[5];
my $r30j=$rates[7];
my $r15j=$rates[9];
my $r51j=$rates[11];
my $date = `date`;
my $message="Interest Rates for $date \n\n\nConforming 30 year fixed: $r30c\nConforming 15 year fixed: $r15c\nConforming 5/1 ARM: $r51c\n\nJumbo 30 year fixed: $r30j\nJumbo 15 year fixed: $r15j\nJumbo 5/1 ARM : $r51j\n\n\n";
# get members list
open(FILE,"../www/mortgage-pros.com/erates/members.txt") || die ("Can't open file!");
my @emaillist=<FILE>;
close FILE;
print "Content-type: text/html\n\n";
my $i=0;
for (@emaillist) {
chomp;

my @emaillistrow=split('\|',$_);
$name= $emaillistrow[0];
$email= $emaillistrow[1];
# print $emaillistrow[0];
# print $emaillistrow[1];
# print $emaillistrow[2];
# print $emaillist[0];
# print length($emaillist[0]);
# print $emaillist[1] ;
# print $emaillist[2];
# exit;
emailrates($name,$email)
}

sub emailrates {
my $sendto=$_[0];
my $fname=$_[1];
unless (open(MAIL, "|/usr/lib/sendmail -t")) {
print "error.\n";
warn "Error starting sendmail: $!"; }
print MAIL "From: info\@mortgage-pros.com\n";
print MAIL "To: $sendto\n";
print MAIL "Subject: $subject\n\n";
print MAIL "Hello $fname,\n\n";
print MAIL $message;
print MAIL "Best Regards,\n\nPacific West Capital";
close(MAIL) || warn "Error closing mail: $!";
print "Email sent to : $email\n";
}


Here is the contents of rates.txt:

C30
6.0
C15
5.75
C5/1
6.125
J30
6.375
J15
6.25
J5/1
6.125


KevinR
Veteran


Apr 19, 2007, 2:29 PM

Post #2 of 7 (872 views)
Re: [Scot] A few glitches in email script [In reply to] Can't Post

the two newlines is probably messing things up. Do two chomps and skip blank lines in the array:


Code
	for (@emaillist) {  
chomp;chomp;
next if /^\s*$/;


see if that helps.
-------------------------------------------------


Scot
Novice

Apr 19, 2007, 6:38 PM

Post #3 of 7 (868 views)
Re: [KevinR] A few glitches in email script [In reply to] Can't Post

Hi,
I fixed the loop with an if statement below. Here's the url again to run it:

http://www.cash-for-structured-settlements.net/cgi-bin/eratesemail.cgi

I"m only receiving the first email though.

#!/usr/bin/perl
my $email='';
my $name='';
my $subject="Interest Rate Updates from Pacific West Capital";
my $i=0;
# get rates
open(FILE,"rates.txt") || die ("Can't open file!");
my @rates=<FILE>;
close FILE;
my $r30c=$rates[1];
my $r15c=$rates[3];
my $r51c=$rates[5];
my $r30j=$rates[7];
my $r15j=$rates[9];
my $r51j=$rates[11];
my $date = `date`;
my $message="Interest Rates for $date \n\n\nConforming 30 year fixed: $r30c\nConforming 15 year fixed: $r15c\nConforming 5/1 ARM: $r51c\n\nJumbo 30 year fixed: $r30j\nJumbo 15 year fixed: $r15j\nJumbo 5/1 ARM : $r51j\n\n\n";
# get members list
open(FILE,"../www/mortgage-pros.com/erates/members.txt") || die ("Can't open file!");
my @emaillist=<FILE>;
close FILE;
print "Content-type: text/html\n\n";
my $i=0;
for (@emaillist) {
chomp;chomp;
# print $emaillist[0];
# print length($emaillist[0]);
# print $emaillist[1];
# print $emaillist[2];
my @emaillistrow=split('\|',$_);
$name= $emaillistrow[0];
$email= $emaillistrow[1];
if ($emaillistrow[1] ne '') { #here's the fix for the loop

unless (open(MAIL, "|/usr/lib/sendmail -t")) {
print "error.\n";
warn "Error starting sendmail: $!"; }
print MAIL "From: info\@mortgage-pros.com\n";
print MAIL "To: $email\n";
print MAIL "Subject: $subject\n\n";
print MAIL "Hello $name,\n\n";
print MAIL $message;
print MAIL "Best Regards,\n\nPacific West Capital";
close(MAIL) || warn "Error closing mail: $!";
print "Email sent to : $email\n";
}
}


KevinR
Veteran


Apr 19, 2007, 8:43 PM

Post #4 of 7 (865 views)
Re: [Scot] A few glitches in email script [In reply to] Can't Post

You'e the poor guy using Netfirms, that is your problem. I am 99% sure Netfirms does not allow you to send multiple emails.
-------------------------------------------------


Scot
Novice

Apr 19, 2007, 8:55 PM

Post #5 of 7 (864 views)
Re: [KevinR] A few glitches in email script [In reply to] Can't Post

Actually, I have this program working now. I had a small booboo in my Outlook Express email account, but the strange thing is now, I'm receiving the 1st and 3rd record, both domains are to link-exchangers.com and the 2nd record is to webwizsoftware.com and I'm not getting that one. This is strange since obviously the loop works now properly, and the 2nd email should arrive and it doesn't get sent. I tested this by sending an email from my outlook to the same email address the program is supposed to send to and it shows up from the Outlook program but when I run the program, for some reason sendmail isn't sending it.


Scot
Novice

Apr 19, 2007, 9:32 PM

Post #6 of 7 (860 views)
Re: [Scot] A few glitches in email script [In reply to] Can't Post

I have tested my program with another email account. Yahoo and MSN. It is working for both of these accounts. The mails do show up. I can only surmise that it has something to do with one of my hosts. This is strange though because I"m not blocking anyone. AOL did not work though.


KevinR
Veteran


Apr 19, 2007, 9:35 PM

Post #7 of 7 (859 views)
Re: [Scot] A few glitches in email script [In reply to] Can't Post

I don't know. It is difficult to trace down email problems of this nature.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives