Forum Moderators: coopster

Message Too Old, No Replies

Help with mail function

fetching email from mysql

         

adammc

1:11 am on Aug 23, 2005 (gmt 0)

10+ Year Member




Hi Folks,

Im in the processs of trying to create a Job Alerts function for my job search site and need some help if possible.

How it works is :

Jobseeker goes to the job alerts page, selects a category that he wishes to be emailed about whenever jobs are posted within that category. Each individual category (job alert) is stored in a database. Info stored is jobseekers username, email and the category selected.

Ok...

So when an employer posts a job, I want the script to then email out all jobseekers who have created an alert for the category that the job was posted into.

I have created the script that jobseekers use to add their job alerts. My problme is getting the email sent out when an employer posts the job. it just wont email.

Im sure its not finding the email variable / email row in the table.

Tables are :
job_post (holds all the employers job info, when they submit a job)

jobalert (this is where the jobseekers job alert info is stored)
uname - jobcategory - email

Heres the form / page that employers use to post their job, you will notice the email function that I have tried to create to no avail, it begins with this line:


$q = "select email from jobalert where jobcategory = \"$JobCategory\"";

Heres the relevant part of the code:


<?

include_once "accesscontrol.php";
include_once "../configuration.inc.php";
$q1 = "select * from job_employer_info where ename = \"$ename\" ";
$r1 = mysql_query($q1) or die(mysql_error(Error5));
$a1 = mysql_fetch_array($r1);
if(!empty($a1[plan]))
{
$a11 = $a1[JP_number];

if(isset($submit) && $submit == 'Post this job')
{
$q2 = "select * from job_employer_info where ename = \"$ename\" ";
$r2 = mysql_query($q2) or die(mysql_error(Error1));
$a2 = mysql_fetch_array($r2);

if (is_array($JobCategory))
{
$JobStr = implode("," , $JobCategory);
}

$qc = "select job_id from job_post order by job_id desc";
$rc = mysql_query($qc) or die(mysql_error(Error2));
$ac = mysql_fetch_array($rc);
$job_id = $ac[0] + 1;

$position = strip_tags($position);
$description = strip_tags($description);

$timezone = +10;

$EXday = date('d', mktime(0,0,0,0, date(d) + $_POST[exdays1], 0));
$EXmonth = date('m', mktime(0,0,0, date(m), date(d) + $_POST[exdays1], 0));
$EXyear = date('Y', mktime(0,0,0,date(m) ,date(d) + $_POST[exdays1], date(Y)));

$q3 = "insert into job_post set
job_id = \"$job_id\",
ename = \"$ename\",
CompanyCountry = \"$a2[CompanyCountry]\",
JobIn = \"$JobIn\",
CompanyState = \"$a2[CompanyState]\",
Company = \"$a2[CompanyName]\",
position = \"$position\",
employment_type = \"$employment_type\",
JobCategory = \"$JobStr\",
description = \"$description\",
availableto = \"$availableto\",
j_target = \"$j_target\",
postdate = \"$postdate\",
s_period = \"$s_period\",
EXmonth = \"$EXmonth\",
EXday = \"$EXday\",
EXyear = \"$EXyear\" ";
$r3 = mysql_query($q3) or die(mysql_error(Error3));

$a11 = $a11 - 1;
$q4 = "update job_employer_info set JP_number = \"$a11\" where ename = \"$ename\" ";
$r4 = mysql_query($q4) or die(mysql_error(Error4));

$q = "select email from jobalert where jobcategory = \"$JobCategory\"";
$r = mysql_query($q) or die(mysql_error());
$a = mysql_fetch_array($r);

$to = $a2[email];
$subject = "Job Alert Test";
$message = "Job Alert Test";
$from = "From: $contactemail";

mail($to, $subject, $message, $from);

echo "
<table width='100%' border='0' cellspacing='1' cellpadding='5'>
<tr>
<td height='16' bgcolor='#FFCC00'><center>
<b><font color='#000000'>$POSTJJ</font></b>
</center></td>
</tr>
</table><br><br><br>
";

}

if ($a11 < 1)
{
echo "<center><br><br><br>$POSTJ2</center><br><br>";
include_once "employers1.php";
}

else
{

echo "";
?>

<table width=100%>
<tr>

<tr>

<tr><td colspan=2>

<div align='left'><font face='verdana' size='2'><strong><br>
Post Your Job Vacancy</strong></font>
<hr size='1' color='#0469D5'><br>
</div>
</td></tr>
<td width="24%"><form action=<?=$PHP_SELF?> method=post name=form onSubmit="return checkFields();">

I would REALLY appreciate some help with this, I know I am so close to doing this whole thing myself.
I have only been doing PHP for 2 months now. Really want to nail this one ;)

Aleister

10:43 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



1) First off, I would use single quotes inside MySQL queries.

From my understanding, double quotes will only work if ANSI_QUOTES SQL mode is enabled. Having it enabled also requires that string literals must be written with single quotes. Double quotes will cause the server to treat the value as an identifier. I hope I got that right :)

So change this:

$q = "select email from jobalert where jobcategory = \"$JobCategory\"";

to this:

$q = "select email from jobalert where jobcategory = '$JobCategory'";

(Maybe I am mistaken, but it cannot hurt)

2) If you are unsure of the data being sent to the mail function, it is pretty easy to test it. You can simply echo the $to variable to the screen, and make sure it is what it should be. Then you will at least know if you are pulling the data out properly.

adammc

9:33 pm on Aug 24, 2005 (gmt 0)

10+ Year Member



Aleister,
thanks for the tips, I have taken your advice.

I added soem error reporting code to the maiol function and got this:

Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in /home/httpd/vhosts/workincairns.com/httpdocs/employers/PostJob-test.php on line 56

line 56 is:
$r3 = mysql_query($q3) or die(mysql_error(Error3));

Aleister

1:13 am on Aug 25, 2005 (gmt 0)

10+ Year Member



Try this:

$r3 = mysql_query($q3) or die(mysql_error());

The only parameter for mysql_error is the resource link (the variable for the database you originally set up. in other words, the variable the result of mysql_connect went into).

When not specified, it uses the last one opened - Just like mysql_query does.

Now if you were using multiple database connections, you would always reference it like this:

$db1 = mysql_connect("localhost", "user", "password");
$r3 = mysql_query($q3, $db1) or die(mysql_error($db1));

But since you probably just using one, you can do this:

$db1 = mysql_connect("localhost", "user", "password");
$r3 = mysql_query($q3) or die(mysql_error());

(The mysql_connect line is just an example, since I am not sure what yours looked like).

Know what I mean? :) Let me know how it works!

adammc

1:49 am on Aug 25, 2005 (gmt 0)

10+ Year Member



Aleister, thanks for your help. Error message has gone, it posted the job.

But I'm back to square one as its still not sending the email :(


<?

include_once "accesscontrol.php";
include_once "../configuration.inc.php";
$q1 = "select * from job_employer_info where ename = '$ename' ";
$r1 = mysql_query($q1) or die(mysql_error(Error5));
$a1 = mysql_fetch_array($r1);
if(!empty($a1[plan]))
{
$a11 = $a1[JP_number];

if(isset($submit) && $submit == 'Post this job')
{
$q2 = "select * from job_employer_info where ename = '$ename' ";
$r2 = mysql_query($q2) or die(mysql_error(Error1));
$a2 = mysql_fetch_array($r2);

if (is_array($JobCategory))
{
$JobStr = implode("," , $JobCategory);
}

$qc = "select job_id from job_post order by job_id desc";
$rc = mysql_query($qc) or die(mysql_error(Error2));
$ac = mysql_fetch_array($rc);
$job_id = $ac[0] + 1;

$position = strip_tags($position);
$description = strip_tags($description);

$timezone = +10;

$EXday = date('d', mktime(0,0,0,0, date(d) + $_POST[exdays1], 0));
$EXmonth = date('m', mktime(0,0,0, date(m), date(d) + $_POST[exdays1], 0));
$EXyear = date('Y', mktime(0,0,0,date(m) ,date(d) + $_POST[exdays1], date(Y)));

$q3 = "insert into job_post set
job_id = '$job_id',
ename = '$ename',
CompanyCountry = '$a2[CompanyCountry]',
JobIn = '$JobIn',
CompanyState = '$a2[CompanyState]',
Company = '$a2[CompanyName]',
position = '$position',
employment_type = '$employment_type',
JobCategory = '$JobStr',
description = '$description',
availableto = '$availableto',
j_target = '$j_target',
postdate = '$postdate',
s_period = '$s_period',
EXmonth = '$EXmonth',
EXday = '$EXday',
EXyear = '$EXyear' ";
$r3 = mysql_query($q3) or die(mysql_error());

$a11 = $a11 - 1;
$q4 = "update job_employer_info set JP_number = '$a11' where ename = '$ename' ";
$r4 = mysql_query($q4) or die(mysql_error(Error4));

$q = "select email from jobalert where jobcategory = '$JobCategory'";
$r = mysql_query($q) or die(mysql_error());
while($myrow = mysql_fetch_array($r))
{

$to = $myrow[email];
$subject = 'Job Alert Test';
$message = 'Job Alert Test';
$from = 'From: $contactemail';

mail($to, $subject, $message, $from);
}

Aleister

2:01 am on Aug 25, 2005 (gmt 0)

10+ Year Member



And what is the contents of the variables it is passing to the mail function? Can I see an example? (Are you echoing them out to see?)

adammc

2:21 am on Aug 25, 2005 (gmt 0)

10+ Year Member



If I understand you correctly...

It is passing the info stored in the jobalert table.

email - email@domain.com
jobcategory - hospitality jobs
uname - username

This info is added by the jobseekers when they signup for job alerts.

And, no I am not echoing the variables to see if its finding them, I wasnt sure how.

Aleister

3:06 am on Aug 25, 2005 (gmt 0)

10+ Year Member



$to = $a2[email];
$subject = "Job Alert Test";
$message = "Job Alert Test";
$from = "From: $contactemail";

echo "To: " . $a2[email] . "<br>";
echo "From: " . $contactemail . "<br>";

Just to verify that the proper data is being sent.

adammc

7:53 am on Aug 25, 2005 (gmt 0)

10+ Year Member



Hmm.. It wouldnt echo anything.


<?
include_once "accesscontrol.php";
include_once "../configuration.inc.php";
$q1 = "select * from job_employer_info where ename = '$ename' ";
$r1 = mysql_query($q1) or die(mysql_error(Error5));
$a1 = mysql_fetch_array($r1);
if(!empty($a1[plan]))
{
$a11 = $a1[JP_number];

if(isset($submit) && $submit == 'Post this job')
{
$q2 = "select * from job_employer_info where ename = '$ename' ";
$r2 = mysql_query($q2) or die(mysql_error(Error1));
$a2 = mysql_fetch_array($r2);

if (is_array($JobCategory))
{
$JobStr = implode("," , $JobCategory);
}

$qc = "select job_id from job_post order by job_id desc";
$rc = mysql_query($qc) or die(mysql_error(Error2));
$ac = mysql_fetch_array($rc);
$job_id = $ac[0] + 1;

$position = strip_tags($position);
$description = strip_tags($description);

$timezone = +10;

$EXday = date('d', mktime(0,0,0,0, date(d) + $_POST[exdays1], 0));
$EXmonth = date('m', mktime(0,0,0, date(m), date(d) + $_POST[exdays1], 0));
$EXyear = date('Y', mktime(0,0,0,date(m) ,date(d) + $_POST[exdays1], date(Y)));

$q3 = "insert into job_post set
job_id = '$job_id',
ename = '$ename',
CompanyCountry = '$a2[CompanyCountry]',
JobIn = '$JobIn',
CompanyState = '$a2[CompanyState]',
Company = '$a2[CompanyName]',
position = '$position',
employment_type = '$employment_type',
JobCategory = '$JobStr',
description = '$description',
availableto = '$availableto',
j_target = '$j_target',
postdate = '$postdate',
s_period = '$s_period',
EXmonth = '$EXmonth',
EXday = '$EXday',
EXyear = '$EXyear' ";
$r3 = mysql_query($q3) or die(mysql_error());

$a11 = $a11 - 1;
$q4 = "update job_employer_info set JP_number = '$a11' where ename = '$ename' ";
$r4 = mysql_query($q4) or die(mysql_error(Error4));

$q = "select email from jobalert where jobcategory = '$JobCategory'";
$r = mysql_query($q) or die(mysql_error());
while($myrow = mysql_fetch_array($r))
{

$to = $a2[email];
$subject = "Job Alert Test";
$message = "Job Alert Test";
$from = "From: $contactemail";

echo "To: " . $a2[email] . "<br>";
echo "From: " . $contactemail . "<br>";

mail($to, $subject, $message, $from);
}

echo "
<table width='100%' border='0' cellspacing='1' cellpadding='5'>
<tr>
<td height='16' bgcolor='#FFCC00'><center>
<b><font color='#000000'>$POSTJJ</font></b>
</center></td>
</tr>
</table><br><br><br>
";

}
if ($a11 < 1)
{
echo "<center><br><br><br>$POSTJ2</center><br><br>";
include_once "employers1.php";
}

else
{

echo "";
?>

<table width=100%>
<tr><tr>
<tr><td colspan=2>

<div align='left'><font face='verdana' size='2'><strong><br>
Post Your Job Vacancy</strong></font>
<hr size='1' color='#0469D5'><br>
</div>
</td></tr>
<td width="24%"><form action=<?=$PHP_SELF?> method=post name=form onSubmit="return checkFields();">

Aleister

4:38 pm on Aug 25, 2005 (gmt 0)

10+ Year Member



If it did not echo those values to the screen, then it sounds like the mail function is either getting empty variables, or that the section of code with the mail function in it is not being executed. I would start checking your queries (by outputting the results to the screen) to make sure they are getting the data you need. It is just a matter of going through your code to double-check everything.