Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using mail function 1

Status
Not open for further replies.

dugen

Programmer
Jun 16, 2003
59
US
Hi,

I recently set up an HR section on my website where people can apply for a job online and set up an interview time. I would now like to implement a script that will be executed once a day, that will remind anyone who has an interview set up for the next day. The reminder will be an email.

So I need there to be some kind of loop that will insert the email addresses from the database in the $to field one by one. Also there will be some personalization in the email such as the applicants name. So i need the applicant's names to correspond with the correct email address.

Here is the code i currently have: (i am new to php and have not used any arrays or looops yet, so i appreciate any help you can give. Thanks.)



<?php

//Connect To Database

//Linking to the database

$link = mysql_connect($host, $user, $password) or die (mysql_error());
Mysql_select_db("dmotion") or die(mysql_error());


//Query the database for email addresses
$query = mysql_query("SELECT emails, date, time, name FROM applicantInfo");

//execute query
mysql_query(($query) or die('Error, query failed'));


//HTML EMAIL auto responder

$to = $_REQUEST['txtemail'];
$subject = 'RE: GCB';
$message = '
<html>
<head>
<title>GCB</title>


</head>
<body><img src="

<font size="2" face="Geneva, Arial, Helvetica, sans-serif">

Thank you ['name'] for your interest in GCB Organizaiton!<p>

Please Remember your interview is scheduled for tomorrow at ['time'] <p>

We look forward to meeting with you! <p>

Sincerely,<br>
Your Friends at the GCB Organization
<p>
<img src="

</body>
</html>

';

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers

$headers .= 'From: GCB <Info@gcb.com>'. "\r\n";
mail($to, $subject, $message, $headers);

?>
 
First, make sure you take a look at the manual for each of these functions. It looks like you did for some (to figure out how to send HTML mail, for instance) but not for others. The examples and user notes there can really help to figure this stuff out. That said, here's a shot at getting this to work.

There are a few problems that stand out. For starters, you are not using the mysql_query() function correctly. You call it twice, but only assign the result to a variable the first time and then use that result resource as the text of the second query. That will probably cause a MySQL error, but even if it doesn't, you will not get the results you want. You need to modify your SQL syntax too. Try adding a WHERE clause so that you only get the results you want.

Change this code:

Code:
//Query the database for email addresses
$query = mysql_query("SELECT emails, date, time, name FROM applicantInfo");

//execute query
mysql_query(($query) or die('Error, query failed'));

It should look more like this:

Code:
//Set date paramater for tomorrow
$tomorrow = time() + (24 * 60 * 60);
$SQLtom = date("Y-m-d", $tomorrow);

//Query the database
$query = "SELECT emails, time, name FROM applicantInfo WHERE date = '" . $SQLtom . "'";
$result = mysql_query($query,$link) or die("Error, query failed");

You then need to iterate over the contents of the result set to create and send each e-mail using code like this:

Code:
while ($row = mysql_fetch_assoc($result))
{
  $to = $row['emails'];
  $subject = "RE: GCB";

  $msg = '
<html>
<head>
<title>GCB</title>
</head>
<body><img src="[URL unfurl="true"]http://www.GCB.com/email-header.jpg"><br><br>[/URL]

<font size="2" face="Geneva, Arial, Helvetica, sans-serif">
 
Thank you ' . $row['name'] . ' for your interest in GCB Organizaiton!<p>

Please Remember your interview is scheduled for tomorrow at ' . $row['time'] . ' <p>

We look forward to meeting with you! <p>

Sincerely,<br>
Your Friends at the GCB Organization
<p>
<img src="[URL unfurl="true"]http://www.gcb.com/25yrs-silverlogo.gif">[/URL]


</body>
</html>';

//Set headers for HTML mail
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

//Additional required headers
$headers .= 'From: GCB <Info@gcb.com>'. "\r\n";

//Send the message
mail($to, $subject, $msg, $headers);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top