I want to pass multiple variable to Mail template page and echo to that page using phpmailer library.
I have two variables to pass welcome.php page and echo there.
$name = 'example';
$email = 'example#mail.com';
I have use this code
$mail->MsgHTML(str_replace('[emailhere]', $email, file_get_contents('welcome.php')), dirname(__FILE__));
You can send arrays to str_replace to replace multiple values.
For example:
$message = str_replace(
array(
'[emailhere]',
'[namehere]'
),
array(
$email,
$name
),
file_get_contents('welcome.php')
);
By the way, you should probably not give your template a php extension as it will parse any php if you call or include it directly. So that could be a security risk if users can modify templates.
You may use output buffering.
Echo variables on your template file ($template):
for ex:
<p><?=$name?></p>
Then include it and pass through output buffering
ob_start(); //Start output buffering
include('welcome.php'); //include your template file
$template = ob_get_clean(); //Get current buffer contents and delete current output buffer
$mail->msgHTML($template); // Add html content into your PHP MAILER class
Related
Triyng to start opencart shop at first time
At the root of the site I have the file "file.ssi"
How to insert its contents into a twig template? (header.twig)
Why is it important that the file must be at the root and called file.ssi -- I have a script that periodically changes the contents of this file at many of my sites, and on static html sites or wordpress I print content of file.ssi into template with SSI, and how to do it on twig I don't understand
I tried write in header.twig
{{ bla_bla }}
then in catalog\controller\common\header.php
$data['bla_bla'] = sprintf($this->language->get('text_bla_bla'), $this->config->get('config_name'), date('Y', time()));
then in catalog\language\ru-ru\common\header.php
$_['text_bla_bla'] = 'my html code 1';
and this html code prints on the right place.
But when I tried to do something like this
$_['text_bla_bla'] = 'my html code 1' . $bla_bla_bla = file_get_contents('/file.ssi');
echo $bla_bla_bla; . 'my html code 2';
site doesn't even open
I understand the pure php error and my modest knowledge (or rather un-knowledge) php is not enough to get in $ _ ['text_bla_bla'] a couple of pieces of html and the contents of the file between them.
In general, I need to insert something in header.twig, or to solve it somehow with php, I myself do not understand where it was wrong.
This works!
in controller
$data['mydata'] = file_get_contents($path);
in template
{{ mydata }}
It would look like this:
Controller:
$text = file_get_contents($path);
return $this->render('page.html.twig', [
'text' => $text,
]);
Then in your twig template:
{{ text }}
bad syntax:
$_['text_bla_bla'] = 'my html code 1' . $bla_bla_bla = file_get_contents('/file.ssi');
echo $bla_bla_bla; . 'my html code 2';
If you need to use sprinf so in language file you can get this data $this->config->get('config_name'), date('Y', time() using %s %d
$_['text_bla_bla'] = 'my html code 1 %s %d';
I got simple caching script which is saving php code into HTML. Everything works fine, but I need to add dynamic tracking code into every saved HTML by including a script from another file with include() statement
....
$s_getdata = http_build_query( array(
'cat' => 'CAT1',
'buffit'=>'TRUE',
)
);
$s_page = file_get_contents('http://example.com/buffer.php?'.$s_getdata);
ob_start();
echo $s_page;
file_put_contents('./index.html', ob_get_contents());
ob_end_clean();
I tried to add this code before ob_start, but it just add code as simple text in string (not working) :
$s_page .= '<?php include("./tr/in.php"); ?>';
Any idea how to do it ? Thanks!
You can do this by 2 ways,
Option 1: return value as a function from the in.php file,
your in.php file code,
function dynatrack() {
// your dynamic tracking code generation here
return $code; // where $code is your dynamic tracking code
}
Your original file,
$s_page = file_get_contents('http://example.com/buffer.php?'.$s_getdata);
include("./tr/in.php");
$s_page .= dynatrack();
Option 2: Get the contents of the in.php file by using file_get_contents again,
$s_page = file_get_contents('http://example.com/buffer.php?'.$s_getdata);
$s_page .= file_get_contents('./tr/in.php');
I hope this helps!
I have a script that gets a streams link based on the external html page.
Is there a way to have one script and a resourced page with links to all the external links or do I need to have an individual page per request?
In other words, can I do this
example.com/videos.php?v=1234
or must I use
example.com/eachVideoPage.php
Here is the script I use to get the link.
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/xmpegURL");
?>
<?php
ob_start(); // ensures anything dumped out will be caught
$html = file_get_contents("http://example.com/aVideoPage.html");
preg_match_all(
'/(http:\/\/[^"].*?\.mp4)[",].*?/s',
$html,
$posts, // will contain the article data
PREG_SET_ORDER // formats data into an array of posts
);
foreach ($posts as $post) {
$link = $post[1];
// clear out the output buffer
while (ob_get_status())
{
ob_end_clean();
}
// no redirect
header("Location: $link");
}
?>
Assuming all you want to do is change the file_get_contents() URL It should be as simple as:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/xmpegURL");
$vid = htmlspecialchars($_GET["v"]);
ob_start(); // ensures anything dumped out will be caught
$html = file_get_contents("http://example.com/$vid/aVideoPage.html");
preg_match_all(
'/(http:\/\/[^"].*?\.mp4)[",].*?/s',
$html,
$posts, // will contain the article data
PREG_SET_ORDER // formats data into an array of posts
);
foreach ($posts as $post) {
$link = $post[1];
// clear out the output buffer
while (ob_get_status())
{
ob_end_clean();
}
// no redirect
header("Location: $link");
}
?>
Updates:
$vid = htmlspecialchars($_GET["v"]);
Using _GET will take the URL Query parameters and do whatever you want with them. In this case a basic value assignment looking for ?v=123 in the URL. ($vid === '123')
It is worth mentioning that you should add some code to handle cases where ?v does not exists, or contains malicious input (depending on who uses it)
Then just reuse the value in the URL you want to call:
$html = file_get_contents("http://example.com/$vid/aVideoPage.html");
Update after comments
In order to call another site with a query string you can simply append it to the URL:
$html = file_get_contents("http://example.com/aVideoPage.html?v=$vid");
This will depend entirely on the remote site you are calling however. .html files usually won't do anything with query strings unless there is JavaScript in the code that uses it. But generally if you can browse to the remote URL with ?v=1234 in the URL and it selects the right video, then the above should also work.
I have a database with the following fields
ID
title
URL
summary
This data is stored in a multi dimensional associative array like this:
$array[0][title] = Title-1
$array[0][summary] = Summary-1
$array[1][title] = Title-2
$array[1][summary] = Summary-2
I also have a script that can loop through these and render as needed.
However I'm finding it tough getting this output into the body of a mail.
Looking around got me the following
Dynamic Content into mail() $message
Dynamic HTML Table in PHP Mail
HTML mail with dyanmic values
How to send HTML Dynamic Table as mail in php?
I found many more solutions via google, but they're more or less similar to the one's above.
These have not been helpful as the content is part static and part dynamic. In my case the whole body would be dynamic
Here's what I have as of now
function get_mailer_contents($email_category) {
global $mailer_contents;
$fetch_mailer_contents = mysql_query("SELECT * from `selected_posts` WHERE industry='$email_category'");
$id = "0";
while($temp_mailer_contents = mysql_fetch_array($fetch_mailer_contents))
{
$mailer_contents[$id]['title'] = $temp_mailer_contents['title'];
$mailer_contents[$id]['description'] = $temp_mailer_contents['description'];
$id ++;
}
}
get_mailer_contents($email_category);
foreach($mailer_contents as $title_cat)
{
echo "==================";
echo "<br>";
echo $title_cat['title'];
echo "<br>";
echo $title_cat['description'];
echo "<br>";
}
The output rendered here is not the final output. I'm using this just for testing purpose.
My problem is, a foreach function(or anything similar that would loop through the array) cannot be part of $message(body of the mail) mailer and I need to have a mechanism as the data is dynamic
Hope I have been clear enough. Do let me know if you need more specifics
You simply need to assign your output to variable and then use it in PhpMailer as in the following code:
$html = '<html><head><meta charset="utf-8" /></head><body>';
foreach ($array as $item) {
$html.= '<p>'.$item['title'].' '.$item['summary']."</p>";
}
$html.= '</body></html>';
// ... setting up phpMailer
$phpMailer->Body = $html;
$phpMailer->AltBody = nl2br(strip_tags($html));
$phpMailer->IsHTML(true); // you need to set HTML format
You also need to use IsHTML method to tell phpMailer to send the content as HTML, you should also set AltBody to display your mail for people who don't want/cannot display it in HTML format. Above I used a very simple way to transform html to text. However you can put here any other text as you want.
I have this:
ob_start(); ?>
<div class="" id="loading">
<?php echo file_get_contents($page["texto"]) ?>
</div> <?php
$content = ob_get_clean();
But I would like to have that template html in a separate file, just like:
$content = file_get_contents('cms/template.php');
Now this won't work because it has php tags inside and when retrieving the string it gets as
how can I achieve this without using a dirty hack like:
$pre = file_get_contents('part1');
$var = file_get_contents($page["texto"]);
$post = file_get_contents('part2');
And adding all them...
It's still hack, but unless you use a templating system like Twig you have no choice:
ob_start();
include 'cms/template.php';
$content = ob_get_clean();
echo $content;
ob_start enables output buffering so nothing gets sent to the browser. We then include the file which will execute PHP normally. We then use ob_get_clean to get the contents of the output buffer (which is your template file). and disable the output buffer, discarding it's contents, as we have the contents in $content.