String too long | using chunk_split - php

I have a heredoc var. in php and sending it via POST to an mail.php - everything worked fine, but since web.de and gmx.net are such tools they won´t accept my mails "String too Long 500..." - using chunk_split solved the problem:
$text1 = chunk_split($text1, 1212,"\r\n");
But now the layout is screwed up sometimes. So instead of Hello there...
It makes an empty space Hello t here...
Any ideas?

It depends on the type of content.
If your content is HTML, one way to handle it is to run the content through tidy::repairString.
If your content is text (without a structure and formatting language), breaking long lines during mail transfer is one of the things quoted-printable encoding does. Run your content through that. Send your message with a header named Content-Transfer-Encoding with value QUOTED-PRINTABLE. Mail clients will automatically put the content back together before displaying it.

Related

How does new line escaping need to formated to get message to display correctly

I have the following saved to my $msg variable. On display my goal was to get it to print formatted correctly with new lines. I am not sure what I could do differently. On a side note I could be going about creating the message all wrong.
$msg ="Hey ".$row['emailName']."\rYou recently signed up to receive email updates from us, and we wanted to verify your email address.\nIf you signed up to receive updates please press the confirmation link below:\nwww.domain.com/validateemail?email=".$row['emailAddress']."&emailID=".$row['emailID']."&emailed=1\nThe request came from".$row['signupIP']."\nIf you did not make this request, you can ignore this email\n \r Thanks,\rKathleen Williams\rHead Trainer";
The issue is that it isn't displaying line breaks.
Problems in your code:
\r is not a new line; \n is the new line character.
www.domain.com/validateemail?... is not an URL. An URL starts with a protocol (http://, https:// etc). Without it, the email client doesn't detect it as an URL and doesn't create a link from it.
There are several ways to write the code to be easy to read and modify. For example, you can use the heredoc syntax for strings. It allows you to write the text on multiple lines without worrying how to write newline characters. Also, PHP parses it for variables and escape sequences the same way it does with double quoted strings.
// The text starting after the line `<<< END` and ending before
// the marker provided after `<<<` is a string.
// It is stored into the $msg variable.
$textBody = <<< END_TEXT
Hey {$row['emailName']}
You recently signed up to receive email updates from us, and we wanted to verify your email address.
If you signed up to receive updates please press the confirmation link below:
http://www.example.com/validateemail?email={$row['emailAddress']}&emailID={$row['emailID']}&emailed=1
The request came from {$row['signupIP']}.
If you did not make this request, you can ignore this email.
Thanks,
Kathleen Williams
Head Trainer
END_TEXT;
If you want to send HTML email you can use the same technique to generate the email body but don't forget that HTML doesn't care about the newlines in the source code. Wrap the text in <p> HTML elements to produce paragraphs and use the <br> HTML element to force a new line inside a paragraph.
The code could be like this:
$htmlBody = <<< END_HTML
<p>Hey {$row['emailName']}</p>
<p>You recently signed up to receive email updates from us,
and we wanted to verify your email address.<br>
If you signed up to receive updates please press this
confirmation link.</p>
<p>The request came from {$row['signupIP']}.<br>
If you did not make this request, you can ignore this email.</p>
<p>Thanks,<br>
Kathleen Williams<br>
Head Trainer</p>
END_HTML;
Different OS use the different symbols for end of line (EOL):
\r\n - CRLF (Windows)
\n - LF (Unix and OS X)
\r - CR (Mac)
If you printing out this message in HTML, then you must change the EOL symbol into <br> tag, because EOL symbols are ignored.
You can use nl2br for converting.

Special characters in file name - download vs delete

I am trying to create a file management system on my website. Problem is with downloading files that contain special characters (other work correctly).
If I use file_exists($mypath) the result is true therefore file exists.
When deleting this file with unlink($mypath) it also works fine.
Only thing that doesn´t work is downloading the file.
The download is done via href link where I echo the path but it somehow converts the characters so the link doesn´t work. The solution is in some conversion but I had no success yet.
I suspect that php is converting the special characters into html entities.
You should use the 'rawurlencode' php method to keep the special characters.
The following link talks about you issue (special chars appearing in file name and wanting to create a link):
http://www.dxsdata.com/2015/03/html-php-mailto-content-link-with-special-characters/
Their solution shows the use of rawurlencode, the following was copied from above link just incase the link goes dead:
Snip start...
Scenario
On your website, you want to offer a link which opens a mail client like Outlook with mail and content suggestion. The content contains a link to a file with special characters in its name, which causes Outlook to break the link, e.g. if it contains spaces or german Umlaute.
Solution
Using PHP, write
<?php
$fullPath = $yourAbsoluteHttpPath . "/" . rawurlencode(rawurlencode($filename));
$mailto = "mailto:a#b.com?subject=File for you&body=Your file: ".$fullPath;
?>
Generate Mail
Note the double usage of “rawurlencode” function. The first one is needed for HTML output, the second one will be needed for the part when Outlook takes the link code into its mail window.
Snip end ;-)

How should i store newlines in a MySQL database?

I'm going around and around in circles in my mind trying to decide how best I should store newlines in a MySQL database.
A bit of background; the user is asked to complete a textarea. This has to stored in a MySQL database before being read back out and the content included in an HTML email.
Should I store them as:
1) String literal - surely this is dangerous and bad practice
2) As a string with \r\n in - when I read this back out of the database its read as 4 characters so nl2br() fails to correctly replace them.
3) As HTML <br /> - as it has to be html entity encoded to be stored it ends up being stored as <br /> so when it gets to the email <br /> is printed rather than an actual newline. Passing it through html_entity_decode() would decode other characters that need to be encoded.
Any help grately appreciated.
Store it as it is but escape first. This is your option 1. When you need to present this data apply whatever function you need to format it. If you need to show it in HTML use nl2br() and htmlentities() functions. That'll work for mail also.
If you have a text area data like this,
$text="Hello Sir,
I am Robert'); Drop table students; --
.....
Yours most obedient pupil
.....";
Just store it as it is after you escape it, or use a prepared statement.
$text = $mysqli->real_escape_string($text);
use the javascript string replace function str.replace(/\n/g,'\\n')
to transform all newlines to escaped literals in the textarea while forming the url or post string.
Retrieving the same using PHP's json_encode() and displaying it through a textarea works well for me.
It depends on application type (how you want to display your text).
If this is HTML e-mail editor - i think html format will be better.
In most CMS (content management systems) HTML format is used (joomla, wordpress, prestashop etc.), because you can just read this from database and send to browser.
Besides - you probably may need other HTML tags anyway (like <b> for bold or <center>).
I think using \r\n format is better for non-web applications, when data is displayed on windows form controls.
One more thing - you may store them in both ways:
- <br> for HTML view
- \r\n for HTML source (to add some newline's and make more readable html source code)
By more readable html i mean this:
<center>
This is header
</center>
<p>
This is paragraph.
<br>
Second line.
</p>
Instead of this:
<center>This is header</center><p>This is paragraph.<br>Second line.</p>

Can non-Western characters be used in HTML "name" attribute?

In an HTML <input>:
Is it an obligation to set name attribute with English characters?
I want to use it later, in $_POST['some_utf8_characters_and_not_english_characters'].
Is it possible to cause a problem later?
According to RFC1866 chapter 3.2.4, an attribute's value can be anything except the value delimiter (single or double quote), and shouldn't contain HTML tag delimiters (< and >).
However, you'll have to test how JavaScript behaves on all browsers (remember your great friend MSIE...) when you try to access a DOM element using name as references. For example: document.anElementWithPersianName or document.forms['aFormWithAPersianName']. So if you use JS to validate, and/or ajax to submit a form, you'll need to be sure that JS is able to handle this character set properly.
In any case, you'll have to ensure that:
your PHP scripts use UTF-8-based functions when it's about string manipulation (I think some functions need to have the charset passed as an argument)
these scripts are themselves saved in UTF-8 files
you correctly set the character set in the HTML header and/or PHP's response header
Best thing to do: create a simple form, do some JS tricks on it, and have a PHP script parse the submitted results and print them.
This is successfully working in one of my websites. No problems.
<input name="UTF_word" />
$_POST['UTF_word']
Both does not give any problems in client side, including jquery (not checked in IE) or server side.

add return carriage from input field after post in php

Hello all I am working on a project at work where people can send emails from a certain part of the site. There is a body textarea field which represents the body of an email. Return carriages show up on the textarea but when I POST the data and send the email everything shows up on one line. How would I express this when the email is sent so that the return carriages are included in the email format. I am using PHP to send textarea information to my PHP script via the POST array. Any help would be greatly appreciated
It depends on format of letters. For html, use this:
$text = nl2br($text);
plain text should work fine with usual symbols.
are you sending html mail? in that case nl2br() is your friend (http://php.net/nl2br). Otherwise newlines should just show up in the source of the mail...
You could do this:
$trans[chr(15)] = "\n";
$the_string = strtr($the_string, $trans);
Should do the trick.
What I understand is that the end user, who reads your mail, reads it in HTML format where as the email is composed in text. Things you can do is to insert line breaks or <br> tags for all carriage returns. I would in fact suggest you to use something like PHPMailer which takes care of mos of these things.

Categories