Gossamer Forum
Home : Products : DBMan : Customization :

Email multiple records...

Quote Reply
Email multiple records...
I am trying to create a subroutine that will take the records that are checked and email each one in a specific format. I was guessing that I would have a layout similar to the "delete records" screen. The user will then check the records he wants to email, click submit and the new sub routine emails the records in the layout specified by a seperate html_record, except of course, it would not be in HTML format, only text.

I appreciate the help!
Chris

------------------
webmaster@racedaze.com
Quote Reply
Re: Email multiple records... In reply to
You're on the right track with using sub delete_records as a pattern. You don't need to worry about changing the file, so you can eliminate

($output .= $line . "\n");

and the whole section that starts with

open (DB, ">$db_file_name") or &cgierr

Instead of

$delete_list{$data[$db_key_pos]} ?
($delete_list{$data[$db_key_pos]} = 0) :

You'll want to use

Code:
if ($delete_list{$data[$db_key_pos]}) {
$email_message .= "[formatting of the record for sending]";
$delete_list{$data[$db_key_pos]} = 0;
}

You will probably want to change $delete_list to a different variable name just so you don't get confused about what you're doing.

The $email_message variable holds the text of your email. The formatting is not in a {b]$rec{'FieldName'}[/b] format, but in an array, based on the number of the field. If you want to print out, for example, the contents of field 4, use $data[4].


------------------
JPD





Quote Reply
Re: Email multiple records... In reply to
I know this isn't right. I thought I would try it myself.HA!
Any help is greatly appreciated.

Code:
sub ebay_mail {
# --------------------------------------------------------
# Sends up to 100 records to ebay bulk lister
#

my ($key, %ebay_list, $rec_to_ebay, @lines, $line, @data, $errstr, $succstr, $output, $restricted);
$rec_to_ebay = 0;
my $mailtag1 = '\<EBAY_BULK\>\<EBAY_BULK_EMAIL\>NCDIECAST\@aol.com\<\/EBAY_BULK_EMAIL\>\n';
my $mailtag2 = '\<EBAY_BULK_DELIM\>\^\<\/EBAY_BULK_DELIM\>\n';
my $mailformata = '\<EBAY_BULK_ITEM\>\n';
my $mailformatb = '\<EBAY_BULK_ITEM_DESC\>$data[22]\<\/EBAY_BULK_ITEM_DESC\>\n';
my $mailformatc = '$data[10]\^\($data[11]\)\^\($data[12]\)\^\($data[13]\)\^\($data[14]\)\^\($data[15]\)\^$data[16]\^\($data[17]\)\^\($data[18]\)\^\($data[19]\ )\^\($data[20]\)\^\($data[21]\)\n';
my $mailformatd = '\<\/EBAY_BULK_ITEM\>\n';
my $mailtag3 = '\<\/EBAY_BULK\>';
my $mailformat = '$mailformata $mailformatb $mailformatc $mailformatd';
foreach $key (keys %in) { # Build a hash of keys to delete.
if ($in{$key} eq "ebay") {
$ebay_list{$key} = 1;
$rec_to_ebay = 1;
}
}
if (!$rec_to_ebay) {
&ebay_send_failure("You must check the items you want to send.");
return;
}
open (DB, "<$db_file_name") or &cgierr("error in delete_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;

($restricted = 1) if ($auth_modify_own and !$per_admin);

LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp ($line);
@data = &split_decode($line);





if ($ebay_list{$data[$db_key_pos]}) {
$email_message .= "[$mailformat]";
$ebay_list{$data[$db_key_pos]} = 0;
}

open (MAIL,"|/usr/lib/sendmail -t");

print MAIL "To: sadie\@netmcr.com\n";
print MAIL "From: NCDIECAST\@aol.com\n";
print MAIL "Subject: MisterLister\n";
print MAIL "$mailtag1";
print MAIL "$mailtag2";
foreach $key (keys %ebay_list) {
$ebay_list{$key} ?
($errstr .= "$key<BR>") : # that should have been.
($succstr .= "$key<BR>"); # For logging, we'll remember the one's we deleted.
print MAIL "$email_message";
}
print MAIL $mailtag2;
chop($succstr); # Remove trailing delimeter
chop($errstr); # Remove trailing delimeter
close MAIL;



&auth_logging("sent records to ebay: $succstr") if ($auth_logging);
$errstr ? # Do we have an error?
&ebay_send_failure($errstr) : # If so, then let's report go to the failure page,
&ebay_send_success($succstr); # else, everything went fine.
}


------------------
webmaster@racedaze.com
Quote Reply
Re: Email multiple records... In reply to
One problem is using the "$data[#]" variables before you have opened the database file. At the place you use them, the fields will just be blank.

Format your email within the loop where the email is actually sent.

If you're using the "my (variable list)," you need to have it before you define the variables.

That's all I see at the moment.

What happens when you try it?



------------------
JPD





Quote Reply
Re: Email multiple records... In reply to
Still can't get it to work. This is what it looks like now. It gives me a SERVER 500 Error. I'm sure it is in the formatting.
Please help.
Code:
sub ebay_mail {
--------------------------------------------------------
Sends up to 100 records to ebay bulk lister


my ($key, %ebay_list, $rec_to_ebay, @lines, $line, @data, $errstr, $succstr, $output, $restricted);
$rec_to_ebay = 0;
my $mailtag1 = '\<EBAY_BULK\>\<EBAY_BULK_EMAIL\>NCDIECAST\@aol.com\<\/EBAY_BULK_EMAIL\>\n';
my $mailtag2 = '\<EBAY_BULK_DELIM\>\^\<\/EBAY_BULK_DELIM\>\n';
my $mailtag3 = '\<\/EBAY_BULK\>';

foreach $key (keys %in) { # Build a hash of keys to delete.
if ($in{$key} eq "ebay") {
$ebay_list{$key} = 1;
$rec_to_ebay = 1;
}
}
if (!$rec_to_ebay) {
&ebay_send_failure("You must check the items you want to send.");
return;
}



open (MAIL,"|/usr/lib/sendmail -t");

print MAIL "To: sadie\@netmcr.com\n";
print MAIL "From: NCDIECAST\@aol.com\n";
print MAIL "Subject: MisterLister\n";
print MAIL "$mailtag1";
print MAIL "$mailtag2";
foreach $key (keys %ebay_list) {
$ebay_list{$key} ?
($errstr .= "$key<BR>") : # that should have been.
($succstr .= "$key<BR>"); # For logging, we'll remember the one's we deleted.
if ($ebay_list{$data[$db_key_pos]}) {
$email_message .= "[\<EBAY_BULK_ITEM\>\<EBAY_BULK_ITEM_DESC\>$data[22]\<\/EBAY_BULK_ITEM_DESC\>$data[10]\^$data[11]\^$data[12]\^$data[13]\^$ data[14]\^$data[15]\^$data[16]\^$data[17]\^$data[18]\^$data[19]\^$data[20]\^$data[21]\<\/EBAY_BULK_ITEM\>]";
$ebay_list{$data[$db_key_pos]} = 0;
}

}
print MAIL "$mailtag2";
chop($succstr); # Remove trailing delimeter
chop($errstr); # Remove trailing delimeter
close MAIL;



&auth_logging("sent records to ebay: $succstr") if ($auth_logging);
$errstr ? # Do we have an error?
&ebay_send_failure($errstr) : # If so, then let's report go to the failure page,
&ebay_send_success($succstr); # else, everything went fine.
}

------------------
webmaster@racedaze.com
Quote Reply
Re: Email multiple records... In reply to
The first thing I see on the second set of code is that you left off the # at the beginning of the comment lines at the beginning. That will give you a 500 Error.

I guess I hadn't noticed it before, but you're also going to need to open your database so you can get the information to go into the $data[#] variables.

Oh, you did open it in the first version of your code. Let me give you want I think will work.

Code:
sub ebay_mail {
# --------------------------------------------------------
# Sends up to 100 records to ebay bulk lister

my ($key, %ebay_list, $rec_to_ebay, @lines, $line, @data, $errstr, $succstr, $output, $restricted);
$rec_to_ebay = 0;
my $mailtag1 = '\<EBAY_BULK\>\<EBAY_BULK_EMAIL\>NCDIECAST\@aol.com\<\/EBAY_BULK_EMAIL\>\n';
my $mailtag2 = '\<EBAY_BULK_DELIM\>\^\<\/EBAY_BULK_DELIM\>\n';
my $mailtag3 = '\<\/EBAY_BULK\>';

foreach $key (keys %in) { # Build a hash of keys to send.
if ($in{$key} eq "ebay") {
$ebay_list{$key} = 1;
$rec_to_ebay = 1;
}
}
if (!$rec_to_ebay) {
&ebay_send_failure("You must check the items you want to send.");
return;
}

open (DB, "<$db_file_name") or &cgierr("error in delete_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;

open (MAIL,"|/usr/lib/sendmail -t");

print MAIL "To: sadie\@netmcr.com\n";
print MAIL "From: NCDIECAST\@aol.com\n";
print MAIL "Subject: MisterLister\n";
print MAIL "$mailtag1";
print MAIL "$mailtag2";

LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp ($line);
@data = &split_decode($line);
if ($ebay_list{$data[$db_key_pos]} ) {
$email_message .= "[\<EBAY_BULK_ITEM\>\<EBAY_BULK_ITEM_DESC\>$data[22]\<\/EBAY_BULK_ITEM_DESC\>$data[10]\^$data[11]\^$data[12]\^$data[13]\^$ data[14]\^$data[15]\^$data[16]\^$data[17]\^$data[18]\^$data[19]\^$data[20]\^$data[21]\<\/EBAY_BULK_ITEM\>]";
$ebay_list{$data[$db_key_pos]} = 0;
}
}
print MAIL "$mailtag3";
close MAIL;
foreach $key (keys %ebay_list) {
$ebay_list{$key} ? # Check to see if any items weren't sent
($errstr .= "$key,") : # that should have been.
($succstr .= "$key,"); # For logging, we'll remember the one's we sent.
}
chop($succstr); # Remove trailing delimeter
chop($errstr); # Remove trailing delimeter

&auth_logging("sent records to ebay: $succstr") if ($auth_logging);
$errstr ? # Do we have an error?
&ebay_send_failure($errstr) : # If so, then let's report go to the failure page,
&ebay_send_success($succstr); # else, everything went fine.
}

Now, I don't know how the whole ebay thing works, so I'm not sure if this is what you want. It sends out one email that lists information from multiple records.

I did test it for syntax errors and I didn't get any. Be sure that your &ebay_send_success, etc. subroutines don't have any syntax errors in them.

I hope this helps. Smile

------------------
JPD





Quote Reply
Re: Email multiple records... In reply to
Tried it, and it still doesn't work. It sends the email, but nothing in the body.Any ideas?

And thank you for your help!
Chris

------------------
webmaster@racedaze.com
Quote Reply
Re: Email multiple records... In reply to
Try setting your $ebay_list hash to something you already know exists.

After

if (!$rec_to_ebay) {
&ebay_send_failure("You must check the items you want to send.");
return;
}

add

$ebay_list{'[id of a record you know exists]'}

See if that works. If it does, the problem is with the form.

I would also change the "to:" email address to your own until you get it worked out.

You said there's nothing in the body. Does that mean the body is completely blank or that the data doesn't print out.

Wait a minute. If it's just that the data doesn't print out, it may be because you don't tell it to print it to the email message.

Is the "$mailtag3" variable the closing formatting for the email message? If so, before

print MAIL "$mailtag3";

add

print MAIL $email_message;

(I've done things like this before. Smile )

If you're getting a completely blank email, I'm not sure what the problem is.




------------------
JPD





Quote Reply
Re: Email multiple records... In reply to
Cool! It works now except for 1 thing. It prints each record twice! Here is the email it returned. I checked only 3 records.
Code:
<EBAY_BULK><EBAY_BULK_EMAIL>NCDIECAST@aol.com</EBAY_BULK_EMAIL>
<EBAY_BULK_DELIM>^</EBAY_BULK_DELIM>
<EBAY_BULK_ITEM>
<EBAY_BULK_ITEM_DESC>1998 Racing Champions #50 Ricky Craven 1/24 Scale 1969 Camaro Stock Rod.
Only 1,998 Produced.</EBAY_BULK_ITEM_DESC>
1/24 Scale #50 Budweiser Camaro Stock Rod^?^n^n^n^NC^http://ncdiecast.com/images/rci01.jpg^1^0.0^16.95^7^n</EBAY_BULK_ITEM>
<EBAY_BULK_ITEM>
<EBAY_BULK_ITEM_DESC>1998 Racing Champions #50 Ricky Craven 1/24 Scale 1969 Camaro Stock Rod.
Only 1,998 Produced.</EBAY_BULK_ITEM_DESC>
1/24 Scale #50 Budweiser Camaro Stock Rod^?^n^n^n^NC^http://ncdiecast.com/images/rci01.jpg^1^0.0^16.95^7^n</EBAY_BULK_ITEM>
<EBAY_BULK_ITEM>
<EBAY_BULK_ITEM_DESC>test</EBAY_BULK_ITEM_DESC>
test^2666^n^n^n^nc^http://^1^0.0^15.00^7^n</EBAY_BULK_ITEM>
<EBAY_BULK_ITEM>
<EBAY_BULK_ITEM_DESC>1998 Racing Champions #50 Ricky Craven 1/24 Scale 1969 Camaro Stock Rod.
Only 1,998 Produced.</EBAY_BULK_ITEM_DESC>
1/24 Scale #50 Budweiser Camaro Stock Rod^?^n^n^n^NC^http://ncdiecast.com/images/rci01.jpg^1^0.0^16.95^7^n</EBAY_BULK_ITEM>
<EBAY_BULK_ITEM>
<EBAY_BULK_ITEM_DESC>test</EBAY_BULK_ITEM_DESC>
test^2666^n^n^n^nc^http://^1^0.0^15.00^7^n</EBAY_BULK_ITEM>
<EBAY_BULK_ITEM>
<EBAY_BULK_ITEM_DESC>djdfjtdyj</EBAY_BULK_ITEM_DESC>
ytjtyj^jytj^n^n^n^nc^http://^1^0.0^15.00^3^n</EBAY_BULK_ITEM>
</EBAY_BULK>

Here is my sub:
Code:
sub ebay_mail {
# --------------------------------------------------------
#Sends up to 100 records to ebay bulk lister
#
my ($key, %ebay_list, $rec_to_ebay, @lines, $line, @data, $errstr, $succstr, $output, $restricted);
$rec_to_ebay = 0;


foreach $key (keys %in) { # Build a hash of keys to send.
if ($in{$key} eq "ebay") {
$ebay_list{$key} = 1;
$rec_to_ebay = 1;
}
}
if (!$rec_to_ebay) {
&ebay_send_failure("You must check the items you want to send.");
return;
}

open (DB, "<$db_file_name") or &cgierr("error in delete_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;

open (MAIL,"|/usr/lib/sendmail -t");

print MAIL "To: sadie\@netmcr.com\n";
print MAIL "From: NCDIECAST\@aol.com\n";
print MAIL "Subject: MisterLister\n";
print MAIL "\<EBAY_BULK\>\<EBAY_BULK_EMAIL\>NCDIECAST\@aol.com\<\/EBAY_BULK_EMAIL\>\n";
print MAIL "\<EBAY_BULK_DELIM\>\^\<\/EBAY_BULK_DELIM\>\n";

LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp ($line);
@data = &split_decode($line);
if ($ebay_list{$data[$db_key_pos]} ) {
$email_message .= "\<EBAY_BULK_ITEM\>\n\<EBAY_BULK_ITEM_DESC\>$data[22]\<\/EBAY_BULK_ITEM_DESC\>\n$data[10]\^$data[11]\^$data[12]\^$data[13] \^$ data[14]\^$data[15]\^$data[16]\^$data[17]\^$data[18]\^$data[19]\^$data[20]\^$data[21]\<\/EBAY_BULK_ITEM\>\n";
$ebay_list{$data[$db_key_pos]} = 0;
print MAIL "$email_message";
}
}
print MAIL "\<\/EBAY_BULK\>";
close MAIL;
foreach $key (keys %ebay_list) {
$ebay_list{$key} ? # Check to see if any items weren't sent
($errstr .= "$key,") : # that should have been.
($succstr .= "$key,"); # For logging, we'll remember the one's we sent.
}
chop($succstr); # Remove trailing delimeter
chop($errstr); # Remove trailing delimeter

&auth_logging("sent records to ebay: $succstr") if ($auth_logging);
$errstr ? # Do we have an error?
&ebay_send_failure($errstr) : # If so, then let's report go to the failure page,
&ebay_send_success($succstr); # else, everything went fine.
}

------------------
webmaster@racedaze.com
Quote Reply
Re: Email multiple records... In reply to
Instead of

Code:
if ($ebay_list{$data[$db_key_pos]} ) {
$email_message .= "\<EBAY_BULK_ITEM\>\n\[etc.]\n";
$ebay_list{$data[$db_key_pos]} = 0;
print MAIL "$email_message";
}
}
print MAIL "\<\/EBAY_BULK\>";

either use

Code:
if ($ebay_list{$data[$db_key_pos]} ) {
$email_message = "\<EBAY_BULK_ITEM\>\n\[etc.]\n";
$ebay_list{$data[$db_key_pos]} = 0;
print MAIL "$email_message";
}
}
print MAIL "\<\/EBAY_BULK\>";

(this takes out the . before the = in the $emailmessage definition and redefines and prints the variable with each iteration of the loop)

or use

Code:
if ($ebay_list{$data[$db_key_pos]} ) {
$email_message .= "\<EBAY_BULK_ITEM\>\n\[etc.]\n";
$ebay_list{$data[$db_key_pos]} = 0;
}
}
print MAIL "$email_message";
print MAIL "\<\/EBAY_BULK\>";

(this prints the whole email message at once)

Either one should do exactly the same thing. Your choice.


------------------
JPD







[This message has been edited by JPDeni (edited May 04, 1999).]
Quote Reply
Re: Email multiple records... In reply to
I kind of thought that was the problem, so I just took $email_message out totally and just printed it's contents. It works great!
Code:
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp ($line);
@data = &split_decode($line);
if ($ebay_list{$data[$db_key_pos]} ) {
$ebay_list{$data[$db_key_pos]} = 0;
print MAIL "\<EBAY_BULK_ITEM\>\n\<EBAY_BULK_ITEM_DESC\>$data[22]\n\<\/EBAY_BULK_ITEM_DESC\>\n$data[10]\^$data[11]\^$data[12]\^$data[1 3]\^$ data[14]\^$data[15]\^$data[16]\^$data[17]\^$data[18]\^$data[19]\^$data[20]\^$data[21]\n\<\/EBAY_BULK_ITEM\>\n\n";
}
}

Thanks for all of your help! I don't know what I would do without you!

Chris

------------------
webmaster@racedaze.com
Quote Reply
Re: Email multiple records... In reply to
Hi JP!

I was wondering. Suppose that one of the variables in @data had multiple entries. Is there a loop construct that can say:

foreach ($data[19]) {
print the whole email_message, one with each $data[19]

would that make $data[19] an array? I heard you shouldn't pass more than 1 array into a subroutine.

Let me know if this is possible.

------------------
webmaster@racedaze.com

[This message has been edited by ChrisE (edited May 19, 1999).]
Quote Reply
Re: Email multiple records... In reply to
First thing you would have to do is to break up the entries in $data[19]. DBMan uses a ~~ to separate multiple entries when they are saved to the database, so you would use

@array = split(/~~/,$data[19]);

To go through each of the entries in the array, you would then use:

Code:
foreach $entry (@array) {
[print out email]
}

Use the variable $entry instead of $data[19] when you print out your message.

------------------
JPD





Quote Reply
Re: Email multiple records... In reply to
Hey JP!

I have a question. I used the code that you recently added. There is one problem. When my multiple field prints out, it prints out with a | between each selection, not a ~~.

When I looked in the database, it is stored as ~~. Why is it printing out the pipe instead? When I used:
@array = split(/~~/,$data[19]);
It didn't recognize any ~~, so it printed the record out once, with a | between each $entry.
I tried to use:
@array = split(/|/,$data[19]);
But this printed out a record for each character.

Thanks for any help.





------------------
webmaster@racedaze.com
Quote Reply
Re: Email multiple records... In reply to
Well, I used:
@array = split(/\|/,$data[19]);
This seems to work. Is there a better way? I would still like to know why ~~ prints out as |.

Thanks.


------------------
webmaster@racedaze.com
Quote Reply
Re: Email multiple records... In reply to
Sorry I missed your previous question.

The reason the ~~ that is stored in the database becomes a | when it is printed is that there is a subroutine that does just that in db.cgi -- sub split_decode. I had forgotten about that when I gave you the previous code.

Your way of making an array is just fine. Makes sense to me.


------------------
JPD