Page 1 of 1

Issue with email code

Posted: Tue Oct 13, 2009 1:31 pm
by junestag
Hello,
I cannot figure out why my code is not working. I create a message, use a for loop to populate it with values from an array and then send it. It doesn't send the email at all. When I remove the for loop from the middle of the $message and put it at the top of the page it populates the variables just fine but then sends just he most recent value for $course and $dept. So what's the problem?

Here's the code that doesn't work:

Code: Select all

<?php
 
$message .= "
<html>
<head>
<title>Regisration Information</title>
</head>
<body>
<div>";
 
$numberofcourses = count($_POST['course']);
 
for($i=0;$i<$numberofcourses;$i++){
    $course[] = $_POST['course'][$i];
    $dept[] = $_POST['dept'][$i];
 
message .= "
 
Course: $course <br />
Department: $dept <br />
 
";
 
}
 
$message .= "
 
</div>
</body>
</html>";
 
$message = wordwrap($message, 78);
 
$to = "myemail@email.edu";
$subject = "student registration";
$headers = "From: myemail@email.edu\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($message));
 
mail($to, $subject, $message, $headers);
 
?>

Re: Issue with email code

Posted: Tue Oct 13, 2009 1:46 pm
by PHPHorizons
Hello junestag,

There are a couple things that might help here. If this sounds nitpicky, the intent is to help tighten up the code and make it easier to debug.

You're assuming that $_PST['course'] is set and that it is an array, and that the array is not empty.

Code: Select all

 
$numberofcourses = count($_POST['course']);
Here's how that could go:

Code: Select all

 
if (!isset($_POST['course']) or !is_array($_POST['course'])) {
     $_POST['course'] = array();
}
$numberofcourses = count($_POST['course']);
 
In the for loop, you are assuming that $_POST['course'] has indexes corresponding to $i.

Then you have two arrays that are later used as strings. $course and $debt are arrays ($course[] and $debt[] means these are arrays), but you later put them into the message variable.

Code: Select all

 
message .= "
Course: $course <br />
Department: $dept <br />
";
Also note how message is missing the $ before it.

Code: Select all

foreach($_POST['course'] as $data){
    $course =$data['course'];
    $dept= $data['dept'];
$message .= "
Course: $course <br />
Department: $dept <br />
";
}
See if that foreach loop works a little better.

Hope that helps.

Re: Issue with email code

Posted: Tue Oct 13, 2009 5:35 pm
by junestag
Thank you. I don't mind you being nitpicky, it's always good to remind to check whether posted variables are set or not.

I stayed with my for loop however because the code that you provided makes each variable = 'c'. Once I put the $ before message in that line it started working...

Each index SHOULD correspond with $i because the form it's coming from uses AJAX to create any number of $course fields. The first one is hard-coded as name="course[0]" with any future courses corresponding to 1+ that.

Re: Issue with email code

Posted: Tue Oct 13, 2009 7:20 pm
by PHPHorizons
You're welcome.

I'm glad it's working now. ;)