Jump to content

[SOLVED] Can PHP tags go within PHP tags?


webmaster1

Recommended Posts

Hi All,

 

I have the following code to process a basic form:

 

<?php

                                     session_start();
                                     if (!$_SESSION['logged'])
                                     {
                                     header("location: loginfl.php");
                                     }
                                     else
                                     {

if (isset($_POST['submit']))

//PROCESS THE FORM WHEN THE SUBMIT BUTTON IS PRESSED

{ 

//VALIDATE THE INDIVIDUAL FIELDS AND DEFINE THEM AS TRUE OR FALSE BASED ON THE USER'S INPUT

        
if (strlen($_POST['recipientemail']) > 0)
		{$recipientemail=TRUE;}
else 	{$recipientemail=FALSE;
		$message_recipientemail=" *You forgot to enter the recipient email!";}


if (strlen($_POST['recipientname']) > 0)
		{$recipientname=TRUE;}
else 	{$recipientname=FALSE;
		$message_recipientname=" *You forgot to enter the recipient name!";
		}
            
if (strlen($_POST['salesagentname']) > 0)
		{$salesagentname=TRUE;}
else 	{$salesagentname=FALSE;
		$message_salesagentname=" *You forgot to enter your name!";
		}

//IF ALL INPUT FIELDS ARE DEFINED AS TRUE / VALIDATED
       		
if 
        ($recipientemail && $recipientname && $salesagentname)

	{

              include("dbinfo.php");
          mysql_connect(localhost,$username,$password);
          @mysql_select_db($database) or die( "Unable to establish a connection to the relevant database.");

                  $recipientemail = mysql_real_escape_string($_POST['recipientemail']);
                  $recipientname = mysql_real_escape_string($_POST['recipientname']);
                  $salesagentname = mysql_real_escape_string($_POST['salesagentname']);

//DEFINE ADDITIONAL VARIABLES

                  $ipaddress = getenv('REMOTE_ADDR');

//INSERT THE INPUT INTO DATABASE

$query = "INSERT INTO zevasce VALUES ('','$recipientemail','$recipientname','$salesagentname', NOW(),'$ipaddress')";
mysql_query($query);


//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////


// I WANT TO ADD THE EMAIL CODE HERE


//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////



//NEXT DISPLAY A SUMMARY OF WHAT HAS BEEN UPLOADED

          echo "Thank you $salesagentname. The desired mail and document has been sent to $recipientemail for $recipientname.</br>";
                  
                  
                  echo "</br><a href='customeremail.php'>Send another Customer Email</a></br>";
                  echo "<a href='admin.php'>Return to Administration Home</a>";

//VERY IMPORTANT! EXIT(); WILL NO LONGER DISPLAY THE FORM

               exit();

//CLOSE: IF ALL INPUT FIELDS TRUE:

	}

//CLOSE: OVERALL VALIDATION:

} 

                                     }

?>

 

I want to add the following to it but I'm not sure how to integrate it because it contains multiple PHP blocks of code.

 


<?php 
//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Test email with attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('cat_scan.pdf'))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/zip; name="attachment.zip"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?> 

 

Does anybody have any advice on how to tackle this? Can I integrate them by simply removing the PHP tags in the second code?

Link to comment
https://forums.phpfreaks.com/topic/140405-solved-can-php-tags-go-within-php-tags/
Share on other sites

Don't ask me why I tried this but I did and it worked:

 

<?php
//define the receiver of the email 
$to = ''.$recipientemail.''; 
?>

 

I basically swapped '$recipientemail' for ''.$recipientemail.''

 

I've seen this done in a few scripts I used a while back. Any ideas whats actually happening when I add the '. .'

 

It looks like I'm opening and closing a string.

All variables in included file are available as if they were defined in main file so doing

 

$to = $recipientemail;

will work.

 

Actually you can just use $recipientemail variable without rewriting it to $to

 

$mail_sent = @mail( $recipientemail, $subject, $message, $headers ); 

 

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.