Posted June 11 '09 at 03:58 PM
Posts: 48
There are a couple ways to accomplish this. First of all, if you're storing the email address as they sign up and you want to send them an email thanking them for doing so, you don't need to retrieve it from the database since you already have the email address on hand, probably in a $_POST variable. Then just send them a thank you note, like this:
// check for the required fields first
$email = $_POST['email'];
$subject = "Thank you for registering";
if (empty($email)) {
header("Location: /error.php");
exit();
}
$message = "Thank you for subscribing to our site....
Thank you
WebMaster, www.nowhere.com
This is an automated response, please do not reply!";
if (@mail($mailto, $subject, $message, "From: webmaster<me@nowhwere.com>\nX-Mailer: PHP/" . phpversion())) {
header("Location: thank-you.html");
exit();
} else {
// This echo's the error message if the email did not send.
// You could change the text in between the <p> tags.
echo('<p>Mail could not be sent. Please use your back button to try again.</p>');
}
?>
This is very basic, no real error checking. You should do email address validation and such, but you should get the idea from this pretty easily.
Now, if you want to email them after-the-fact, say at a later date, you would do something like this:
<?php
$message = "Thank you for subscribing to our site....
Thank you
WebMaster, www.nowhere.com
This is an automated response, please do not reply!";
$subject = "Thank you for registering";
//run a query to retrieve info from the table
$result = mysql_query('SELECT * from users');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// Use result
while ($row = mysql_fetch_assoc($result)) {
@mail($row['email'];, $subject, $message, "From: webmaster<me@nowhwere.com>\nX-Mailer: PHP/" . phpversion());
}
// clean up after ourselves
mysql_free_result($result);
?>
Again, this is pretty simplistic and doesn't do a lot of the error checking and stuff that it should, but it should be enough to point you in the right direction. Obviously, you want to substitute your own message and subject and your database fields may be different, but you get the idea.
Hope this helps.