Silverstripe 3 - send email with data of foreach-loop - email

I want to send an email with the data of a foreach loop but I don't know how to get the data into the email body.
that's my current code
...
...
foreach($cartItems as $cartItem) {
'<strong>' . $cartItem->Amount . 'x ' . $cartItem->Title . '</strong>' . '<span>' . $cartItem->Price . ' €/St. insgesamt ' . $cartItem->Sum . '</span><br>';
}
...
...
$messageBody = "
foreach() Content should be here
";
$email->setBody($messageBody);
...
...
can someone help me?
thank you in advance

Use the built in Silverstripe Email functionality to take advantage of Silverstripe's templating with your email.
With Silverstripe you can create a template for your email and populate it with whatever data you like.
Within your send function in your controller you can set the template with setTemplate and push data into the email template with populateTemplate:
$email = new Email($from, $to, $subject);
$email->setTemplate('EmailTemplate');
$email->populateTemplate(array(
'CartItems' => $cartItems,
'PageTitle' => $this->Title
));
$email->send();
Note that $cartItems has to be a ArrayList or DataList to be able to use <% loop %> in template. If $cartItems is an array, read how to convert it to ArrayList here.
Put your template into themes/mytheme/templates/Email/EmailTemplate.ss
EmailTemplate.ss
<!DOCTYPE HTML>
<html>
<head>
...
</head>
<body>
...
<% if $CartItems %>
<% loop $CartItems %>
...
<% end_loop %>
<% end_if %>
...
</body>
</html>

Related

link is not showing in email body in php

I am trying to to use a link in my email body but failed to do that, Whenever i send an email, email is sent but it contain only the plain text but i also want to print a link so that that link will redirect me to another page.
here is my php code,
<?php
include("admin/config.php");
$email=$_POST['email'];
/// mail to customer
$to=$email;
$subject="Thanks for Contacting with us";
// Message
$message = '
<html>
<head>
<title>Request to Change Password</title>
</head>
<body>
<p>You have requested to change password <br> Please follow the below link to change your password.<br><br></p>
<p>Change Password<br><br></p>
<p>Regards<br></p>
<p>http://www.example.com<br></p>
<p>9999999999<br></p>
</body>
</html>
';
$from = 'MIME-Version: 1.0' . "\r\n";
$from.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$from.= 'From: <no-reply#abc#gmail.com>' . "\r\n";
$mail =mail($to,$subject,$message,$from);
if ($mail == true){
header('location:forgot_password.php');
}
?>
Please help me out, thanks in advance
The link is not "showing" because you wrapped it in a <p> tag which is for text (paragraph) only. Wrap the link in a <a> tag and set the href attribute accordingly :
<a href="your link">
A descriptive texte or your link again
</a>

Use Mojolicious action as title in template

I'm trying to make the default "title" variable in stash be set to the English version of the action name. For example:
sub customer_orders {
...
}
Would have:
title => 'Customer Orders',
Available in the stash for the templates to use. Does anyone know how to do this? Thanks!
You could get the subroutine name via caller:
my $sub_name = (caller(0))[3];.
Judging from your output, you may want to also upcase it and replace _ with whitespace
$sub_name =~ s /_/ /g;
I would upcase like this:
my $title = join(' ', map{ ucfirst lc }split(' ', $sub_name) );
It looks like $c->action is available in Mojolicious templates as $action. So you can just do this:
<title><%= title || action_to_title($action) %>
This way other templates can override the title like so:
% title 'My Override Title'
And you can add the action_to_title helper to prepare the action for a title if title is not set.
You can put title into layout template
<!DOCTYPE html>
<html>
<head>
<title><%= content 'title' %></title>
<style>
label.field-with-error { color: #FF4C4D }
input.field-with-error { background-color: #FF4C4D }
.error_msg { color: #FF4C4D }
</style>
%= content 'styles';
</head>
<body>
%= include 'basic/menu'
%= include 'basic/error_messages'
%= content
%= content_for 'include_js'
</body>
</html>
Then at your template fill it with content (read also about content_for, content_with)
% content_with title => "$title_schet за $title_date";
% content_for styles => '<link rel="stylesheet" type="text/css" href="/static/css/report.css">';
<form>
...
</form>

html calling perl subroutine and return values from subroutine to display in the html

here is my html code
<html>
<title>Results</title>
<body><h1> Here are your results</h1>
<p>Please click the Button to see your result run by Ravi's team.</p>
<form action='index.pl' method='post'>
<input type='submit' value='submit'>
</form>
</body>
</html>
and index.pl is my perl and my subroutine is as follows.
sub my_result{
my $run;
my $dir="/kbio/sraja/BenzoExposedDataSet/database/Output";
my $parsebphtml = "/parse_bphtml.pl";
my $olgacsvfile = "/database/Output/sample.csv";
my #bp=<$dir/*.bp>;
$run ="perl $parsebphtml > $olgacsvfile";
# print "$com\n";
system($run)==0 or my_err("Could not run $run\n");
#printing the table
open(F,"$olgacsvfile") or my_err("Could not open the csv ($olgacsvfile) file");
print "<h2> Average Results </h2>";
print "<table border=1>";
while(my $line=<F>){
print "<tr>";
my #cells= split ',',$line;
foreach my $cell (#cells)
{
print "<td colspan=1>$cell</td>";
}
print "</tr>";
}
print "</table>";
}
So as you see, table is what i need to return to results.html
Any help would be really appreciable.
thanks .
Geet
I don't know how much work you want to do but, if you want to keep it simple give a try at the HTML::Template module. Here is a simple usage example.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>A random page</title>
</head>
<body>
<TMPL_VAR NAME=page_content>
</body>
</html>
My perl code contained something like this. Better yet, check the documentation at http://metacpan.org/pod/HTML::Template .
use HTML::Template;
sub my_result {
return $html_string;
}
my $master_template = HTML::Template->new(filename => "Path to html template file");
$master_template->param('page_content' => my_result());
Depending on how far you plan on going with this, I would recommend that you a more advanced templating system such as the one used by the mojolicious framework (http://mojolicio.us/perldoc/Mojo/Template).
Cheers,
MrMcKizzle

GET/POST variable not set in netbeans IDE

I dunno if its a silly mistake but a post/get variable is not being set, while it is supposed to be. Here are the HTML and php code snippets:
<html>
<head>
<title>
Chain Story
</title>
</head>
<body>
<form method="GET" action="check-valid.php">
<textarea name="a" rows="5" cols="50"></textarea>
<input type="submit" value="Add" />
</form>
</body>
</html>
check-valid.php:
<?php
require 'includes/connect.inc.php';
$conn_ref = connect_db('chainstory') or die(mysqli_error());
if(isset($_GET)){
echo 'Get variable set';
if(isset ($_GET['a'])){
$as = $_GET['a'];
$query = "insert into story1 values (1, " . $as . ")";
mysql_query($query, $conn_ref);
}
else{
echo $_GET;
}}
?>
I get the following output:
Get variable set
Notice: Array to string conversion in /home/kevin/Code/php/myWebsite/check-valid.php on line 15
Array
I am coding this in netbeans. Can anyone point me out what mistake im making? :(
Did you try rename the textarea? Longer name , and give id to textarea same the name.
What browser does you use for testing?
I met some problems with input names in IE for example if an input name matches a javascript function name or protected names. Have you a javascript function or variable in your code what's name is a ? Because if the name of the input conflicts with a js var or name IE does not send the input field to the server. (Chrome Firefox and other browsers does)

Silverstripe loop over ALL children of the sitetree

I´m trying to create a sitemap of all children of the sitetree. For some pages ShowInMenus is set to false. But still these pages should be shown in the sitemao. I know you can loop over all children of a page like this (ignoring ShowInMenus):
<% loop AllChildren %>
$Title
<% end_loop %>
Can I do something similar on the top Level of the sitetree?
Thx,
Florian
i do not know any build in loop you can use for this but you can do in php
public function AllPagesWithParentIDZero() {
return SiteTree::get()->filter('ParentID', 0);
}
and use in template
<% loop $AllPagesWithParentIDZero %>
$Title
<% end_loop %>