Syntax error, unexpected T_SL - mime

I'm fairly new to php and I'm using a script that creates a function called the "mime_mailer" which essentially allows me to use PHP to send emails that are able to be designed with CSS instead of being merely plain text.
Yet, in my registration script, I try to write some code that sends a CSS email, but I get an error saying that there's a syntax error. Could someone please fill me in on this?
$subject = "Your Red-line Account";
$css = "body{ color: #090127; background-color: #f0f0f0; }";
$to = $usercheck;
//Message
$message =<<<END
<html>
<head>
<title>
Red-line
</title>
</head>
<body>
<p>
Hi $first_name,
</p>
<p>
Your Red-line account is almost complete. To finish, go to <a href='www.thered-line.com'>The Red-line</a> and enter your eight digit confirmation code.
</p>
<p>
Your confirmation code is: <b>$code</b>
</p>
<p>
Sincerely,
</p> <br />
<p>
The Red-line Operator
</p>
</body>
</html>
END;
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= "From: The Red-line <messages#theredline.com>\r\n";
$headers .= "To: $first_name $last_name <$usercheck>\r\n";
// Mail it
require_once("function_mime_mailer.php");
mime_mailer($to, $subject, $message, $headers, NULL, $css);
}
Here is the code for the "function_mime_mailer.php" file.
if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) send_404(); // stop http access to this file
function mime_mailer($to, $subject, $message, $headers = NULL, $attachments = NULL, $css = NULL)
{
if(!preg_match('/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*#([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a- z]{2,6})$/', $to)) return FALSE;
if(preg_match('/<(html|head|body|div|a|h|p|table|br|img|b|hr|ol|ul|span|pre|i|form)[^>]*[^>]*>/i', $message)) $html = TRUE;
if(stristr($message, '<body')) $message = stristr($message, '<body');
$message = delete_local_links($message);
if(empty($headers)){
$headers = "MIME-Version: 1.0\n";
}else{
$headers.= "\nMIME-Version: 1.0\n";
}
if(empty($html)){
$result = plain_text($message);
}elseif(isset($html) and $html == TRUE){
if(!isset($css)) $css = NULL;
if(preg_match('/<img[^>]+>/i', $message)){
$result = multipart_related($message, $css);
}else{
$result = multipart_alternative($message, $css);
}
}
$result['message'] = delete_non_cid_images($result['message']);
if(!empty($attachments)){
$parts = attachments($attachments);
array_unshift($parts, implode('', $result));
$result = multipart_mixed($parts);
}
$headers = $headers.$result['headers'];
//print '<pre>'.htmlspecialchars($headers.$result['message']).'</pre>';exit;
if(mail($to, $subject, $result['message'], $headers)) return TRUE;
return FALSE;
}
?>

Just had the same problem.
Turned out to be content on the same line as my opening HERDEOC
wrong example
echo <<<HEREDOC code started on this line
HEREDOC;
correct example
echo <<<HEREDOC
code should have started on this line
HEREDOC;
Hope this helps someone else!

Have a look at the List of Parser tokens.
T_SL references to <<.
You should not use tabs or spaces before you use END;. Have a look at this huge warning.

A side note, but might well help someone: a bad git merge can cause this. Consider:
function foo
<<<<<<< HEAD
$bar = 1;
<<<<<<< e0f2213bc34d43ef
$bar = 2;
The PHP parser would produce the same error.
Source: just got bit by this ;)

It had the same exact same issue but mine was because I had whitespace at the end of my heredoc on the top line:
$var = <<< HTML(whitespaces here caused the error)
stuff in here
HTML;
Source: http://realtechtalk.com/_syntax_error_unexpected_T_SL_in_PHP_Solution-2109-articles

What version of php are you using?
The nowdoc syntax is only valid since PHP 5.3.0.
See the manual:
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

There is a bug in function_mime_mailer.php:
if(empty($headers)){
$headers = "MIME-Version: 1.0\n";
}else{
$headers.= "\nMIME-Version: 1.0\n";
}
should be
if(empty($headers)){
$headers = "MIME-Version: 1.0\r\n";
}else{
$headers.= "\r\nMIME-Version: 1.0\r\n";
}
also, if you include the MIME-Version header, then the function will include it once more - effectively having two of them.

Problem is related to needless spaces/tabs in code...
Ran into this after using get merge feature/branchname. We use Prettier as formatter, so after accepting both changes - saving the file, fixed the problem.

Related

New Version of Chrome cannot render dd function in network preview window Laravel

I want to share live saver with people who debugging with dd() and have to refresh every time because it gets status 200 and stuck.
If you want to change status to 500 for your ajax requests you just need to update
project/vendor/symfony/var-dumper/Dumper/HtmlDumper.php
class on line 111 where dump() method exists.
Just add line http_response_code(500); at the beginning of function. It works for Laravel 5.6 version.
Answer I found in: https://github.com/laravel/framework/issues/21808
See my solution at
https://gist.github.com/fontenele/7625cb71c0a8356213abc727278b48d5
I created a new helper, get the content, replaced div for span because google does not permit <DIV> tags in DevTools.
use Illuminate\Support\Debug\Dumper;
if (!function_exists('_dd')) {
function _dd(...$args)
{
$content = '<span>';
ob_start();
foreach ($args as $x) {
(new Dumper)->dump($x);
}
$content .= ob_get_contents();
ob_end_clean();
$content.= '</span>';
$content = str_replace(['<div', '</div>'], ['<span', '</span>'], $content);
response()->make($content, 500, ['Content-Type' => 'text/html'])->send();
die(1);
}
}
Updated
Lumen:
function _dd(...$args)
{
$content = '<span>';
ob_start();
dump(...$args);
$content .= ob_get_contents();
ob_end_clean();
$content .= '</span>';
$content = str_replace(['<div', '</div>'], ['<span', '</span>'], $content);
response()->make($content, 500, ['Content-Type' => 'text/html'])->send();
die(1);
}

ReCaptcha For Newbies

I've got ReCaptcha working but despite reading the documentation and the answers posted here, I'm still at a loss for setting up the server side. My HTML form calls <form id="contactForm" class="well" method="POST" action="php/contactform.php">.
What and where do I place the server-side recaptcha in this file? (I meant it when I titled this newbie. I really need explicit instructions):
<?php
if($_POST){
// response hash
$response = array('message'=>'');
}
try {
// Get values from form
$name=$_POST['cname'];
$email=$_POST['cemail'];
$subject=$_POST['csubject'];
$message=$_POST['cmessage'];
$formcontent="From: $name \n Email: $email \n Subject: $subject \n: $message";
$recipient = "rabbidubrow#fivegates.org";
$subject = "KHF Contact Form";
$mailheader = "From: $email \r\n";
$send_contact=mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
// let's assume everything is ok, setup successful response
$response['type'] = 'success';
$response['message'] = 'Thank you! We will be in touch shortly.';
} catch(Exception $e){
$response['type'] = 'error';
$response['message'] = $e->getMessage();
}
// now we are ready to turn this hash into JSON
print json_encode($response);
exit;
?>
You will need
1. Include your recaptcha.php
2. Declare your private and public keys
3. Check for POST of your captcha. If it success, give a response, if it fails, catch the exception.
Below is one of my scripts that was done up for your reference.
require_once('assets/config/recaptchalib.php');
$publickey = "xxxx";
$privatekey = "xxxxx";
if ($_POST["recaptcha_response_field"]) {
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
$continue = true;
}
}

Add image and hyperlink in auto response email (after form submission)

I am new to php and building my first project.
I am creating a auto response email which will be sent to user after form submission.
The auto response email need to have content as follows,
Logo image
Thank you for contacting us.
Here you can view our case study.(need to link pdf file to view**)
I manage to get text. but not able to add image and hyperlink.
tried using variable to store image url but code is visible instead of an image.
i request if someone Please guide me.to solve this.
<?php
$to = $_POST['email'];
$from = "info#company.com";
$headers = "From: company";
$subject = "Thank you for contacting us.";
//$img='<img src="http://www.http://company.com/images/logo.jpg"/>';
$linkedin='Linkedin';
$twitter='Twitter';
$message=
"Dear ".$firstname." Thank you for contacting us.
Here you can view our case study.
www.company.com/data/casestudy.pdf
www.company.com | info#company.com | Linkedin | Twitter
";
$mailsent = mail($to, $subject, $message, $headers);
?>
First, In $header add content type to HTML
$headers .= "Content-type:text/html;charset=UTF-8";
http://php.net/manual/en/function.mail.php
This will set mail Content type to HTML, Now you can add all Tags and link of HTML without using variables, So full code will be like this
<?php
$to = $_POST['email'];
$from = "info#company.com";
$headers = "From: company";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$subject = "Thank you for contacting us.";
$message="<img src='http://company.com/images/logo.jpg'/>
<br/>Dear ".$firstname." Thank you for contacting us.
Here you can view our case study.
<a href='www.example.com'>File Name </a>
www.company.com | info#company.com |<a href='www.linkedin.com'>Linkedin</a> |<a href='twitter.com'> Twitter</a>
";
$mailsent = mail($to, $subject, $message, $headers); ?>
And don't get confused in single-quotes and double-quotes used here also i assume your $firstname has some value.

PHPmailer - After Form submitted the Inbox message displays HTML numeric code

The problem is with this PHP code that goes through an AJAX function and sends the content of a Form to a specific e-mail.
The essential part of the script is working, but some text appears to be converted in the Inbox when the e-mail arrives. Specifically, single-quotes (') are replaced with double-quotes (").
After searching the web and browsing SO, I couldn't find an answer. Any help would be deeply appreciated.
The code follows.
<?php
require '../config/squareconfig.php';
if($_POST)
{
//check if ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
//check $_POST vars are set, exit if any missingc
if(!isset($_POST["usercName"]) || !isset($_POST["usercEmail"]) || !isset($_POST["usercMessage"]))
{
die();
}
//Sanitize input data using PHP filter_var().
$user_cName = filter_var($_POST["usercName"], FILTER_SANITIZE_STRING);
$user_cEmail = filter_var($_POST["usercEmail"], FILTER_SANITIZE_EMAIL);
$user_cMessage = filter_var($_POST["usercMessage"], FILTER_SANITIZE_STRING);
if(strlen($user_cName)<4) // If length is less than 4 it will throw an HTTP error.
{
header('HTTP/1.1 500 Name is too short or empty!');
exit();
}
if(!filter_var($user_cEmail, FILTER_VALIDATE_EMAIL)) //email validation
{
header('HTTP/1.1 500 Please enter a valid email!');
exit();
}
if(strlen($user_cMessage)<5) //check emtpy message
{
header('HTTP/1.1 500 Too short message! Please enter something.');
exit();
}
//proceed with PHP email.
$headers = 'From: '.$user_cEmail.' ';
$templatemail = PHP_EOL . PHP_EOL . 'User E-mail:' . PHP_EOL . $user_cEmail;
#$sentMail = mail($squarecmail, $squarecsubject, $user_cMessage . PHP_EOL . $templatemail, $user_cName);
if(!$sentMail)
{
header('HTTP/1.1 500 Could not send mail! Sorry..');
exit();
}else{
echo $squarectymessage . '<br>' . '<div class="subagain">' . $squarecontagain . '</div>';
?>
<script type="text/javascript">
$('#contact_form').fadeOut();
</script>
<?php
}
}
I'm uncertain about the "problem", but a reasonable suggestion might be to send the e-mail as HTML, since filter_var() returns html-entities for non-alphanumeric characters. Specify the content type for the e-mail message.
$headers = "From: yomama#jokes.com\r\n";
$headers .= "Reply-To: arenot#always.funny\r\n";
$headers .= "CC: yomamasobig#whenshewearsayellowcoatppleyell.taxi\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mail_sent = #mail($sender, $subject, $message, $headers);
The above suggestion is based on the fact that FILTER_SANITIZE_STRING causes html-unicode-encoding (or some such) on the mentioned characters.
$ php-shell
PHP-Shell - Version 0.3.1, with readline() support
(c) 2006, Jan Kneschke <jan#kneschke.de>
>> echo filter_var('\' -- "', FILTER_SANITIZE_STRING);
' -- "

Mail function not sending in HTML format

HUGE EDIT!!!!!!!!!
Ok, so it's not the HTML or whatever. When I paste it raw in the PHP message variable, it sends perfectly formatted. It's something with the textarea box paste part... that's making it not work properly. Is it the newline thing?.... Hmmmmm
Ok so this has been a frustrating day for me. I'm trying to send HTML emails where I can paste it in the message form textarea. Well I stripped it down to the barebones and it will NOT let anything above the body and after the body be put into HTML.
It prints this:
<html> <head> <title>HalfOffDeals - Thank you!</title> </head> <body>
Then it spits out the body... although unformatted with no style.
Then this afterwards:
</body> </html>
I tried putting the inline CSS how you're suppose to with the style tag and it just omits that completely. I'm using CodeIgniter and I don't know if it strips it automatically which it shouldn't.. I have a form that says (TO, SUBJECT, and MESSAGE). And in the message part, you paste your HTML email template and it's suppose to send to the to email.
And another question I had was how to add '' to the headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
I'm just confused on why my email isn't accepting HTML. This is how I have it setup:
public function send_email() {
$to = $this->input->post('to');
$subject = $this->input->post('subject');
$message .= $this->input->post('message');
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
// Additional headers
$headers .= 'To: Customer <'.$to . "\r\n";
$headers .= 'From: noreply#intellectproductions.com <noreply#intellectproductions.com>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
}
Any ideas?
HERE IS THE EMAIL TEMPLATE: http://pastebin.com/D04cLXe4
$headers .= 'To: Customer <'.$to . "\r\n";
change to
$headers .= 'To: Customer <'. $to .'>'."\r\n";
and try:
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
change to
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
your server is linux, newline is "\n" if windows "\r\n"
I see that you are indeed using CodeIgniter -- so, I recommend using CodeIgniter's Email class.
Try changing your function to this:
public function send_email() {
$to = $this->input->post('to');
$subject = $this->input->post('subject');
$message .= $this->input->post('message');
$this->load->library('email');
// To send HTML mail, set the appropriate mailtype
$this->email->set_mailtype('html');
// setup the message
$this->email->to($to);
$this->email->from('noreply#intellectproductions.com');
$this->email->subject('Put the subject here');
$this->email->message($message);
// Mail it
$this->email->send();
}
This will do the right thing to handle HTML emails even if they have attachments.
Save your code to what matters, you don't need to do all this work. ;)
To send html in email you can use default CI email settings;
$this->load->library('email');
$config = array(
'mailtype' => 'html',
'newline' => '\r\n',
'charset' => 'utf-8' //default charset
);
$this->email->initialize($config);
$this->email->from($sender);
$this->email->to($receiver);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
For more details see CI email class.
Also try to use $this->email->print_debugger() to see email errors/
Back when I used the PHP mail function, I also ended up with some problems. Then I met the class phpmailer and my problems were over. Take a good look at it. It's worth it.

Resources