Multiple autoreplies

Hi!

I am trying to configure the basic upload script so that it sends out two separate autoreplies: one to us with the attachment, and one to the person who filled out the form confirming receipt of the attachment. I thought I’d scripted it properly, but the second (confirmation) autoreply does not send. Can anyone tell me where I screwed up?


	// remove escape characters from POST array
	if (PHP_VERSION < 6 && get_magic_quotes_gpc()) {
	  function stripslashes_deep($value) {
		$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
		return $value;
		}
	  $_POST = array_map('stripslashes_deep', $_POST);
	  }

	// List the expected fields
	$expected = array('to', 'from', 'subject', 'sender', 'email', 'cover', 'fileatt');
	// Set the required fields
	$required = array('to',  'from', 'sender', 'email', 'cover', 'fileatt');
	// Create an empty array for any missing fields
	$missing = array();
	
	//assume that there is nothing suspect
	$suspect = false;
	//Create a pattern to locate suspect phrases
	$pattern = '/Content-Type:|Bcc:|CC:/i';
	// function to check for suspect phrases
	function isSuspect($val, $pattern, $suspect) {
	// if the variable is an array, loop through each element
	// and pass it recursively back to the same function
	if (is_array($val)) {
	  foreach ($val as $item) {
		isSuspect($item, $pattern, $suspect);
		}
	  }
	else {
	  // if one of the suspect phrases is found, set Boolean to true
	  if (preg_match($pattern, $val)) {
		$suspect = true;
		}
	  }
	}
	
  // check the $_POST array and any subarrays for suspect content
  isSuspect($_POST, $pattern, $suspect);

  if ($suspect) {
    $mailSent = false;
    unset($missing);
  } else {
    // process the $_POST variables
    foreach ($_POST as $key => $value) {
      // assign to temporary variable and strip whitespace if not an array
      $temp = is_array($value) ? $value : trim($value);
      // if empty and required, add to $missing array
      if (empty($temp) && in_array($key, $required)) {
        array_push($missing, $key);
      } elseif (in_array($key, $expected)) {
	    // otherwise, assign to a variable of the same name as $key
        ${$key} = $temp;
      }
    }
  }
  
  // validate the email address
  if (!empty($email)) {
    // regex to identify illegal characters in email address
    $checkEmail = '/^[^@]+@[^\\s\\r\
\\'";,@%]+$/';
	// reject the email address if it doesn't match
    if (!preg_match($checkEmail, $email)) {
      $suspect = true;
      $mailSent = false;
      unset($missing);
    }
  }
  
  // go ahead only if not suspect and all required fields OK
  if (!$suspect && empty($missing)) {
	
	// Obtain file upload vars
	$fileatt      = $_FILES['fileatt']['tmp_name'];
	$fileatt_type = $_FILES['fileatt']['type'];
	$fileatt_name = $_FILES['fileatt']['name'];
	
	$headers = "From: $from";
	$body =    "Great News!!\\r\
\\r\
$sender has submitted a resume for our open position of: $subject.\\r\
\\r\
Here is the cover letter:\\r\
\\r\
" . $cover . "\\r\
\\r\
";
	$body = wordwrap($body, 70);
	$headers2 = "From: noreply@foo.bar";
	$autoreply = "Thank you for submitting your resume and expressing interest in Foobar!\\r\
\\r\
";
	$autoreply .= "Due to the volume of applications that we receive, it is not possible to respond to all applicants individually. Successful candidates will be contacted within two weeks of application through the contact information you provided. However, if you are qualified and the position you are applying for has been filled up, we will keep you in our active list for three months and notify you when the need for the position arises. If you do not hear from us within a week, your application was not selected for the position.\\r\
\\r\
";
	$autoreply .= "We wish you every personal and professional success with your job search and in the future.\\r\
\\r\
";
	$autoreply .= "Thank you, again, for your interest in our company.\\r\
\\r\
";
	$autoreply .= "Foobar\\r\
Human Resources Department";
	
	if (is_uploaded_file($fileatt)) {
	  // Read the file to be attached ('rb' = read binary)
	  $file = fopen($fileatt,'rb');
	  $data = fread($file,filesize($fileatt));
	  fclose($file);
	
	  // Generate a boundary string
	  $semi_rand = md5(time());
	  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
	  
	  // Add the headers for a file attachment
	  $headers .= "\
MIME-Version: 1.0\
" .
				  "Content-Type: multipart/mixed;\
" .
				  " boundary=\\"{$mime_boundary}\\"";
	
	  // Add a multipart boundary above the plain message
	  $message = "This is a multi-part message in MIME format.\\r\
\\r\
" .
				 "--{$mime_boundary}\
" .
				 "Content-Type: text/plain; charset=\\"iso-8859-1\\"\\r\
" .
				 "Content-Transfer-Encoding: 7bit\\r\
\\r\
" . 
				 "Great News!!\\r\
\\r\
$sender has submitted a resume for our open position of: $subject.\\r\
\\r\
Here is the cover letter:\\r\
\\r\
" . $cover . "\\r\
\\r\
";
	
	  // Base64 encode the file data
	  $data = chunk_split(base64_encode($data));
	
	  // Add file attachment to the message
	  $message .= "--{$mime_boundary}\
" .
				  "Content-Type: {$fileatt_type};\\r\
" .
				  " name=\\"{$fileatt_name}\\"\\r\
" .
				  "Content-Disposition: attachment;\\r\
" .
				  " filename=\\"{$fileatt_name}\\"\\r\
" .
				  "Content-Transfer-Encoding: base64\\r\
\\r\
" .
				  $data . "\
\
" .
				  "--{$mime_boundary}--\\r\
";
				  
	}
	// Send the message
	if ($suspect) {
		$ok = false;
		unset ($missing);
	}
	else {
	if (!$suspect && empty($missing)) {
		$ok = mail($to, $subject, $message, $headers);
		if ($ok) {
			// redirect to the "Thank You" page and send the autoreply
			header('Location:rsm_thanks.php');
			$reply  = mail($sender, $subject, $autoreply, $from);
			unset($missing);
		} 
	}
	}
  }


Try sending the mail first and then redirect?

I moved the $reply line ahead of the header() line-- no change.
I moved it completely out of the “if($ok)” loop, up a level-- still no change.

Ok, so something in the parameters might be wrong. Try echoing them and see what they contain.

Not sure I quite understand what you are suggesting. But here is what I did do:

I replaced:

header(‘Location:rsm_thanks.php’);
$reply = mail($sender, $subject, $autoreply, $from);

with

echo $sender, $subject, $autoreply, $from;

It printed across the top of the page. It did show me that I used one wrong parameter: $sender should have been $email–replaced that, but still no go!

It seems that the mail function is not working–

But it works for the other mail you send, just a couple of lines of code earlier?
What’s the difference between the two? Just the receiver email address? Then maybe it’s getting caught in the spam filter?

Well, I used my work email address in the script and my home email address in the form. I checked the spam folder of my home email address and the confirmation was not there–not even a day later.

Before I go asking my sysadmin to start checking the smtp server-- y’all are sure that my code is legit?

Anyway–

I just put an autoresponder in my website’s control panel. I am beginning to think that the mail() function can only be instantiated once.

To test your theory, why not comment out the first mail() instance and see if it sends to the second?

If you can’t instantiate the mail() function twice (which seems odd), try putting the reply in rsm_thanks.php and see if it works, OR you could look into Swift Mailer and use SMTP.

I also noticed $from isn’t set anywhere. That could be causing your problem. of course, as guido said, put the mail function before the redirect or it won’t work. :slight_smile: