I know you are able to do
<?php if ($a == 5): ?>
A is equal to 5
<?php endif; ?>
but is it possible to create a string outside a php script?
<?php $email_body = ' ?>
<body>
<h1>This is my email</h1>
<br/><br/>
<div>
<span style="font-family: sans-serif; color: #404040;">This is a test email</span>
</div>
</body>
<?php '; ?>
<?php
email($email_body, $to, $from);
?>
I want to keep the script formatted in html... but it is kinda ugly when you create a string with html... you can't really format it well.
You're looking for a Heredoc (or a Nowdoc, in which variables are not interpreted. More info in the same section of the PHP documentation linked)
<?php $email_body = <<<EOS
<body>
<h1>This is my email</h1>
<br/><br/>
<div>
<span style="font-family: sans-serif; color: #404040;">This is a test email</span>
</div>
</body>
EOS;
email($email_body, $to, $from);
?>
You can do it using buffers (this way you can use php in your html) :
<?php
ob_start();
?>
<body>
<h1>This is my email the <?=date('d/m/Y')?></h1>
<br/><br/>
<div>
<span style="font-family: sans-serif; color: #404040;">This is a test email</span>
</div>
</body>
<?php
$html = ob_get_contents();
ob_end_clean();
?>
Do you want this?:
<php
$email_body = '<body>
<h1>This is my email</h1>
<br/><br/>
<div>
<span style="font-family: sans-serif; color: #404040;">This is a test email</span>
</div>
</body>';
email($email_body, $to, $from);
?>
Related
Why I cant display the data I echo from the database in a PDF file ?
I used DOMPDF to convert my html. Anyone please help.
Here is the PDF result
PDF result image
Here is connection code
<?php
$id = $_GET['id'];
$conn = pg_connect("host=localhost dbname=mydatabase user=postgres password=admin");
if (!$conn) {
echo "An error occurred. cannot connectect to localhost.\n";
exit;
}
$sql = pg_query($conn, "SELECT * FROM mytable WHERE reference_no = $id ");
if (!$sql) {
echo "Error Ariel 7:50 !!!.\n";
}
while ($row = pg_fetch_assoc($sql)) {
$reference_no = $row['reference_no'];
$wo_name = $row['wo_name'];
}
Here is my convertion code:
error_reporting(0);
require_once ('xdompdf/dompdf_config.inc.php');
$pdf_content='
<style type="text/css">
div.refno{padding-left: 190px;}
.line{padding-left: 260px; padding-top: -37px;}
</style>
<body>
<div>
<h4> Work Order Form </h4>
<p> Reference No. <div class="refno"><?php echo $reference_no;?></div> </p>
<p class="line">:_________________________________________________</p>
<p> Work Order Name <div class="woname"><?php echo $wo_name;?></div> </p>
<p class="line">:_________________________________________________</p>
</div>
</body>'
;
$name = date("Ymd").rand().'.pdf';
$reportPDF=createPDF(12, $pdf_content, 'activity_Report', $name );
function createPDF($pdf_userid, $pdf_content, $pdf_For, $filename){
$path='xdompdf/';
/*$rndNumber=rand();
$filename=$pdf_userid.date("Ymd").$rndNumber.'.pdf';*/
$dompdf=new DOMPDF();
$dompdf->load_html($pdf_content);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($path.$filename, $output);
return $filename;
}
echo '<a href="xdompdf/'.$name.'" > Download </a>';
?>
Please Help me how to display the data.
Change $pdf_content to
$pdf_content='
<style type="text/css">
div.refno{padding-left: 190px;}
.line{padding-left: 260px; padding-top: -37px;}
</style>
<body>
<div>
<h4> Work Order Form </h4>
<p> Reference No. <div class="refno">' . $reference_no . '</div> </p>
<p class="line">:_________________________________________________</p>
<p> Work Order Name <div class="woname">' . $wo_name . '</div> </p>
<p class="line">:_________________________________________________</p>
</div>
</body>';
I think you just need to concat your data in $pdf_content :
$pdf_content='
<style type="text/css">
div.refno{padding-left: 190px;}
.line{padding-left: 260px; padding-top: -37px;}
</style>
<body>
<div>
<h4> Work Order Form </h4>
<p> Reference No. <div class="refno">'.$reference_no.'</div> </p>
<p class="line">:_________________________________________________</p>
<p> Work Order Name <div class="woname">'.$wo_name.'</div> </p>
<p class="line">:_________________________________________________</p>
</div>
</body>';
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 7 years ago.
I am trying send email using php mail() function. It sends email but only HTML Part and not including the dynamic values through php. Any help will be appreciated.
I am trying to make it work but it is not working. I have tried multiple variations but still it only sends HTML part.
$to = $vendor_email;
$subject = "";
$mime_boundary = '<<<--==+X['.md5(time()).']';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-Type: multipart/mixed;';
$headers[] = ' boundary="'.$mime_boundary.'"';
$message = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/home.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<title>BuyBusTours</title>
<style>
body{
font-family: Calibri,Candara,Segoe,"Segoe UI",Optima,Arial,sans-serif;
}
h4{
text-align: center;
}
#wrapper{
width: 60%;
}
hr{
border-color: black;
border-width: 1px;
}
label{
display: inline-block;
width: 250px;
}
</style>
</head>
<body>
<div class="container" id="wrapper">
<h3 style="text-align: center; background-color: orange; height: 60px; padding-top: 18px;">Booking Confirmation for Order Number: { <?php echo $order_id;?> }</h3>
<h4>Dear <?php echo $vendor_name ?></h4><br>
<h4>Thank you for Choosing BuyBusTours</h4>
<hr>
<p style="color:red;">This is the confirmation email that we have received your order and it is processing. Please do not confuse it with your e-ticket.</p>
<p>We have received your order and we are working on it. As soon as your order is confirmed we will send your E-Ticket. Complete information about your tour and other important details will be mentioned on your E-Ticket. If in case your tour is sold out or it is not operating we shall refund your complete payment.
</p>
<p>This is <b>NOT A CONFIRMATION</b> that you have reserved a seat on the bus. We are working on your order and as soon as it is confirmed we will send your e-ticket.</p>
<hr>
<h3 style="color:#F60">Order Details</h3>
<label>Tour Name:</label><?php echo $tour_name;?><br>
<label>Departure Date</label> <?php echo $dept_date;?> <br>
<label>Departure Location:</label> <?php echo $dept_loc;?> <br>
<label>Tour Code:</label> <?php echo $tourcode;?> <br>
<label>Vendor Tour Code:</label> <?php echo $vendorcode;?> <br>
<label>Number of People:</label> <?php echo $no_of_adult + $no_of_child;?> <br>
<?php if (!empty($no_of_rooms)) { ?>
<?php for ($i = 1 ; $i <= $no_of_rooms ; $i++) {?>
<?php if ($i == 1) {?>
<label style="color:#F33">Room <?php echo $i;?></label> <br>
<?php if (!empty($room1traveler1)) {?>
<label>Traveler 1: </label>
<?php echo $room1traveler1;?><br>
<?php } ?>
<?php if (!empty($room1traveler2)) {?>
<label>Traveler 2: </label>
<?php echo $room1traveler2;?><br>
<?php } ?>
<?php if (!empty($room1traveler3)) {?>
<label>Traveler 3: </label>
<?php echo $room1traveler3;?><br>
<?php } ?>
<?php } ?>
<?php if ($i == 2) {?>
<label style="color:#F33">Room <?php echo $i;?></label><br>
<?php if (!empty($room2traveler1)) {?>
<label>Traveler 1: </label>
<?php echo $room2traveler1;?><br>
<?php } ?>
<?php if (!empty($room2traveler2)) {?>
<label>Traveler 2: </label>
<?php echo $room2traveler2;?><br>
<?php } ?>
<?php if (!empty($room2traveler3)) {?>
<label>Traveler 3: </label>
<?php echo $room2traveler3;?><br>
<?php } ?>
<?php } ?>
<?php if ($i == 3) {?>
<label style="color:#F33">Room <?php echo $i;?></label><br>
<?php if (!empty($room3traveler1)) {?>
<label>Traveler 1: </label>
<?php echo $room3traveler1;?><br>
<?php } ?>
<?php if (!empty($room3traveler2)) {?>
<label>Traveler 2: </label>
<?php echo $room3traveler2;?><br>
<?php } ?>
<?php if (!empty($room3traveler3)) {?>
<label>Traveler 3: </label>
<?php $row["room3_traveler3"];?><br>
<?php } ?>
<?php } ?>
<?php if ($i == 4) {?>
<label style="color:#F33">Room <?php echo $i;?></label><br>
<?php if (!empty($room4traveler1)) {?>
<label>Traveler 1: </label>
<?php echo $room4traveler1;?><br>
<?php } ?>
<?php if (!empty($room4traveler2)) {?>
<label>Traveler 2: </label>
<?php echo $room4traveler2;?><br>
<?php } ?>
<?php if (!empty($room4traveler3)) {?>
<label>Traveler 3: </label>
<?php echo $room4traveler3;?><br>
<?php } ?>
<?php } ?>
<?php } }?>
<hr>
<h3 style="color:red; text-align:center">Total Amount Charged: <?php echo $total_price;?></h3>
<h3 style="text-align: center; background-color: orange; height: 60px; padding-top: 18px;">Contact us at: 0345-1272211</h3>
</div>
</body>
</html>';
$message = html_entity_decode($message);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
<h4>Dear <?php echo $vendor_name ?></h4><br>
Should be
<h4>Dear '.$vendor_name.'</h4><br>
The reason being is that in the mail body you are already in <?php ?> tags, and so there is no needs to repeat them.
What's happening here is you are ending $message='.......... with Dear '
and then concatenating with the . $variable . (concatenate again) '</h4> Resume writing out $message.
Repeat this for all of the times you use a variable within a string.
Anything in there such as an if() statement or a for() loop, you should calculate these before you start defining any of the email strings, and assign each of their results an individual $var and assign just like I mention above. This will also make your code less messy, easier to navigate and thus adjust if and when you need to.
Your email message is being sent inside single quotes, ', so no PHP variables will be interpolated inside it. That will fix quite a few of the problems immediately, but a bigger problem is that you appear to have PHP statements inside there too, such as loops and conditionals – you can't put those inside strings.
What you need to do is pre-calculate your text, i.e. moving all those loops and conditionals out of your HTML message text, so that whatever they calculate end up just being variables. You should then switch to using double-quoted strings (or, in this case, heredoc is probably easier) to put those values into your message.
I would like to mail the content of a Web page that looks like:
<html>
<body>
<?php
function sendPageContentToEmail($destEmail)
{
ob_start();
$buffer = ob_get_contents();
ob_end_clean();
$subject = 'Subject name';
mail($destEmail, $subject, $buffer);
}
?>
<div style="width:400px; margin:0 auto;">
<p>
Name: <?php print($customerData['customer_name']); ?>
</p>
<p>
....
</p>
</div>
</body>
</html>
<?php
sendPageContentToEmail($customerData['customer_email']);
//erase all temp data
session_destroy();
?>
$buffer is always empty (ob_get_content()) regardless of where the sendPageContentToEmail() is called.
Where should this function be called (assuming that it is the right way to do it)?
What ob_start does is start caching all the output, so, if you output something before calling it, that part won't be cached.
Just at the start, before <html> do a <?php ob_start(); ?>
I am unsure as too why I am getting the following errors when trying to pass data through to my html email template -> The PHP errors are coming from the view.
Undefined variable: companyName -> line 9
Undefined variable: firstName -> line 12
Controller:
function _userRegEmail($activateCode,$email,$firstname,$lastname){
$data['companyName'] = $this->core_model->companyDetails()->coreCompanyName;
$data['companyEmail'] = $this->core_model->companyDetails()->coreContactEmail;
$data['companyContact'] = $this->core_model->companyDetails()->coreContactName;
$data['firstName'] = $firstname;
$data['lastName'] = $lastname;
$data['email'] = $email;
$data['activateCode'] = $activateCode;
$this->email->from($this->core_model->companyDetails()->coreContactEmail, $this->core_model->companyDetails()->coreCompanyName);
$this->email->to($email);
$this->email->subject($this->core_model->companyDetails()->coreCompanyName 'User Registration Confirmation');
$messageContent= $this->load->view('email_templates/userReg','', TRUE);
$this->email->message($messageContent);
$this->email->send();
echo $this->email->print_debugger();
}
View:
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title/>
</head>
<body>
<div class="container" style="width: 600px;height: 100%;margin: 0 auto;font-family: Arial, "MS Trebuchet", sans-serif">
<div class="header" style="width: 100%">
<h1 style="text-align: center;color: #00afd8"><?php echo $companyName; ?></h1>
</div>
<div class="content">
<h2 style="font-size: 15px">Hello <?php echo $firstName; ?></h2>
<p style="margin-left: 15px">Thank you for signing up to Farm Ads.</p>
<p style="margin-left: 15px">Could you please click <a href="<?php base_url(); ?>users/confirm/"<?php $activateCode; ?>>here</a> to activate your account.</p>
<div class="from">
<p class="bold" style="margin-left: 15px;font-weight: bold;font-size: 12px">Regards,</p>
<p style="margin-left: 15px;font-weight: bold;font-size: 12px"><?php $companyContact; ?></p>
</div>
</div>
</div>
</body>
</html>
If you want to use the variables you have set in $data in the view you shouldn't do:
$messageContent= $this->load->view('email_templates/userReg','', TRUE);
But:
$messageContent= $this->load->view('email_templates/userReg', $data, TRUE);
This makes sure you send the variables to the view so they can be used in there like you are trying already.
I generate all my html on my page with ajax calls and never refresh my page. My setup is something like
HTML: <a id="user_click_here">test link</a>
JQUERY:
$("#user_click_here").live("click", function(event){
console.log("click on test link");
ajax_function("action=return_some_html");
});
The ajax function calls php, where I create the html. What I struggle with is to generate html with php. I try to use this:
PHP:
$html = <<<HTML
<div class="box">
<h2>
$text[0]
</h2>
<div class="block">
<div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">
HTML;
$html .= '<p>Username: <a id="username">' . $_SESSION['username'] . '</a></p>';
$html .= <<<HTML
<div style="float:left; width: 30%; margin:5px;">
<p>
Level:<br />
Weapon:<br />
Power:<br />
Bullets:<br />
</p>
</div>
<div style="float:right; width: 60%; margin:5px;">
<p>
HTML;
$html .= '<b id="level">empty</b><br/>';
$html .= <<<HTML <---ERROR HERE unexpected '<<'
Weapon blabla<br />
2 - 5<br />
3/6<br />
</p>
</div>
</div>
I tend to just try and fail until it works with this <<<WHATEVER (dont remember what it's called). Like now where I get an unexpected '<<' error for some reason.
Do I have to use this method:
$html = '<div class="box">
<h2>
' . $text[0] . '
</h2>
<div class="block">
<div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">';
?
What is the best way to save html in php and send it back to jquery.
I send the html like this now:
PHP:
$data = array("html" => $html);
return json_encode( $data );
Off course I want it compressed as much as possible, preferably without any stuff like this: \n\t\t\t\t to take up space.
EDIT:
Ok. I don't think everybody noticed the fact that this is an ajax call and I have to return a json element. I cannot do the common <?php php code ?> html code <?php more php ?> some html
It looks like your last HEREDOC (that's what the <<< syntax is called) is unclosed, missing it's
HTML;
And don't forget that the final HTML; cannot have any whitespace on the same line before or after it.
However, you're going about it all wrong. The great thing about the HEREDOC syntax is that you can embed all your variables into it without requiring any concatenation. Just create the whole thing in one HEREDOC and echo it out to jQuery. There's no need to make it JSON if you are just going to use it as HTML when received by the AJAX call.
All your code above belongs inside one $html = <<<HTML block. Enclose all your complex variables like $_SESSION['whatever'] in {} {$_SESSION['whatever']}.
$html = <<<HTML
<div class="box">
<h2>
{$text[0]}
</h2>
<div class="block">
<div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">
<p>Username: <a id="username"> {$_SESSION['username']}</a></p>
<div style="float:left; width: 30%; margin:5px;">
<p>
Level:<br />
Weapon:<br />
Power:<br />
Bullets:<br />
</p>
</div>
<div style="float:right; width: 60%; margin:5px;">
<p>
<b id="level">empty</b><br/>
<!--etc -->
<!--etc -->
HTML;
// Now just echo it back to the AJAX caller as HTML
echo $html;
exit();
Don't use HEREDOC. It's messy, and should only be used when you need to store long strings in a variable.
Try writing out the HTML page you expect to be returned as if it was a normal HTML page. Where you need PHP output (a variable, array value, etc.), begin a PHP tag, echo the value, and then end the PHP tag. This is much easier than trying to use heredoc.
<div class="box">
<h2><?php echo $text[0]; ?> </h2>
<div class="block">
<div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">
<p>Username: <a id="username"><?php echo $_SESSION['username']; ?></a></p>
<div style="float:left; width: 30%; margin:5px;">
<p>Level:<br />Weapon:<br />Power:<br />Bullets:<br /></p>
</div>
<div style="float:right; width: 60%; margin:5px;">
<p><strong id="level">empty</b><br />Weapon blabla<br />2 - 5<br />3/6<br /></p>
</div>
</div>
If you're worried about line breaks (you shouldn't be), you can add an output handler that removes them.
ob_start("clean_linebreaks");
function clean_linebreaks($input) {
return str_replace("\n", "", str_replace("\r", "", $input));
}
Also, may I suggest using tables instead of floated divs. The level, weapon, and power will be easier to line up with their values if you use a table.