if ($nav->action == "newsletter_subscribe") {
$req = new Request(array("first", "last", "email"));
if (empty($req->email) || (empty($req->first) && empty($req->last))) {
$feedback->error("Either your name or email address was left blank. <a href=\"$SITE_URL/?page=newsletter&action=subscribe\">Click here to try again.</a>");
header("Location: $SITE_URL/?page=feedback");
exit;
}
$email = strtolower($req->email);
$existing = get_newsletter_subscriber_by_email($database, $email);
if ($existing == false) {
$date = date("Y-m-d");
$sql = "INSERT INTO newsletter_subscribers SET
first='$req->first',
last='$req->last',
email='$email',
date_joined='$date'
";
if (! $database->insert($sql) ) {
$feedback->error("An error was encountered while submitting your subscription request.");
} else {
$msg = "Thank you for requesting the Food For Life Newsletter.
You will receive the first issue via email shortly.
Just as a reminder, we will not sell, give or otherwise disseminate your information to any source. To ensure that you continue to receive emails from us, add newsletter@foodforlife.com to your address book today.
If you have any questions please do not hesitate to contact us. If you would like specific questions answered or addressed in the newsletter please let me know.
Nichole Hastings
Food For Life News
Food For Life Baking Company";
$to_name = "$req->first $req->last";
$subject = "Newsletter Subscribe Confirmation";
$from_email = "info@foodforlife.com";
$from_name = "Food For Life";
if (! send_email_notification($req->email, $to_name, $subject, $msg, false, $from_email, $from_name) ) {
$feedback->error("Error sending email. Cannot continue.");
}
}
}
if (! $feedback->waserror()) {
$feedback->conf("<h3>Thank you</h3><br /><br />Your friends at Food For Life");
}
header("Location: $SITE_URL/?page=feedback");
exit;
}
elseif ($nav->action == "initiate_unsub") {
$req = new Request(array("email"));
$subscriber = get_newsletter_subscriber_by_email($database, $req->email);
if ($subscriber == -1) {
/* pretend like it worked */
$feedback->conf("Your request has been received but you are not yet done! A confirmation email has been sent to the address provided. Please check your inbox for the email (which should be arriving shortly) and click the link it contains to complete the unsubscribe process.");
} else {
$code = verify_code($req->email);
$message = "Greetings,
Someone (hopefully you) has submitted a request to unsubscribe your email address from the Food For Life newsletter. If you would like to continue unsubscribing from the newsletter, please click the link below to complete the process:
$SITE_URL/process.php?action=complete_unsub&id=$subscriber->id&code=$code
";
$to_name = "$subscriber->first $subscriber->last";
$subject = "Newsletter Unsubscribe Confirmation";
$from_email = "info@foodforlife.com";
$from_name = "Food For Life";
if (! send_email_notification($req->email, $to_name, $subject, $message, false, $from_email, $from_name) ) {
$feedback->error("Error sending email. Cannot continue.");
} else {
$feedback->conf("Your request has been received but you are not yet done! A confirmation email has been sent to the address provided. Please check your inbox for the email (which should be arriving shortly) and click the link it contains to complete the unsubscribe process.");
}
header("Location: $SITE_URL/?page=feedback");
exit;
}
}
elseif ($nav->action == "complete_unsub") {
$req = new Request(array("id", "code"));
$subscriber = get_newsletter_subscriber($database, $req->id);
if ($req->code != verify_code($subscriber->email)) {
$feedback->error("Problem in unsubscription process (0), cannot continue.");
} else {
if ($subscriber == -1) {
$feedback->error("Problem in unsubscription process (1), cannot continue.");
} else {
$sql = "DELETE FROM newsletter_subscribers WHERE id='$subscriber->id'";
if (! $database->delete($sql) ) {
$feedback->error("Problem in unsubscription process (2), cannot continue.");
}
}
}
if (! $feedback->waserror() ) {
$feedback->conf("You have been successfully unsubscribed from the newsletter.");
}
header("Location: $SITE_URL/?page=feedback");
exit;
}