how to add php code in .tpl file in prestashop - php

Does anyone know how to add php code in .tpl file in prestashop..i tried a lot but couldn't find any solutions..I want to add a mail function in my .tpl file
This is the function
<?php
$name=$_REQUEST["name"];
$email=$_REQUEST["email"];
$matter=$_REQUEST["matter"];
$subject=$name."has shared a weblink";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$contactMsg = '
<html>
<head>
<title>Tell A Friend</title>
</head>
<body>
<table>
<tr>
<td>E-mail: '.$email.' </td>
</tr>
<tr>
<td>Comment: '.$matter.' </td>
</tr>
</table>
</body>
</html>
';
$headers .= "from:abcd" . "\r\n";
$headers .= "To: Info <info#abcd.in";
$headers .= "reply-to:".$email."\r\n";
$headers .= "Cc:".$email."\r\n";
$headers .= 'Bcc: ' . "\r\n";
if(mail($email, $subject, $contactMsg, $headers))
{
$_REQUEST = array();
$error.="Mail Sent Successfully";
}
else
{
$error.="Error Mail Sent Fail!";
//
}
?>
i tried writing the code inside {php} {/php} block..but couldn't help
And how to see the error log in prestashop

You can edit the controller file, eg. if you want to add the form in a cms page, you have to edit this /controller/CMSController.php
edit this
public function displayContent()
{
parent::displayContent();
self::$smarty->display(_PS_THEME_DIR_.'cms.tpl');
}
with this
public function displayContent()
{
IF ($_POST['submit_form']==1){
// here submit action mail() for example
}
parent::displayContent();
self::$smarty->display(_PS_THEME_DIR_.'cms.tpl');
}

it's better to add php functions classes etc in your module controller,
{php} is depreciated for a reason as it allow poor practices. Smarty recommends putting the included script into the PHP logic (controllers,classes,functions)

The use of the {php}{/php} tags in .tpl files is turned off in somme versions of Prestashop
I had the same problem using Prestashop 1.5.4.1
To turn it on make changes in the file config/smarty.config.inc.php :
find these lines (line 27 to line33)
...
define('_PS_SMARTY_DIR_', _PS_TOOL_DIR_.'smarty/');
require_once(_PS_SMARTY_DIR_.'Smarty.class.php');
global $smarty;
$smarty = new Smarty();
$smarty->setCompileDir(_PS_CACHE_DIR_.'smarty/compile');
...
change it to
...
define('_PS_SMARTY_DIR_', _PS_TOOL_DIR_.'smarty/');
require_once(_PS_SMARTY_DIR_.'SmartyBC.class.php');
global $smarty;
$smarty = new SmartyBC();
$smarty->setCompileDir(_PS_CACHE_DIR_.'smarty/compile');
...
...shortly, use SmartyBC.class.php instead of Smarty.class.php
(warning: using {php}{/php} tags in template files is deprecated in Prestashop!)

Related

Moodle email templates

I am working on a Learning Management System build upon Moodle. I want to add an email header and footer for each email.
I did some change in Moodle for adding an image in ./lib/moodlelib.php as follows:
function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '', $attachment = '', $attachname = '',
$usetrueaddress = true, $replyto = '', $replytoname = '', $wordwrapwidth = 79) {
global $CFG, $PAGE, $SITE;
// Append image at top
if($messagehtml){
$tmp = $messagehtml;
// Added image here
$messagehtml = '<img src="'.$CFG->wwwroot.'/images/logo.png" alt="LMS" /><br/>';
// $messagehtml = $image;
$messagehtml .= $tmp;
}
if($messagetext){
$tmp = $messagetext;
// Added image here
$messagetext = '<img src="'.$CFG->wwwroot.'/images/logo.png" alt="LMS" /><br/>';
// $messagehtml = $image;
$messagetext .= $tmp;
}
....................
but I want the header and footer as fixed templates. Please help me.
You could create a message header in a language file (in English either directly under /lang/en or in a plugin) and then add the following string in the language file:
$string ['message_header'] = '<p> write here whatever your style and HTML structure you want in your header</p>
adding your image as well <img src="'.$CFG->wwwroot.'/images/logo.png" alt="LMS" />';
Then you could write a string for your footer as well:
$string ['message_footer'] = 'Your HTML footer here';
And finally you could insert your strings in the message:
$message = 'here your body';
// insert message header - if your language string is in /lang/moodle.php:
$message = get_string ( 'message_header') . $message;
// insert message footer - if your language string is in your /local/myplugin:
$message .= get_string ( 'message_footer', 'local_my_plugin' );
This way your header and footer may be customized at will if you change them directly in the language file (or, in the DB. See here).
a little bit too late, but i hope it helps others:
https://docs.moodle.org/dev/Themed_emails just look for e-mail templates in /var/www/html/moodle/lib/templates. The template with a name email_html.mustache should be the right one.

wordpress mail header set else plain text

Hope to get some help with a piece of code, I am using a theme for wordpress which sets the mail headers to text/html, this causes some problems with plain text mail ex. linebreaks don't show anymore.
I tried setting :
} else {
return 'text/plain';
}
but I don't know php very well so I don't know where to place it to make it work. I would like to set the text/plain for mails not defined.
this is the code for the wp header :
/**
* filter mail headers
*/
function wp_mail($compact) {
if (isset($_GET['action']) && $_GET['action'] == 'lostpassword') return $compact;
if ($compact['headers'] == '') {
//$compact['headers'] = 'MIME-Version: 1.0' . "\r\n";
$compact['headers'] = 'Content-type: text/html; charset=utf-8' . "\r\n";
$compact['headers'].= "From: " . get_option('blogname') . " < " . get_option('admin_email') . "> \r\n";
}
$compact['message'] = str_ireplace('[site_url]', home_url() , $compact['message']);
$compact['message'] = str_ireplace('[blogname]', get_bloginfo('name') , $compact['message']);
$compact['message'] = str_ireplace('[admin_email]', get_option('admin_email') , $compact['message']);
$compact['message'] = html_entity_decode($compact['message'], ENT_QUOTES, 'UTF-8');
$compact['subject'] = html_entity_decode($compact['subject'], ENT_QUOTES, 'UTF-8');
//$compact['message'] = et_get_mail_header().$compact['message'].et_get_mail_footer();
return $compact;
}
Instead of changing that, change your plain line breaks to html.
$message=nl2br($message); // of course use your var name.
That way you get to keep a standard format for email as well. plain text has nothing so special to need a separate header in this case. This function will convert all line breaks to html version.
Other than new lines most of your plain text will hold its formatting even in html because it has no special tags.
Here is how you will place it
function wp_mail($compact) {
// leave your existing code intact here, don't remove it.
$compact["message"]=nl2br($compact["message"]);
return $compact;
}

HTML site with AJAX request image onclick execute PHP file

I will really appreciate if you could help me in that coding. I really need help.
I want with single press over the image, to get the data from fields sent over email.
So my HTML image code like that :
<img src="images/some.png" id="help" onClick="help();">
Right before that tag in HTML file I put AJAX like that:
<script type="text/javascript">
function help() {
$.get("/help.php");
return false;
}
</script>
And with that function i want, when the user click over the image to execute my PHP script which is on other file and it contains code like that :
help.php
<?php
{
$helpSubject = 'help';
$webMaster = '****#mail.com';
$helpField = $_POST['help'];
$problemField = $_POST['problem'];
$body = <<<EOD
<br><hr><br>
Help: $help <br>
Problem: $problem <br>
EOD;
$headers = "Reason: $help\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $helpSubject, $body, $headers);
$theResults = <<<EOD
Success
EOD;
echo "$theResults";
}
?>
Something is wrong, and I can't find what. Could anyone help me with that?
Thank you.

Need stylesheet before ob_endflush() and before header(location: ), How?

My page has the following structure:
<?php
session_start();
ob_start();
?>
HTML HEADER
links to stylesheets global.css and mail.css
<?php
function email(){
/* using the stylesheets global.css and mail.css */
$text = '<div class="test">
<p id="par">...
<span id="write" class="text">...</span>
</p>
</div>';
return $text;
}
?>
/* some text and html stuff */
<?php
if(isset(...)){
/* php form validation */
/* after validating I send an email to the user where I call
the function email() which use the stylesheets. */
$mail_text = 'Dear .....';
$mail_text .= email();
$mail_headers = "From: test#test.com\r\n".
'X-Mailer: PHP/' . phpversion().
'MIME-Version: 1.0' . "\r\n".
'Content-type: text/html; charset=utf-8' . "\r\n";
if(mail(..., ..., $mail_text, $mail_headers)){
header('location: newlocation.php');
}
?>
/* HTML FORMS and text and .... */
<?php
ob_end_flush();
?>
The email is sent with the text coming from the email() function, but without formatting cause the CSS are "printed out" only after ob_end_flush() at the bottom of the page.
How could I solve that? There are many classes and styles inside email(), so writing every times <div style="..."> and so on is not a good solution.
You are confused about output buffering. The html/css you print out at the start of the page isn't being captured anywhere, and will NOT be part of $mail_text. Just because you're usign output buffering doesn't make that buffer magically appear in a variable. You'd need at least $mail_text .= ob_get_clean() or something to extract the buffer's contents and put it into your variable.

PHP variable containing white space causing emails to fail

I am trying to send emails using the PHP mail function, but I am running into issues when it actually sends the email.
For some reason, the $to variable has a space between each character. So when it's emailed, the spaces are filled with dots. The $to email is being grabbed from a TSV file generated from a remote system that I do not control.
I have seen where people have a line where it searches for white space and deletes it but i do not want to go that route.
The CC function works perfectly.
Does anyone know what may be happening? I have seen a couple of threads and bugs but they do not match it exactly.
<?php
$Name= $_POST["Name"];
//File Upload Script
if ($_FILES["file"]["error"] > 0)
{
}
else
{
echo "File name: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
move_uploaded_file($_FILES["file"]["tmp_name"], "C:/xampp/htdocs/Uploads/" . $_FILES["file"]["name"]);
echo "http://php/Uploads/" . $_FILES["file"]["name"];
}
$URLLink = "C:/xampp/htdocs/Uploads/" . $_FILES["file"]["name"];
//End File Upload Script
//Reading TSV File
$filename = $URLLink;
//Deleting first line and last 5 lines
$alines=file($filename);
$dlines=array_slice($alines,1, -5);
$fileContents=implode($dlines);
$fp=fopen($filename,'w+');
fwrite($fp,$fileContents);
fclose($fp);
//END of Deleting first line and last 5 lines
$filename = $URLLink;
$file = file($filename);
foreach($file as $line)
{
list($start_time, $end_time, $instance, $status, $coordinator) = explode(" ", $line);
$from = $SentFrom;
$to = $coordinator;
$to = str_replace(' ', '', $coordinator);
//$to = str_replace('.', '', $coordinator);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:" . $from . "\r\n" . "CC:" . $mailer . "," . $emailone. "," . $emailtwo;
$subject = "Test";
include_once("EmailBody.php");
mail($to, $subject, $message, $headers);
}
?>
Page before is a form.
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Impact Notification</title>
<link rel="stylesheet" href="../style/style.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../style/menustyle.css" type="text/css" media="screen" />
<style type="text/css">
.auto-style1 {
font-family: "Lucida Calligraphy";
}
</style>
</head>
<body style="color: #black; background-color: #0000FF;">
<div align="center"id="MainWrapper" style="width: 70%">
<div id="container" style="width:100%">
<div id="PageHeader">
<?php include_once("../template/template_header.php");?>
</div>
<div id="leftcolumn" style="height:auto;width:11%;float:left;padding:15px 15px 15px 15px;">
<?php include_once("../template/menu.php"); ?>
</div>
<div id="content" style="background-color:#EEEEEE;height:auto;width:80%;float:left;">
<h2 class="auto-style1">Email Notification</h2>
<form action="sendemail.php" method="post" name="emails" enctype="multipart/form-data">
<table style="width: 100%">
<tr>
<td>Enter Your Name</td>
<td><input name="Name" type="text" style="width: 300px; height: 24px;" /></td>
<tr>
<td style="width: 180px"><strong>File Upload:</strong></td>
<td><input type="file" name="file" id="file" /></td>
</tr>
</table>
<br />
<input name="Submit" type="submit" value="Submit" /><input name="Clear" type="reset" value="Reset" /><br />
<br />
</form>
</div>
</div>
</div>
Revised Answer: BAD DATA HANDLING!
Whilst it may still be a UTF / Charset issue, according to the code you have provided and sample data, your data handling is incorrect. Here are some tips for you (which have been incorporated into my suggested foreach loops below).
Your text file header contains a number of fields that you would like to retrieve. These are represented as the following fields sequentially in a row, separated by tabs:
Start Time End Time Instance Type Status Endpoint Organizer ....etc
You seem to be wanting a number of these fields to be captured, and amongst them, the $coordinator value needs to contain the data within the organiser field as listed within the text file (which appears to always be an email address):
Looking at your raw tsv file, your "list" processor is processing the wrong variables as you have missed the endpoint field!! Even if you're not going to use it, you'd need to put something in there for it to work the way you have set things up. At the very best, you would be storing the value for endpoint as $coordinator, but I suspect you wouldn't be getting that far because of the way you are exploding (see point 2 below).
The file you submitted seems to be separated by tabs, yet you're exploding using spaces. Change the " " in your explode to "\t". Currently, you're probably getting junk in your variable data (as it is splitting after every space in each line) - the organiser email address is not going to ever make it into $coordinator - perhaps you could echo your variables as a test to see what I mean (and comment out your mail function while you're testing).
I have revised your foreach loop:
foreach($file as $line)
{
list($start_time, $end_time, $instance, $status, $endpoint, $coordinator) = explode("\t", $line);
$from = $SentFrom;
$to = $coordinator;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:" . $from . "\r\n" . "CC:" . $mailer . "," . $emailone. "," .$emailtwo;
$subject = "Test";
include_once("EmailBody.php"); // Can' really comment on what's going on here - as I don't know what is in this file ! :)
mail($to, $subject, $message, $headers);
}
You are stripping 5 lines from the bottom of the file and one from the top, but I see 6 lines that need to be removed from the bottom and one from the top in your sample file. Perhaps change the -5 to -6. Or alternatively, you could do something like this in your foreach loop to instruct the routine not to send an email if the $coordinator variable is "blank" so it doesn't fail if it reads a blank line, etc:
Alternative ForEach Loop - to stop blank lines within your file causing failures:
foreach($file as $line)
{
list($start_time, $end_time, $instance, $status, $endpoint, $coordinator) = explode("\t", $line);
if (trim($coordinator) !== '')
{
$from = $SentFrom;
$to = $coordinator;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:" . $from . "\r\n" . "CC:" . $mailer . "," . $emailone. "," .$emailtwo;
$subject = "Test";
include_once("EmailBody.php"); // Can' really comment on what's going on here - as I don't know what is in this file ! :)
mail($to, $subject, $message, $headers);
}
}
You seem to be using a Windows implementation of PHP Mail. You may not have any issues with your current message content - just be aware that any lines starting with a period/fullstop will be removed. If you have any of your message lines starting with a '.', you might need something like this:
$vardata = str_replace("\n.", "\n..", $vardata);
Looking at a hex dump of the file, your data is encoded as UTF-16, which for ASCII data includes a nul character between each character of data. You can convert with iconv()
Note: you need to run this on the original file before you try extracting the data.
$str = file_get_contents('2.tsv');
$result = iconv($in_charset = 'UTF-16LE' , $out_charset = 'UTF-8' , $str);
if (false === $result)
{
throw new Exception('Input string could not be converted.');
}
$result = substr($result, 3); // Strip BOM
file_put_contents('3.tsv',$result);
echo $result;
Alternatively you can use
$str = file_get_contents('2.tsv');
$result = mb_convert_encoding($str, 'ASCII', 'UTF-16LE'); //instead of iconv()
file_put_contents('3.tsv',$result);
I have run both successfully on my development server.
You could use trim(), to strip all sorts of whitespace characters only at the beginning and end of the string. I don't think I've seen it suggested yet.
http://nl1.php.net/trim
This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:
" " (ASCII 32 (0x20)), an ordinary space.
"\t" (ASCII 9 (0x09)), a tab.
"\n" (ASCII 10 (0x0A)), a new line (line feed).
"\r" (ASCII 13 (0x0D)), a carriage return.
"\0" (ASCII 0 (0x00)), the NUL-byte.
"\x0B" (ASCII 11 (0x0B)), a vertical tab.

Categories