Jump to content

Need help with with displaying correct info in email


Johnnyboy123

Recommended Posts

Well this is pretty much the first time I'm attempting something new without a tutorial aiding me ( I know your gonna think I probably should have used 1 when checking my code :D) Honestly my brain is fried, but I have a deadline for tomorrow.

 

Basicly I'm sending an email upon registration ( email sends fine )

 

I made it so the email display the students name and course. However in my registration a student can select contact y or n, which determines whether the student wants to be contacted by other students. So when sending an email to a student who selected n for contact, is should only display the students name and course (sname, fname, cname). However, for a student who selected y for contact, it should display the name and course aswell of a list display the sname,fname and email of all the other students in my student table who selected y in their contact_flag field.

 

Here is my misguided code:

 

<?php
function sendmail(){
$cname = mysql_real_escape_string($_POST['cname']); 
$sname = mysql_real_escape_string($_POST['sname']); 
$fname = mysql_real_escape_string($_POST['fname']); 
$contact = mysql_real_escape_string($_POST['contact']); 
$Name = "Student Course Registration"; //senders name
$email = "[email protected]"; //senders e-mail adress
$recipient = ($_POST['email']); //recipient
$mail_body = "Congratulations  $fname $sname. You have successfully registered for the
	      following course: $cname "; //mail body
    
$subject = "Course registration successful!"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

if ($contact=='y'){
$query = "SELECT sname,fname,email FROM student WHERE $contact = ['contact_flag'] ";
$run = mysql_query($query) or die(mysql_error());
$found = mysql_fetch_array($run);


while ($found = mysql_fetch_array($run))
$contactemail=$person['email'];
$contactsname=$person['sname'];
$contactfname=$person['fname'];

$mail_body2 = "Congratulations  $fname $sname. You have successfully registered for the
			  following course: $cname. Here is a list of all the students
			  who you may be in contact with: $contactsname, $contactfname, $contactemail"; //mail body for contact flag
{			  
    mail($recipient, $subject, $mail_body2, $header);
}

}
else {
mail($recipient, $subject, $mail_body, $header);
}
}
?>

 

I am not receiving any errors from it, and I'm receiving an email which displays $mail_body instead of $mail_body2 which is my else statement. Please, help would be appreciate. Thanks in advance.

Changed the code up a bit, think I fixed a few things, though still getting the same results. But hope I made it easier to work off.

 

<?php
function sendmail(){
$cname = mysql_real_escape_string($_POST['cname']); 
$sname = mysql_real_escape_string($_POST['sname']); 
$fname = mysql_real_escape_string($_POST['fname']); 
$contact = mysql_real_escape_string($_POST['contact']); 
$contactstudents = "SELECT contact_flag FROM student";
$runcontact = mysql_query($contactstudents);

$Name = "Student Course Registration"; //senders name
$email = "[email protected]"; //senders e-mail adress
$recipient = ($_POST['email']); //recipient
$mail_body = "Congratulations  $fname $sname. You have successfully registered for the
	      following course: $cname "; //mail body
    
$subject = "Course registration successful!"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

if ($contact=="y"){
$query = "SELECT sname,fname,email FROM student WHERE $contact = $runcontact";
$run = mysql_query($query) or die(mysql_error());

while ($found = mysql_fetch_array($run))
$contactemail=$found['email'];
$contactsname=$found['sname'];
$contactfname=$found['fname'];

$mail_body2 = "Congratulations  $fname $sname. You have successfully registered for the
			  following course: $cname. Here is a list of all the students
			  who you may be in contact with: $contactsname, $contactfname, $contactemail"; //mail body for contact flag
{			  
    mail($recipient, $subject, $mail_body2, $header);
}

}
else {
mail($recipient, $subject, $mail_body, $header);
}
}
?>

 

Can anyone point me in the right direction please? :)

 

try this (debugging code)

<?php
function sendmail(){
$cname = mysql_real_escape_string($_POST['cname']);
$sname = mysql_real_escape_string($_POST['sname']);
$fname = mysql_real_escape_string($_POST['fname']);
$contact = mysql_real_escape_string($_POST['contact']);
$contactstudents = "SELECT contact_flag FROM student";
$runcontact = mysql_query($contactstudents);
$Name = "Student Course Registration"; //senders name
$email = "[email protected]"; //senders e-mail adress
$recipient = ($_POST['email']); //recipient
$mail_body = "Congratulations  $fname $sname. You have successfully registered for the		      following course: $cname "; //mail body
$subject = "Course registration successful!"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
if ($contact=="y"){
	echo "Sending you mail_body2 because contact=y";
	$query = "SELECT sname,fname,email FROM student WHERE $contact = $runcontact";
	$run = mysql_query($query) or die(mysql_error());
	while ($found = mysql_fetch_array($run))
	{
		$contactemail=$found['email'];
		$contactsname=$found['sname'];
		$contactfname=$found['fname'];
		$mail_body2 = "Congradulations  $fname $sname. You have successfully registered for the following course: $cname. Here is a list of all the students who you may be in contact with: $contactsname, $contactfname, $contactemail"; //mail body for contact flag
		mail($recipient, $subject, $mail_body2, $header);
	}
}
else {
	echo "sending you \$mail_body because \$contact=$contact";
	mail($recipient, $subject, $mail_body, $header);
}
}
?>

Doesn't quite seem to be working, or just me haha. But however in there was a few errors on my part in the previous code that could have effected yours. In any case I just kept getting the else statement email, even when the echo confirmed that it was = Y. But my fault for providing you with such a faulty code :) thanks though

 

I worked a bit more on my code. Maybe I can give you something more decent to work with haha. Basicly I got it down to getting the if else statement to work fine i.e if =y I get mail_body 2 email and else I get mail_body. My only problem is I cant get the fields in the mail_body_2 to display ( $contactsname, $contactfname, $contactemail) which is the list of other students who selected Y.

 

Here is my new code:

<?php
function sendmail(){
$cname = mysql_real_escape_string($_POST['cname']); 
$sname = mysql_real_escape_string($_POST['sname']); 
$fname = mysql_real_escape_string($_POST['fname']); 
$contact = mysql_real_escape_string($_POST['contact']); 

$Name = "Student Course Registration"; //senders name
$email = "[email protected]"; //senders e-mail adress
$recipient = ($_POST['email']); //recipient
$mail_body = "Congratulations  $fname $sname. You have successfully registered for the
	      following course: $cname "; //mail body
    
$subject = "Course registration successful!"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

if ($contact=="Y"){
$contactstudents = "SELECT contact_flag FROM student WHERE contact_flag ='$contact'";
$runcontact = mysql_query($contactstudents);

$query = "SELECT sname,fname,email FROM student WHERE '$contact' = '$runcontact'";
$run = mysql_query($query) or die(mysql_error());

    $found = mysql_fetch_array($run);
$contactemail=$found['email'];
$contactsname=$found['sname'];
$contactfname=$found['fname'];

$mail_body2 = "Congratulations  $fname $sname. You have successfully registered for the
			  following course: $cname. Here is a list of all the students
			  who you may be in contact with: $contactsname, $contactfname, $contactemail"; //mail body for contact flag
{			  
    mail($recipient, $subject, $mail_body2, $header);
}

}
else {
mail($recipient, $subject, $mail_body, $header);
}
}
?>

 

It's like 3am here and my php skills is fairly poor so expect a few questionable mistakes :D. Thanks

I re-worked the logic a bit. Let me know if my assumptions were correct.

<?php
function cleanInput($POST){
$out = array();
foreach ($POST as $key=>$val){
	$out[$key] = mysql_real_escape_string($val);
}
return $out;
}
function sendmail(){
$cPOST = cleanInput($_POST);
$cname = $cPOST['cname'];
$sname = $cPOST['sname'];
$fname = $cPOST['fname'];
$contact = $cPOST['contact'];
// If I understand your code, it looks like you are only pulling the contactflag for all the students. Why?
$contactstudents = "SELECT contact_flag FROM student";
$runcontact = mysql_query($contactstudents);
$Name = "Student Course Registration"; //senders name
$email = "[email protected]"; //senders e-mail adress
$recipient = $_POST['email']; //recipient
$mail_body = "Congratulations  $fname $sname. You have successfully registered for the		      
					following course: $cname "; //mail body
$subject = "Course registration successful!"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
//!!!!!!!!!!!!!!!!!!!Assuming $contact is a checkbox on the form !!!!!!!!!!!!!!!!!!!!!!!
if (!isset($contact) || is_null($contact)){
	mail($recipient,$subject,$mail_body,$header);
}
else{
	$query = "SELECT sname,fname,email FROM student WHERE $contact = $runcontact";
	$run = mysql_query($query) or die(mysql_error());
	$contact_info = "";
	while ($found = mysql_fetch_assoc($run))
	{
		$contactemail=$found['email'];
		$contactsname=$found['sname'];
		$contactfname=$found['fname'];
		$contact_info .= "$contactsname, $contactfname, $contactemail\r\n";
	}
	$mail_body2 = "Congratulations  $fname $sname. You have successfully registered for the
							following course: $cname. Here is a list of all the students 
							who you may be in contact with: $contactsname, $contactfname, $contactemail"; //mail body for contact flag
	mail($recipient, $subject, $mail_body2, $header);
}
}
?>

Your code had a few errors, however your comments and code logic made me look at my code more and change it up a bit. Everything is working fine now. If else statement is correct, it displays the variebles in the else statement and in the mail_body2. My only problem now is, instead of displaying all the students in the mail_body2 with a contact of "Y". It's only displaying 1 (assuming the first 1) I got these results after putting it in a while loop ( first attempt I got 200 emails haha ) how can I change this so it will not just display the first student in my table but all of them?

 

<?php
function sendmail(){
set_time_limit(60); 
$cname = mysql_real_escape_string($_POST['cname']); 
$sname = mysql_real_escape_string($_POST['sname']); 
$fname = mysql_real_escape_string($_POST['fname']); 
$contact = mysql_real_escape_string($_POST['contact']); 

$Name = "Student Course Registration"; //senders name
$email = "[email protected]"; //senders e-mail adress
$recipient = ($_POST['email']); //recipient
$mail_body = "Congratulations  $fname $sname. You have successfully registered for the
	      following course: $cname "; //mail body
    
$subject = "Course registration successful!"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

if ($contact=="Y"){
$contactstudents = "SELECT sname,fname,email FROM student WHERE contact_flag ='$contact'";


$run = mysql_query($contactstudents) or die(mysql_error());

    while ($found = mysql_fetch_array($run)){

$contactemail=$found['email'];
$contactsname=$found['sname'];
$contactfname=$found['fname'];
$studentlist ="$contactsname $contactfname $contactemail";

}
{	
$mail_body2 = "Congratulations  $fname $sname. You have successfully registered for the
			  following course: $cname. Here is a list of all the students
			  who you may be in contact with: $studentlist  "; //mail body for contact flag
    mail($recipient, $subject, $mail_body2, $header);
}

}
else {
mail($recipient, $subject, $mail_body, $header);
}
}
?>

 

Sorry I'm not completely using your code. Project is based on the way I coded it and will be looked at closely. Also for my own education I would like to find the errors in and correct my code. Thanks for your help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.