Gossamer Forum
Home : Products : DBMan : Customization :

Selective Emailing of Records ?

Quote Reply
Selective Emailing of Records ?
Hi Forum I have searched the forums and the FAQ for a few hours now and have not found a solution to my problem but a few people have made similar enquiries.

I have a Jobs database where employers post their Jobs - Straightforward I think, and related to this is the employers details (dbase number 2 relational mods). Now thirdly i am creating a small dbase which will be a Job Agent type scenario. I will allow people to signup and add a record containing their email address, Name and two search criteria from a drop down box namely, Location and Specialism.

What i am looking to do is incorporate some automation into the emailing process. i.e

when an employer posts a job it searches the Job Agent for those people whose Location and Specialism Matches that of the Job and then emails a copy of the job to those persons whose criteria match. This is something I would only want to run after add_success and only for the added record.



The nearest i have come to this can be found at post no: 131277 but I cant understand if it ever worked.

Regards



James Wink
Quote Reply
Re: [jamesamorris] Selective Emailing of Records ? In reply to
I assume the structure would be like this:

1) Employer adds record to Jobs database.

2) When the record is added, look for the E-Mail-Addresses of people in the Job Agent database which match the added record in fields "location" and "specialism".

3) Send the added record to those people, and only those people

In html_add_success, you could add this code after the line "&html_record(%rec);":

"&mail_to_jobseekers(%rec);"

And then you could add this sub, modifying the parameters (field names, field numbers, database file name) as needed:



sub mail_to_jobseekers {

my %rec = @_;

my $location = $rec{'Location'};

my $specialism = $rec{'Specialism'};

my $emailnum = 5; # name of email field in job agent database

my $locationnum = 4; # name of location field in job agent database

my $specialismnum = 6; # name of specialism field in job agent database

my (@email);

open (DB, "<$db_script_path/jobagent.db") || &cgierr ("can't open file in mail_to_jobseekers: $!"); # jobagent.db = name of job agent database

while (my $line = <FILE>) {

chomp($line);

my @line = &split_decode($line);

if (($line[$locationnum] eq $location) || ($line[$specialismnum] eq $specialism)) {

push (@email, $line[$emailnum]);

}

close (DB);

my $text = qq|insert here how the record information should be printed in the email: $rec{'Location'}, $rec{'Specialism'}, $rec{'whatever'} - you can just copy the code used for printing the record in html_record for the Job database|;

foreach my $email (@email) {

open(SENDMAIL, "|/usr/sbin/sendmail -oi -t") or die "Can't fork for sendmail: $!\n";
print SENDMAIL <<"EOF";
From: Yourownemail <youremail\@some.provider>
To: <$email>
Subject: A job for you

A job which matches your location and specialism was just added to our database\n

$text

EOF
close(SENDMAIL) or warn "sendmail didn't close nicely";


}

}

I didn't test it, but unless I missed any syntax errors, it should work.
kellner
Quote Reply
Re: [kellner] Selective Emailing of Records ? In reply to
Thanks for your help , you assumptions were correct I have installed said code and modified as appropriate but it doesnt seem to do anything i had to ad a } to the end because it kept returning a 500 server error. This is the final code.

sub mail_to_jobseekers {

my %rec = @_;

my $location = $rec{'Location'};

my $specialism = $rec{'Specialism'};

my $emailnum = 2;

my $locationnum = 4;

my $specialismnum = 3;

my (@email);

open(DB,"<$db_script_path/mail.db")||&cgierr("cant open jobmail file:$!");

while (my $line = <FILE>){

chomp($line);

my @line = &split_decode($line);

if(($line[$locationnum] eq $location)||($line[$specialismnum] eq $specialism)){

push (@email,$line[$emailnum]);

}

close(DB);

my $text = qq|TEST $rec{'Location'},$rec{'Specialism'}|;

foreach my $email (@email){

open(SENDMAIL,"$mailprog") or die "Can't fork for sendmail: $!\n";

print SENDMAIL <<"EOF";

From:webmaster\@ehrecruitment.co.uk

To:$email

Subject: A Job for you\n

$text

EOF

Close(SENDMAIL) or warn "sendmail didnt close nicely";

}

}


}

With regards to placing the &mail to jobseekers after the &html_record(%rec) in add success, I dont seem to be able to find it as i am using the short long mod. and so have added the call here

&html_record_long(&get_record($in{$db_key}));

&mail_to_jobseekers(%rec);

could it be i have put the call in the wrong place ?

Thanx again



JamesSmile

Quote Reply
Re: [jamesamorris] Selective Emailing of Records ? In reply to
ah yes, the aulde bracket thing.

Try this:

my %rec = &get_record($in{$db_key});

&html_record_long(%rec);

&mail_to_jobseekers(%rec);

kellner
Quote Reply
Re: [kellner] Selective Emailing of Records ? In reply to
I still cant get it to work! And changing this

my %rec = &get_record($in{$db_key});

doesnt seem to make a difference, it wont send any emails, nothing shows in debug and it wont show an error. Any other ideas gratefully received.

I am at a total loss as to why it wont work ive tried calling the sub from different positions but with the same effect.

Thanx for the assistance



JamesSmile

Quote Reply
Re: [jamesamorris] Selective Emailing of Records ? In reply to
please post the exact code of how you call the sub, and how you have actually coded the sub in your script. Also, you might have to adjust the path to sendmail on your server. I think in the code I gave you it's /usr/sbin/sendmail. It might be different on your server.
kellner
Quote Reply
Re: [kellner] Selective Emailing of Records ? In reply to
Hi Kellner, having toyed for a few hours changing things around i still am no further forward. Heres the code i am using the $mailprog is in my .cfg file and this is used for all my other mail functions and seems to work great.

Heres the db.cgi code :

sub mail_to_jobseekers {

my (%rec) = @_;

my $location = $rec{'Location'};

my $specialism = $rec{'Specialism'};

my $emailnum = 2;

my $locationnum = 4;

my $specialismnum = 3;

my (@email);

open(DB,"<$db_script_path/mail.db")||&cgierr("cant open jobmail file:$!");

while (my $line = <FILE>){

chomp($line);

my @line = &split_decode($line);

if(($line[$locationnum] eq $location)||($line[$specialismnum] eq $specialism)){

push (@email,$line[$emailnum]);

}

close(DB);

my $text = qq|TEST $rec{'Location'},$rec{'Specialism'}|;

foreach my $email (@email){

open (SENDMAIL, "$mailprog") || &cgierr("unable to open mail program");

print SENDMAIL <<"EOF";

From:webmaster\@ehrecruitment.co.uk

To: <$email>

Subject: A Job for you

A job that matches has been found\n

$text

EOF

Close(SENDMAIL) or warn "sendmail didnt close nicely";

}

}


}

All seems fine to me with above no errors etc.

heres my add_success:

sub html_add_success {

# --------------------------------------------------------

# The page that is returned upon a successful addition to

# the database. You should use &get_record and &html_record

# to verify that the record was inserted properly and to make

# updating easier.

$page_title = "Record Added";

&html_page_top;

# < -- Start page text -- >

print qq|

<P><$font>The following record was successfully added to the database:</FONT>

|;

# < -- End page text -->

&html_record_long(&get_record($in{$db_key}));

&mail_to_jobseekers(&get_record($in{$db_key}));

&html_footer;

&html_page_bottom;

}

Just a thought should the field count start from 0 as in the cfg file or start from 1 ? Thanx again

JamesSmile

Quote Reply
Re: [jamesamorris] Selective Emailing of Records ? In reply to
The field code should start from 0. The field numbers should be exactly the same as in your cfg-file.

Caught two errors: replace the "||" in the condition "if (($line[$locationnum] eq $location) || ...." with "&&", and replace "<FILE>" with "<DB>".
kellner
Quote Reply
Re: [kellner] Selective Emailing of Records ? In reply to
Made Said Changes still doesnt seem to want to work i will perservere and c if i can get it to work.



Thanks Again

James
Quote Reply
Re: [jamesamorris] Selective Emailing of Records ? In reply to
OK, do you get an error message or does just nothing happen at all?

In the latter case, I suggest checking whether variables are available where they should be, by printing them to a text file. Add this line before you call the sub in html_add_success:

open (TEST, ">testing.txt") || die ("can't write to testing.txt: $!");

and then just print the individual values to TEST wherever you want to check their availability:

print TEST "$locationnum\n";

...
foreach my $key (keys %rec) { print TEST "$key: $rec{$key}\n";}


and so on.
kellner
Quote Reply
Re: [kellner] Selective Emailing of Records ? In reply to
Hi again,

Got it to work !

But as there is always a but for some bizarre reason it will only send emails to my hotmail account when a job is added and not to any of the other emails i have in the Job agent file. Maybe just my ISP bit slow today. Secondly and more importantly it comes up with an error when a job ad is successfully added i have attached final code and error below



Error Message : fatal error: Undefined subroutine &main::Close called at /home/ehrecrui/public_html/cgi-bin/dbases/db.cgi line 1654.

Script Location : /home/ehrecrui/public_html/cgi-bin/dbases/db.cgi
Perl Version : 5.006001
Setup File : jobs.cfg
User ID : iehclimited
Session ID : iehclimited.101491331883855

Heres the final code:

In Jobs .cfg file i added :

# Full Path and File name of the job agent file.

$db_file_mail = $db_script_path . "/mail.db";

In DB.cgi is as follows :

sub mail_to_jobseekers {

my (%rec) = &get_record($in{$db_key});



my $location = $rec{'Location'};

my $specialism = $rec{'Specialism'};

my $emailnum = 2;

my $locationnum = 4;

my $specialismnum = 3;

my (@email);

open(DB,"<$db_file_mail")||&cgierr("cant open jobmail file:$!");

while (my $line = <DB>){

chomp($line);

my @line = &split_decode($line);

if(($line[$locationnum] eq $location)&&($line[$specialismnum] eq $specialism)){

push (@email,$line[$emailnum]);

}

close(DB);

my $text = qq|TEST $rec{'Location'},$rec{'Specialism'}|;

foreach my $email (@email){

open (SENDMAIL, "$mailprog") || &cgierr("unable to open mail program");

print SENDMAIL <<"EOF";

From:webmaster\@ehrecruitment.co.uk

To: <$email>

Subject: A Job for you

A job that matches has been found\n

$text

EOF

Close(SENDMAIL) or warn "sendmail didnt close nicely";

}

}

}

Finally in the jobs_html_pl

in add_success

&mail_to_jobseekers;

I cant understand why it will only kick an error when i add a record that matches the job agent file that uses a hotmail address and only sends to this record. It is definitely an intermittent thing, could it be caused by sendmail not closing or some other process not closing properly perhaps ?

But it works, just need to get rid of error.

Thanks again for your help



JamesSmile

Quote Reply
Re: [jamesamorris] Selective Emailing of Records ? In reply to
Close(SENDMAIL)

needs to be:

close(SENDMAIL)
Quote Reply
Re: [RedRum] Selective Emailing of Records ? In reply to
Wot a silly boy I am changed the close to lower case seems to have cured the error message but for some reason the script will only recognise .com addresses an extract from my dbase is below: Column 0 is user name,1 is date created,2 is email address , 3 is job specialism, 4 is location. When i add a job to the jobs database with the criteria Food Safety, South East it sends an email to jameaamorris99@hotmail.com which is correct. When i add Food Safety, North West it wont send to jamesmorris@ehrecruitment.co.uk or tonym@iehc.demon.co.uk. It appears to not like the .co.uk domains.

Would i be better replacing the EOF statement with a simple print mail statement as my other scripts accept .co.uk addresses ok.

peterpan|27-Feb-2002|jameaamorris99@hotmail.com|Food Safety|South East|James|Morris
jamorris|27-Feb-2002|jamesmorris@ehrecruitment.co.uk|Food Safety|North West|James|Morris
tonymorris|28-Feb-2002|tonym@iehc.demon.co.uk|Trading Standards|Scotland|tony|morris

Any thoughts would be gratefully received.
Quote Reply
Re: [kellner] Selective Emailing of Records ? In reply to
All is working fine now, It seems to only search the first line of the job agent database for a match rather than the whole file. I originally thought it was the format of email address causing problems but it now seems that if i move the position around in the file it still only searches the first record. Should their be something like a for next loop (excuse my language im new to perl) to search all records and send email to all those that match.

And on another note do you think it would be complicated to allow the user to leave

field blank i.e if they want all in a specific location they select location and leave discipline blank and vice versa.

Apologies for being a pain!

Thanx again

JAmes
Quote Reply
Re: [jamesamorris] Selective Emailing of Records ? In reply to
Sorry, there's another error (this was really sloppy code - apologies!): before the line "close(DB)", there should be another closing curly bracket. It might be that this was really the one that was missing in the first place, and which made you add one to the end of the subroutine. Therefore, it's possible that you have to remove one from the end of the sub. - This should solve the "only searches first line of file" problem.

As for the blank fields, this would require some thinking.

Do you mean if there's only a location or only a discipline in the job agent database, and a record is added to the job database which matches either of the two, mail the user, but also mail users who have entered both location and discipline if the record matches both?

I'm too tired right now, but I'm sure there's a way to do this. RedRum to the rescue :-)
kellner
Quote Reply
Re: [kellner] Selective Emailing of Records ? In reply to
Kellner, You are a star thank you very much it works a treat, will test it a lot before going live but seems to work no problem. With regards to the other question just a thought ?

At present when the user registers for job agent they have to choose a 'location' and a 'specialism' I just wondered how difficult it would be to adapt the code to allow them to leave one field blank.

i.e Profile1 Food Safety - London will send food safety in london jobs

i.e Profile2 BLANK - London will send jobs in london but any discipline.

i.e Profile3 Food Safety - BLANK will send jobs in food safety any location.

Thanx again for your help!

JamesSmile

HERE IS THE FINAL WORKING CODE - THANX KELLNER

In Jobs Database .cfg File

# Full Path and File name of the Job Agent database file.

$db_file_mail = $db_script_path . "/mail.db";

In DB. CGI New Sub

sub mail_to_jobseekers {

my (%rec) = &get_record($in{$db_key});

my $location = $rec{'Location'};

my $specialism = $rec{'Specialism'};

my $emailnum = 2;

my $locationnum = 4;

my $specialismnum = 3;

my (@email);

open(DB,"<$db_file_mail")||&cgierr("cant open jobmail file:$!");

while (my $line = <DB>){

chomp($line);

my @line = &split_decode($line);

if(($line[$locationnum] eq $location)&&($line[$specialismnum] eq $specialism)){



push (@email,$line[$emailnum]);

}

}

close(DB);

my $text = qq|TEST $rec{'Location'},$rec{'Specialism'}|;

foreach my $email (@email){



open (SENDMAIL, "$mailprog") || &cgierr("unable to open mail program");

print SENDMAIL <<"EOF";

From:webmaster\@ehrecruitment.co.uk

To: $email

Subject: A Job for you

A job that matches has been found\n

$text

EOF

close(SENDMAIL) or warn "sendmail didnt close nicely";

}


}

And to call routine after Job ad successfully posted:(Using Short Long Mod)

Sub html add Success

After:

&html_record_long(&get_record($in{$db_key}));

Place:

&mail_to_jobseekers(&get_record($in{$db_key}));

Works a Treat!

Last edited by:

jamesamorris: Mar 1, 2002, 6:12 AM
Quote Reply
Re: [jamesamorris] Selective Emailing of Records ? In reply to
Great that this works. Here's some code that would fulfil your requirements for the blank fields, but it's a bit clumsy. Just paste it into the subroutine instead of the conditional there.

if ((!$line[$locationnum]) && ($line[$specialismnum] eq $specialism)) { # location empty, specialism matches

push (@email, $line[$emailnum]);}
elsif ((!$line[$specialismnum]) && $line[$locationnum] eq $location) { # specialism empty, location matches

push (@email, $line[$emailnum]);}
elsif (($line[$specialismnum] eq $specialism) && ($line[$locationnum] eq $location)) { # specialism and location match
push (@email, $line[$emailnum]);}
kellner
Quote Reply
Re: [kellner] Selective Emailing of Records ? In reply to
Thanx Kellner Works a Treat!!
Quote Reply
Re: [kellner] Selective Emailing of Records ? In reply to
In Reply To:
Great that this works. Here's some code that would fulfil your requirements for the blank fields, but it's a bit clumsy. Just paste it into the subroutine instead of the conditional there.

if ((!$line[$locationnum]) && ($line[$specialismnum] eq $specialism)) { # location empty, specialism matches

push (@email, $line[$emailnum]);}
elsif ((!$line[$specialismnum]) && $line[$locationnum] eq $location) { # specialism empty, location matches

push (@email, $line[$emailnum]);}
elsif (($line[$specialismnum] eq $specialism) && ($line[$locationnum] eq $location)) { # specialism and location match
push (@email, $line[$emailnum]);}


Just one more thought before i finish with this subject if a user left both criteria fields blank and wanted to receive all Jobs would i add a line like this ?

elsif ((!$line[$specialismnum]) && $line[$locationnum] ) { # both empty
push (@email, $line[$emailnum]);}


Regards James

Thank you very much for your help with this!