Page 1 of 1

Error writing some custom PHP (is 'require' allowed?)

Posted: Tue Mar 20, 2007 2:56 pm
by anthony
I am going to creating a custom form using phpmailer that will send out an email to the appropriate office selected in a drop-down.

Now I went into config.php and set use_smarty_php_tags = true; and then got started on my template that will hold the form.  Here is my code:

Code: Select all

{php}
echo 'Test1';
############## EMAIL LEADER ######################
// generate email
require("/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
echo 'Test2';

$mail->From = 'test@test.com';
$mail->FromName = 'Test';
// FOR TESTING
$mail->AddAddress("test@test.com", "Test");
$mail->Subject = "Test request";
// FOR TESTING
$mail->Body    = "\nTesting a request from the web site.";

if(!$mail->Send())
	{
	   echo "Message could not be sent. Please contact <a href=\"mailto:test@test.com\">Test</a>. <p>";
	   echo "Mailer Error: " . $mail->ErrorInfo;
	   exit;
	}
// comment out below line for live version
echo "Message has been sent";

{/php}
Now it seems that as soone as it hits "require("/phpmailer/class.phpmailer.php");" nothing happens.  For example I do not see the text "Test2" that I entered just to see where I was having the problem, no email ends up getting sent but no error or confirmation gets written to the screen either.

I'm wondering, am I allowed to use require inside of a smarty template file?  Also, is this the best way to do this or should I be going about this another way?

Many thanks in advance.

Re: Error writing some custom PHP (is 'require' allowed?)

Posted: Tue Mar 20, 2007 3:15 pm
by Dee
anthony wrote: is this the best way to do this or should I be going about this another way?
The recommended way is to use a User Defined Tag (UDT).

Regards,
D

Re: Error writing some custom PHP (is 'require' allowed?)

Posted: Tue Mar 20, 2007 3:19 pm
by anthony
That works for me.

Are there any limitations in creating my own tag?  As I said, it seems that all the custom code that I entered into the body of the template isn't being parsed.  I am just looking for something that will parse for sure and of course is recommended.

Thanks.

Re: Error writing some custom PHP (is 'require' allowed?)

Posted: Tue Mar 20, 2007 3:39 pm
by Dee
There are no real limitations in using your own PHP. It's probably a include error because of the relative path you're using, try:

Code: Select all

$gCms = $GLOBALS['gCms'];
require($gCms->config['root_path'] . "/phpmailer/class.phpmailer.php");
or

Code: Select all

$gCms = $GLOBALS['gCms'];
require(cms_join_path($gCms->config['root_path'], 'phpmailer', 'class.phpmailer.php'));
Regards,
D