I’ve been working on my website for a week straight and finally finalizing the scripts. I have a fully working contact script, which I got a template from online, I’m trying to convert it into a Tell-A-Friend script where it asks for the users input for which email address to send to instead of sending to my hard-coded email address.
This is my code:
In my HTML: [php]<?php include "tyf-form.php"; ?>[/php]
tyf-form.php:
[php]<?php
if (session_id() == ‘’) session_start();
require “formfiles/contact-config.php”;
$error_tyfmessage = ‘’;
if (!isset($_POST[‘submit’])) {
showForm();
} else { //form submitted
$error = 0;
if(!empty($_POST[‘name’])) {
$name[2] = clean_var($_POST[‘name’]);
}
else {
$error = 1;
$name[3] = ‘color:#FF0000;’;
}
if(!empty($_POST[‘email’])) {
$email[2] = clean_var($_POST[‘email’]);
if (!validEmail($email[2])) {
$error = 1;
$email[3] = ‘color:#FF0000;’;
$email[4] = ‘Invalid email’;
}
}
else {
$error = 1;
$email[3] = ‘color:#FF0000;’;
}
if(!empty($_POST[‘tyfsubject’])) {
$tyfsubject[2] = clean_var($_POST[‘tyfsubject’]);
if (function_exists(‘htmlspecialchars’)) $tyfsubject[2] = htmlspecialchars($tyfsubject[2], ENT_QUOTES);
}
else {
$error = 1;
$tyfsubject[3] = ‘color:#FF0000;’;
}
if(!empty($_POST[‘tyfmessage’])) {
$tyfmessage[2] = clean_var($_POST[‘tyfmessage’]);
if (function_exists(‘htmlspecialchars’)) $tyfmessage[2] = htmlspecialchars($tyfmessage[2], ENT_QUOTES);
}
else {
$error = 1;
$tyfmessage[3] = ‘color:#FF0000;’;
}
if(empty($_POST[‘captcha_code’])) {
$error = 1;
$code[3] = ‘color:#FF0000;’;
} else {
include_once “formfiles/contact-securimage.php”;
$securimage = new Securimage();
$valid = $securimage->check($_POST[‘captcha_code’]);
if(!$valid) {
$error = 1;
$code[3] = 'color:#FF0000;';
$code[4] = '<strong><span style="color:#FF0000;">Incorrect code</span></strong>';
}
}
if ($error == 1) {
$error_tyfmessage = ‘Please correct/enter field(s) in red.’;
showForm();
} else {
if (function_exists('htmlspecialchars_decode')) $tyfsubject[2] = htmlspecialchars_decode($tyfsubject[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $tyfmessage[2] = htmlspecialchars_decode($tyfmessage[2], ENT_QUOTES);
$body = "$name[0]: $name[2]\r\n\r\n";
$body .= "$email[0]: $email[2]\r\n\r\n";
$body .= "$tyfmessage[0]:\r\n$tyfmessage[2]\r\n";
if (!$from) $from_value = $email[2];
else $from_value = $from;
require_once('formfiles/class.phpmailer.php');
$mail = new PHPMailer();
$mail->SetFrom($from_value);
$mail->AddReplyTo($email[2]);
$mail->tyfsubject = "$tyfsubject_prefix - $tyfsubject[2]";
$mail->Body = $body;
$mail->AddAddress($f_email);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
if (!$thank_you_url) {
if ($use_header_footer) include $header_file;
echo '<a name="cform"><!--Form--></a>'."\n";
echo '<div id="formContainer" style="width:'.$form_width.';height:'.$form_height.';text-align:left; vertical-align:top;">'."\n";
echo $GLOBALS['thank_you_tyfmessage']."\n";
echo '</div>'."\n";
if ($use_header_footer) include $footer_file;
}
else {
header("Location: $thank_you_url");
}
session_unset();
session_destroy();
}
} //else submitted
function showForm()
{
global $name, $y_email, $f_email, $tyfsubject, $tyfmessage, $code;
global $where_included, $use_header_footer, $header_file, $footer_file;
global $form_width, $form_height, $form_background, $form_border_color, $form_border_width, $form_border_style, $cell_padding, $left_col_width;
if ($use_header_footer) include $header_file;
echo $GLOBALS[‘error_tyfmessage’];
{$y_email[0]} | {$y_email[4]} |
{$f_email[0]} | {$f_email[4]} |
{$tyfsubject[0]} | |
{$tyfmessage[0]} | {$tyfmessage[2]} |
{$code[0]} | {$code[4]}
Please enter the text in the image above. Click here if you cannot recognize the code. |
All fields are required. | |
if ($use_header_footer) include $footer_file;
}
function clean_var($variable) {
$variable = strip_tags(stripslashes(trim(rtrim($variable))));
return $variable;
}
/**
Email validation function. Thanks to http://www.linuxjournal.com/article/9585
/
function valid_yEmail($y_email)
{
$isValid = true;
$atIndex = strrpos($y_email, “@”);
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($y_email, $atIndex+1);
$local = substr($y_email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == ‘.’ || $local[$localLen-1] == ‘.’)
{
// local part starts or ends with ‘.’
$isValid = false;
}
else if (preg_match(’/\.\./’, $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match(’/^[A-Za-z0-9\-\.]+$/’, $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match(’/\.\./’, $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if (!preg_match(’/^(\\.|[A-Za-z0-9!#%&`_=\/$’+?^{}|~.-])+$/’, str_replace("\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match(’/^"(\\"|[^"])+"$/’,
str_replace("\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && function_exists(‘checkdnsrr’))
{
if (!(checkdnsrr($domain,“MX”) || checkdnsrr($domain,“A”))) {
// domain not found in DNS
$isValid = false;
}
}
}
return $isValid;
}
function valid_fEmail($f_email)
{
$isValid = true;
$atIndex = strrpos($f_email, “@”);
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($f_email, $atIndex+1);
$local = substr($f_email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == ‘.’ || $local[$localLen-1] == ‘.’)
{
// local part starts or ends with ‘.’
$isValid = false;
}
else if (preg_match(’/\.\./’, $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match(’/^[A-Za-z0-9\-\.]+$/’, $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match(’/\.\./’, $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if (!preg_match(’/^(\\.|[A-Za-z0-9!#%&`_=\/$’*+?^{}|~.-])+$/’, str_replace("\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match(’/^"(\\"|[^"])+"$/’,
str_replace("\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && function_exists(‘checkdnsrr’))
{
if (!(checkdnsrr($domain,“MX”) || checkdnsrr($domain,“A”))) {
// domain not found in DNS
$isValid = false;
}
}
}
return $isValid;
}
?>[/php]
And the contact-config.php:
[php]<?php
/* Email address where the messages should be delivered */
$to = ‘[email protected]’;
/* This will be appended to the subject of contact form message */
$subject_prefix = ‘YCN Contact Form’;
/* Name of the file where you are including the contact form */
$where_included = ‘tellyourfriend.php’;
/*******************************************************************************
- OPTIONAL
*******************************************************************************/
/* From email address, in case your server prohibits sending emails from
- addresses other than those of your own domain (e.g. [email protected]). */
$from = ‘’;
/* Whether to use header/footer files? If yes, then set to TRUE */
$use_header_footer = FALSE;
/* Form header file */
$header_file = ‘formfiles/contact-header.php’;
/* Form footer file */
$footer_file = ‘formfiles/contact-footer.php’;
/* Thank you message to be displayed after the form is submitted. Can include
- HTML tags. Write your message between and */
$thank_you_message = <<<EOD
Your message has been successfully sent, if needed, we will get back to you as soon as possible.
EOD;/* URL to be redirected to after the form is submitted. If this is specified,
- then the above message will not be shown and user will be redirected to this
- page after the form is submitted. */
$thank_you_url = ‘’;
/*******************************************************************************
- COSMETICS
*******************************************************************************/
/* Form width in px or % value */
$form_width = ‘70%’;
/* Form height in px */
$form_height = ‘500px’;
/* Form background color or image. Value can contain just a color value or
- complete background shorthand property (with background image). */
$form_background = ‘#F7F8F7’;
/* Form border color */
$form_border_color = ‘#CCCCCC’;
/* Form border width */
$form_border_width = ‘0px’;
/* Form border style. Examples - dotted, dashed, solid, double */
$form_border_style = ‘solid’;
/* Form cell padding */
$cell_padding = ‘5px’;
/* Form left column width */
$left_col_width = ‘25%’;
/*******************************************************************************
- Do not change anything below, unless of course you know very well
- what you are doing
*******************************************************************************/
$name = array(‘Name’,‘name’,NULL,NULL);
$email = array(‘Email’,‘email’,NULL,NULL,NULL);
$y_email = array(‘Your Email’, ‘y_email’,NULL,NULL,NULL);
$f_email = array(‘Friends Email’, ‘f_email’,NULL,NULL,NULL);
$subject = array(‘Subject’,‘subject’,NULL,NULL);
$tyfsubject = array(‘Subject’,‘subject’,“One of your friends thought you would appreciate our services at YourCollegeNerd.com. Check us out!”,NULL);
$message = array(‘Message’,‘message’,NULL,NULL);
$tyfmessage = array(‘Message’,‘message’,“Short message to save room for post.”, NULL);
$code = array(‘Code’,‘captcha_code’,NULL,NULL,NULL);
?>[/php]
Here is a visual copy of both:
http://yourcollegenerd.com/contact.php <-- Fully functional contact form
http://yourcollegenerd.com/tellyourfriends.php <-- The look is finalized, just need help to get the script fully converted.
I took a year of programming in College, so PHP looks similar to C++, in the contact-config.php page, I made the $y_email, $f_email, $tyfsubject, and $tyfmessage variables, which work.
My major problem is I have limited understanding on which part of the code actually does the sending, also another part that is confusing me is instead of using the hardcoded $to variable for an address to send to, I need to use $f_email to be validated and then used as the address to send to.
Been staring at this code for atleast 5 hours, really hope someone can help