adding a line feed when building a string php - php

i have been looking all over and i cant find the answer. i am sure this is easy
i am sending an email, and in the body are names of clients
the code looks for clients that fit the search conditions and as each one is found it goes into a loop
while($row = mysql_fetch_array($result)) {
$clients = $clients . $first_name . ???
}
the result should be
Client1
Client2
Client3
but what i keep getting is:
Client1Client2Client3
I have tried
$clients = $clients . $first_name . lf;
$clients = $clients . $first_name . cr;
$clients = $clients . $first_name . '\n';
but always the same result
TIA

If your output will be the command line, file, etc. Use PHP_EOL. If it will be a web browser use <br>:
while($row = mysql_fetch_array($result)) {
$clients = $clients . $first_name . PHP_EOL;
}
while($row = mysql_fetch_array($result)) {
$clients = $clients . $first_name . "<br/>";
}

For text e-mails, the simplest case would be:
$clients = $clients . $first_name . "\n";
Note that you need to enclose \n in double (") instead of single (`) quotes, so that it gets treated as a new line character.
If you are sending HTML e-mails, then you'll need the appropriate HTML tag, i.e. <br>

You can use "\n" or PHP_EOL (single quoted strings like '\n' are not interpolated)
$clients = $clients . $first_name . PHP_EOL;
If you're outputting this to the browser, newline characters are not displayed in html, so you can do something like this:
print nl2br($clients);
Or this:
header('Content-type: text/plain');
print $clients;`
Or this:
printf('<pre>%s</pre>', $clients);

Related

imap_search() with for() will not search for sender

The loop ($zeile[$i]) will not be executed, when it is in the imap_search() function.
The syntax ($inbox, 'FROM " ' . $zeile[$i] . ' " ') is like a lot of examples I have found.
Outside of this codeblock it works well.
But inside even the line on the bottom (echo "#" .$zeile[$i]."<br>";) will not show anything.
With a single var ($test = "domain.de";) it works though.
$test = "domain.de";
$zeile = file("blacklist.txt");
for ($i=0;$i < count($zeile); $i++) {
$emails = imap_search($inbox, 'FROM " ' . $zeile[$i] . ' " ');
if ($emails) {
foreach ($emails as $email_number) {
imap_setflag_full($inbox, $uid, "\\Seen", ST_UID);
echo "#" .$zeile[$i]."<br>";
}
} // if emils
} //Dateischleife
imap_close($inbox, CL_EXPUNGE);
okay, I found the problem.
The first email address from the text file determines the number of $ mails and thus the number of loops of if ($ mails). After that, there is no turning back to the line above.

PHP Google API Client v3 get contacts

I still have trouble with the new google_api_client php library. I'm trying to retrieve the user's contacts.
I'm very close to the right solution ... I mean, I just got all the results but a can't parse it.
Probably it's because I'm not strong with XML parser. After tests and tests ... I get this solution (based on the example file by Google):
...
$req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
$val = $client->getIo()->authenticatedRequest($req);
$response = simplexml_load_string($val->getResponseBody());
foreach($response->entry as $entry)
{
$child = $entry->children("http://schemas.google.com/g/2005");
$mail_info = $child->attributes();
}
...
In the $response I can get the title field where my contact's full name is stored, and in the $mail_info a got an object where i see the address field when I get the email address.
It's SAD and UGLY solution ... what if I want the company name, address ... phone numbers ... photos. Where are all these informations.
How can I use the Google response in a great and clean solution?
Anyone can give me some help.
Bye
What helped me was requesting JSON instead of XML. Try adding ?alt=json to the end of the URL in the request you make to google.
$req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full?alt=json");
$val = $client->getIo()->authenticatedRequest($req);
$string = $val->getResponseBody();
$phparray = json_decode($string);
Certainly not child's play to get what you want but working with php arrays is probably easier.
For completeness this is the google contacts php example that we both probably found that helped us:
https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/contacts/simple.php
EDIT:
Here is another link that might help. In the comments it describes a cleaner of accessing contact's data using JSON.
http://25labs.com/import-gmail-or-google-contacts-using-google-contacts-data-api-3-0-and-oauth-2-0-in-php/
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$xmlresponse = curl_file_get_contents($url);
$temp = json_decode($xmlresponse,true);
foreach($temp['feed']['entry'] as $cnt) {
echo $cnt['title']['$t'] . " --- " . $cnt['gd$email']['0']['address'] . "</br>";
}
and
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$xmlresponse = curl_file_get_contents($url);
$temp = json_decode($xmlresponse,true);
foreach($temp['feed']['entry'] as $cnt) {
echo $cnt['title']['$t'] . " --- " . $cnt['gd$email']['0']['address'];
if(isset($cnt['gd$phoneNumber'])) echo " --- " . $cnt['gd$phoneNumber'][0]['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$street'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$street']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$neighborhood'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$neighborhood']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$pobox'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$pobox']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$postcode'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$postcode']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$city'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$city']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$region'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$region']['$t'];
if(isset($cnt['gd$structuredPostalAddress'][0]['gd$country'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$country']['$t'];
echo "</br>";
}

can't get loop in mysql_fetch_array to print on separate lines

I am trying to loop through my mysql query result and print out some of the data. What I expected was when I added "\n" to the end of the print message, it would print each message on a separate line. But for some reason its all on one line. Why is this and how can I make each message be on a separate line?
while($row = mysql_fetch_array($result))
{
$message = $row['action_type'] . " " . $row['identifier'] . " # " . " placeholder ";
if($row['location'] !== NULL)
{
$message += " on " . $row['location'] . "\n";
}
echo $message . "\n";
}
Your $message variable is ending with a /n when it should be \n. Try updating it to fix (unless of course, in that section of the code it's on purpose):
$message += " on " . $row['location'] . "\n";
The actual echo statement ends with a real newline, so this should work properly in a command-line, but not in a browser.
To get it to display on a new line in a browser, change the \n instances to <br />:
echo $message . "<br />";

Print PHP string in new lines

Anything wrong with this code? I want it to print the name and address - each on a separate line, but it all comes up in one line.
Here's the code
<?php
$myname = $_POST['myname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$town = $_POST['town'];
$county = $_POST['county'];
$content = '';
$content .="My name = " .$myname ."\r\n";
$content .="Address1 = " .$address1 ."\n";
$content .="Address2 = " .$address2 ."\n";
$content .="Address3 = " .$address3 ."\n";
$content .="town = " .$town ."\n";
$content .="county = " .$county ."\n";
echo $content;
?>
It looks like the '\n' character is not working.
In your source code this will show on a next line, but if you want to go to another line in HTML you will have to append <br />.
So:
$content .="My name = " .$myname ."<br />\r\n";
I left the \r\n here because it will go to the next line in your source code aswell, which might look nicer if you have to view the source.
The \n character properly works just fine. The problem is, it's not what you expect.
If you see this in a browser, you won't see line breaks, because line breaks are ignored in the source code. The HTML parser only reads <br> as line breaks.
If you try to go to your website and view the source code, you'll find that the line breaks are in there.

php imap get from email address

How do I retrieve the email address from an email with imap_open?
If the sender name is known I get the sender name instead of the email address if I use the 'from' parameter.
Code: http://gist.github.com/514207
$header = imap_headerinfo($imap_conn, $msgnum);
$fromaddr = $header->from[0]->mailbox . "#" . $header->from[0]->host;
I battled with this as well but the following works:
// Get email address
$header = imap_header($imap, $result); // get first mails header
echo '<p>Name: ' . $header->fromaddress . '<p>';
echo '<p>Email: ' . $header->senderaddress . '<p>';
I had used imap_fetch_overview() but the imap_header() gave me all the information I needed.
Worst case, you can parse the headers yourself with something like:
<?php
$headers=imap_fetchheader($imap, $msgid);
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $headers, $matches);
?>
$matches will contain 3 arrays:
$matches[0] are the full-lines (such as "To: user#user.com\r\n")
$matches[1] will be the header (such as "To")
$matches[2] will be the value (user#user.com)
Got this from: http://www.php.net/manual/en/function.imap-fetchheader.php#82339
Had same issue as you....had to piece it together, don't know why it's such gonzoware.
Untested example here:
$mbox = imap_open(....)
$MN=$MC->Nmsgs;
$overview=imap_fetch_overview($mbox,"1:$MN",0);
$size=sizeof($overview);
for($i=$size-1;$i>=0;$i--){
$val=$overview[$i];
$msg=$val->msgno;
$header = imap_headerinfo ( $mbox, $msg);
echo '<p>Name / Email Address: ' . $header->from[0]->personal ." ".
$header->from[0]->mailbox ."#". $header->from[0]->host. '<p></br>';
}
imap_close($mbox);
imap_fetch_overview could be what you're looking for: http://www.php.net/manual/en/function.imap-fetch-overview.php
An example of use can be found here: http://davidwalsh.name/gmail-php-imap, specifically
echo $overview[0]->from;
This function is simple, but has limitations. A more exhaustive version is in imap_headerinfo ( http://www.php.net/manual/en/function.imap-headerinfo.php ) which can return detailed arrays of all header data.
Had trouble until I spotted that the $header is an array of stdClass Objects. The following 2 lines worked:
$header=imap_fetch_overview($imap,$countClients,FT_UID);
$strAddress_Sender=$header[0]->from;
Full working code with an online example
Extract email addresses list from inbox using PHP and IMAP
inbox-using-php-and-imap
I think all you need is just to copy the script.
I am publishing two core functions of the code here as well (thanks to Eineki's comment)
function getAddressText(&$emailList, &$nameList, $addressObject) {
$emailList = '';
$nameList = '';
foreach ($addressObject as $object) {
$emailList .= ';';
if (isset($object->personal)) {
$emailList .= $object->personal;
}
$nameList .= ';';
if (isset($object->mailbox) && isset($object->host)) {
$nameList .= $object->mailbox . "#" . $object->host;
}
}
$emailList = ltrim($emailList, ';');
$nameList = ltrim($nameList, ';');
}
function processMessage($mbox, $messageNumber) {
echo $messageNumber;
// get imap_fetch header and put single lines into array
$header = imap_rfc822_parse_headers(imap_fetchheader($mbox, $messageNumber));
$fromEmailList = '';
$fromNameList = '';
if (isset($header->from)) {
getAddressText($fromEmailList, $fromNameList, $header->from);
}
$toEmailList = '';
$toNameList = '';
if (isset($header->to)) {
getAddressText($toEmailList, $toNameList, $header->to);
}
$body = imap_fetchbody($mbox, $messageNumber, 1);
$bodyEmailList = implode(';', extractEmail($body));
print_r(
',' . $fromEmailList . ',' . $fromNameList
. ',' . $toEmailList . ',' . $toNameList
. ',' . $bodyEmailList . "\n"
);
}

Categories