My message on from parameter comming with angles quotes, like that:
MyFromName <New message!>
I try $mail->ClearAllRecipients(), but not work.
Any tips?
Nope. You can't do this. It's part of the email specification, which says a mailbox (which is the thing you're talking about) is made up of:
mailbox = name-addr / addr-spec
name-addr = [display-name] angle-addr
angle-addr = [CFWS] "<" addr-spec ">" [CFWS] /
obs-angle-addr
That means an address can be in one of two forms when it appears in a header; as a name and address:
User Name <user#example.com>
Or as just an address:
user#example.com
Nearly everything uses the former, and usually if the name is given, that is displayed in preference to the address part, however, that is entirely up to the client application you're using to display the message, over which you have no control at all.
If you remove the angle brackets and keep the name, your message will never arrive because it's an invalid format.
You can use a replace function, for example;
function removeAngleBrackets($from)
{
// Create an array of things to replace
// Use both standard and html encoded to cover basis (you could use the ASCII too)
$replace = array("<", ">", "<", ">");
// Create the variable to replave the strings with
$replace_with = "";
// Replace this in the string and return the value when done
return str_replace($replace, $replace_with, $from);
}
Calling this on your string will remove the angle braces
Related
I have a html form, where the user can write his / her own last name and send it to an php file, which checks if all is correct.
Now I would like to make an auto correction, for this case:
Last name: maX-poWEer
this should be corrected as follows
Max-Power
the first letters should be allways uppercase, and the others should be lowercase:
strtolower("maX-poWEer");
New Result:
max-power
But how can I realize the first letters of each name as uppercases?
ucwords() doenst work in this case
Use the mb_convert_case() function, this function can handle utf-8 characters, which is useful when dealing with international names.
$name = "maX-poWEer";
$formatted_name = mb_convert_case($name, MB_CASE_TITLE, "UTF-8");
echo $formatted_name; // Output: "Max-Poweer"
The contents of stdin is getting corrupted with word wrapping and trailing "=" throughout which obviously breaks the URL that I need to post.
I need to extract a URL/link from an email then post the URL. So, I'm piping my email to a php script in cpanel using a standard code snip I've seen all over the internet:
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) { $email .= trim(fread($fd, 1024)); }
fclose($fd);
Then dumping the contents of the email to a file "pipemail.txt" for now to inspect it and make sure it's all working properly.
$fdw = fopen("pipemail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);
The output is looking like this:
...
<table style=3D"width:100%" cellpadding=3D"0" cellspacing=3D"0" border=3D"0=
"><tbody><tr><td><table style=3D"background-color:#ffffff;color:#3c445a;fon=
t-family:arial;font-size:10px;font-weight:bold;width:100%" cellpadding=3D"0=
" cellspacing=3D"0">
...
I have been working on this for over a day now and I'm completely stumped. I've tried trimming the trailing "=" from incoming lines and it does not give me the expected result. Instead it seems to remove random "=" from seemingly random locations in the content. I am guessing that it is not random but it only seems so because it's not what I expect. It's probably only removing it if it happens to be the last character of the 1024 k/char line but if that is true then where else is the wordwrapping coming from? I don't know enough about how this works to trouble shoot this myself.
Why is it wrapping? Where are the "=" coming from? Does anyone have any suggestions?
Emails are commonly encoded in the quoted printable format (http://en.wikipedia.org/wiki/Quoted-printable)
You can decode it using quoted_printable_decode() - this is done automatically by your email client, which is why it looks like php is adding those character.
http://www.php.net/manual/en/function.quoted-printable-decode.php
from an email
and
trailing “=” on each line
means that the email is probably quoted-printable encoded. You need to parse the message properly and run the body part through quoted_printable_decode().
There are Content-Type headers that will tell you what kind of encoding is used, and probably MIME parts and boundaries that you'll need to deal with.
Your data is in quoted-printable format, use quoted_printable_decode to decode it:
$email = quoted_printable_decode(file_get_contents("php:://stdin"));
My code is:
$rawhtml = file_get_contents( "site url" );
$pat= '/((http|ftp|https):\/\/[\w#$&+,\/:;=?#.-]+)[^\w#$&+,\/:;=?#.-]/i';
preg_match_all($pat,$rawhtml,$matches1);
foreach($matches1[1] as $plinks)
{
$links_array[]=$plinks;
}
After testing several situations I noted that the function had some "leaks". The link gets broken if there is whitespace.
For example I have this text URL in a variable:
$rawhtml = " http://www.filesonic.com/file/2185085531/TEST Voice 640-461 Test Cert Guide.epub
"
The result should be one link by line:
http://www.filesonic.com/file/2185085481/TEST Voice (640)+461 Test Cert Guide.pdf
but the result is
http://www.filesonic.com/file/2185085531/TEST
Sometimes extracted links also contains , or ' or " at the end. How to get rid of these?
how to get rid of those commas,quotes or double quotes from the extracted links
One could use (?<![,'"]) to exclude something at the end. But your problem is that you simply shouldn't use the trailing character class:
[^\w#$&+,\/:;=?#.-]
That's what matches " and '.
As a hackish workaround to the other problem, the first character class could be augmented with a space.
[\w#$&+,\/:;=?#. -]+
▵
As said, that's probably not a good solution and might lead to other mismatches.
I have an email address param where email addresses are passed un-encoded like so:
http://domain/script?email=test+test#gmail.com
What PHP escaping/encoding will let me safely display the email address on an input field on the page?
Everything I tried causes the encoded chars to show up instead of the user-friendly email address (i.e. test%2Btest%40test.com)
Update - here's what I've tried:
Going from ?email=test+test#gmail.com to:
urlencode($_GET['email']) = test+test%40test.com (# sign is encoded)
htmlspecialchars($_GET['email']) = test test#test.com (lost the +)
htmlspecialchars(urlencode($_GET['email']) = test+test%40test.com (# sign encoded)
Recall that I'm trying to take the unencoded url email param and safely output it into the value of an input field while keeping plus signs intact.
Maybe I should try this?
str_replace("%40", "#", htmlspecialchars(urlencode($_GET['email'])))
If you want to safely output it in the value of an input field, you need to htmlencode it first with htmlspecialchars.
Example :
<input type="email" name="email" value="<?php echo htmlspecialchars($_GET['email']); ?>"
Note : If you aren't using double quote around what you are output, you need to apply more escaping. This page explains it all.
This works:
str_replace("%40", "#", htmlspecialchars(urlencode($_GET['email'])))
You're probably looking for urldecode()? That's what converts %40, %2B, etc. back into normal characters.
use emailaddress = urldecode($_GET['email']); as Kevin suggested. it will do whatever you need.
What if you validate the email and write it as it was, if only an email address is acceptable?
I am using cakephp. And I have textarea field where users paste data, I using tinymce plugin to format text. I have warned users not to enter telephone number or email address inside the textarea. But, I dont want to take chances.
Is there a way I can extract the telephone number and email from textarea and replace it something like XXXX#gmail.com..
I appreciate any help.
Thanks.
Here's something off the top of my head for replacing the e-mail address with hidden:
$str = "My e-mail is shown#gmail.com Contact me for more details";
$str = preg_replace("/([a-zA-Z0-9\._]+)(#[a-zA-Z0-9\-\.]+)/", "hidden\\2", $str);
print($str);
The e-mail regex is not the best, but it's something that works for your example. You can get more interesting regexes (emails and phone numbers) at http://www.regexlib.com/ and use them with a simple preg_replace.
You could:
$string = "blabla#blablabla.com";
$parts = explode("#",$string);
\\$parts[0] contains the local part
\\$parts[1] contains the domain.
Keep in mind that, (even though it is not usual), the format defined by RFC 822 allows the "#" symbol to appear within quotation marks. This means: "bl#bla"#blablabla.com is technically correct.