PHP. How to remove a %0D character from my code - php

I am trying to send me an email with a daily digest of some RSS feeds. I receive the following error, caused by an extra %0D character. Could you suggest me what I need to correct in the code to remove it?
Thanks a lot
The error is: simplexml_load_file(https://hdblog.it/feed%0D): failed to open stream: Invalid redirect URL!
// Set the timezone to Rome, Italy
date_default_timezone_set('Europe/Rome');
// Set the cutoff time to 24 hours ago
$cutoff_time = time() - (24 * 60 * 60);
// Set the subject of the email
$subject = "Digest RSS " . date("d/m/Y");
// Initialize the message
$message = "<html><body>";
$message .= "<h1>RSS Digest</h1>";
$message .= "<p>Ecco gli ultimi aggiornamenti dai tuoi feed RSS:</p>";
// Read the list of RSS feeds from a file
$rss_feeds_file = file_get_contents('rss_feeds.txt');
$rss_feeds = explode("\n", $rss_feeds_file);
// Loop through the list of RSS feeds
foreach ($rss_feeds as $rss_feed) {
// Load the RSS feed
$rss = simplexml_load_file($rss_feed);
if ($rss) {
// Initialize a flag to keep track of whether there are feed items to include in the email
$items_included = false;
// Add the feed title to the message
$message .= "<h2>" . $rss->channel->title . "</h2>";
// Add a list of items to the message
$message .= "<ul>";
// Loop through the feed items
foreach($rss->channel->item as $item) {
// Check if the item was published within the last 24 hours
$item_time = strtotime($item->pubDate);
if ($item_time > $cutoff_time) {
// If it was, add the feed item to the email
$message .= "<li><a href='" . $item->link . "'>" . $item->title . "</a><br />";
$message .= strip_tags($item->description) . "</li>";
$items_included = true;
}
}
$message .= "</ul>";
}
}
$message .= "</body></html>";
I am trying to send me an email with a daily digest of some RSS feeds. I receive the following error, caused by an extra %0D character. Could you suggest me what I need to correct in the code to remove it?
Thanks a lot
The error is: simplexml_load_file(https://hdblog.it/feed%0D): failed to open stream: Invalid redirect URL!

Are you running on Windows?
%0D is the carriage return character. You are exploding the input file on \n (line feed), but perhaps your input file has \r\n line terminators. Try exploding on '\r\n' instead.
If your input file has been created on the same platform as you are running your code you could try using the php constant PHP_EOL.

Related

how to line break in php

($message = 'EmailAddress : ' . $username . '-Accessed on : '.date("Y-m-d") . '-Time : ' . $datetime .'';)
This is my code it's made for sending messages to mailing someone using my login form. result going on like this (EmailAddress: dula#gmail.com-Accessed on 2021-02-23-Time: 10:59 AM) email data and time now I need to line break for before (Accessed on) I used <br\> \r\n but it's not working can you help me, someone, please...
($message = 'EmailAddress : ' . $username . '-Accessed on : '.date("Y-m-d") . '-Time : ' . $datetime .'';)
for this code how to used line break inside this code ?
You can use p element to automatically creates a new line.
$message = '<p>EmailAddress : '.$username.'</p><p>-Accessed on : '.date("Y-m-d") . '-Time : ' . $datetime .'</p>';
And set the headers to Content-Type to text/html.
header("Content-Type: text/html");
/* i think you suppose to use Content-Type is text/html for br tag when sending mail */

Inserting shortcode into php line

I want to insert this sortcode:
echo do_shortcode('[AUTHORIMAGE-CIRCLE]');
and replace it with the ua1 class on this php line:
$STRING .= $b_f.''.$CORE->_e(array('head','4')).''.$b_a;
This is the real code. the full one is:
if( $canShowBar ){
if(!$isTop){
$STRING .= "<div class='breadcrumb'>";
}
if(isset($userdata) && $userdata->ID){
if(isset($GLOBALS['CORE_THEME']['links'])){
// my account
$STRING .= $b_f.''.$CORE->_e(array('head','4')).''.$b_a;
}
}
}
when working on local host and try to modify the code, after refreshing the whole page is turning blank

PHP : Second Function not being called

I am using PHP and want to run 2 functions, one after the other.
These functions are getallmydata() and createCSV()
The code below runs the first function fine but the second function createCSV() is not working. It seems like it is not being called properly.
Is there anything wrong with the structure of my code as both functions work correctly independently? I cannot work this out!
<?php
//run this function//
getallmydata();
//then run this function//
createCSV();
function getallmydata(){
require_once dirname(__FILE__).'/cm/csrest_general.php';
require_once dirname(__FILE__).'/cm/csrest_clients.php';
require_once dirname(__FILE__).'/cm/csrest_campaigns.php';
$api_key = 'MY API KEY';
$wrap = new CS_REST_General($api_key);
$result = $wrap->get_clients();
$Content = "";
if ($result->was_successful()) {
foreach ($result->response as $client) {
$client_wrapper = new CS_REST_Clients($client->ClientID, $api_key);
$client_details_result = $client_wrapper->get();
$campaigns_result = $client_wrapper->get_campaigns();
if ($client_details_result->was_successful()) {
/* This is where the client details will be */
$client_details = $client_details_result->response;
echo ('<pre>');
/*print out the company name*/
echo "Company Name = " . $client_details->BasicDetails->CompanyName . "<br/>";
/*print out the company markup*/
echo "Markup On Delivery = " . $client_details->BillingDetails->MarkupOnDelivery . "<br/>";
$count = 0;
if ($campaigns_result->was_successful()) {
/*print out the latest campaign name of the current campaign*/
foreach ($campaigns_result->response as $campaign_ob) {
echo 'Latest Campaign Name = ' . $campaign_ob->Name . '<br/>';
//echo 'Latest Subject = ' . $campaign_ob->Subject . '<br/>';
//echo 'Total Recipients = ' . $campaign_ob->TotalRecipients . '<br/>';
//echo 'Sent Date = ' . $campaign_ob->SentDate . '<br/>';
/*Set content for CSV File*/
//$Content .= "This is within the loop \n";
$count++;
if($count > 0) break;
}/*end loop*/
}/*end campaigns if statement*/
echo ('</pre>');
} else {
echo 'Failed with code '.$client_details_result->http_status_code."\n<br /><pre>";
var_dump($client_details_result->response);
}
}
} else {
echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
var_dump($result->response);
echo ('</pre>');
}
} //end main function
/*create the downloadable csv file*/
function createCSV(){
$FileName = date("d-m-y") . '.csv';
# Titlte of the CSV
//$Content = "Company_Name Markup Campaign_Name Subject Recipients Date \n";
# fill data in the CSV
//$Content .= "\"John Doe\",\"New York, USA\",15,65465464 \n";
$Content .= "Testing The Function Works OK \n";
//$Content .= "This should be another line";
header('Content-Type: application/csv');
header("Content-length: " . filesize($NewFile));
header('Content-Disposition: attachment; filename="' . $FileName . '"');
echo $Content;
exit();
}//end csv download function
/*end create downloadable .csv file */
?>
I think you should get the error: headers already sent. (you checked that the second function is called right? You can find it out by placing a echo on the first line of the function.)
You are trying to create a CSV page but you are parsing HTML in the first function, so the header is already sent to the client saying that it is a normal HTML page. Remove these echo's in the first function and it should work.
Quote of the PHP manual:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
More info about headers: PHP header
First think I would tell you is
1 you should first write function definition and then you should call a function
i.e
function getallmydata(){
// function code
}
function createCSV(){
// function code
}
getallmydata();
createCSV();
2 . The second thing is that check that is there any white space left in the code out side php code or any o/p that is sent as resonse as because when ever you user header() at that if any kind of content other than header() is sent as response then header() function fails. Try this things and check again.

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