Backup2mail can't send email with database backup..multiple or malformed newlines? - database-backups

Until 18 June I received all days my backup database on Gmail without problems, I had finally found the solution for this. But after 18 June emails with database are stopped. I don't receive neither normal email with only log file txt..nothing I receive now.
I use Backup2mail http://www.backup2mail.com/
PHP Version 5.6.13, Ubuntu Linux 14.04.1
The database backup file is perfectly created on server but it can't send more to Gmail.
I have this error in index.php file of Backup2mail:
Warning: mail(): Multiple or malformed newlines found in additional_header in > /srv/users/serverpilot/apps/myappname/public/backuptomail/index.php on line 119
Database not sent! Please check your mail settings.
Sent? No
On line 119 there is:
if (mail($send_to, $subject, $body, $headers)) {
$sent = 'Yes';
echo ($file_is_db ? 'Backup file' : 'Report') . ' sent to ' . $send_to . '.<br />';
if ($file_is_db) {
if ($delete_backup) {
unlink($file);
echo 'Backup file REMOVED from disk.<br />';
} else {
echo 'Backup file LEFT on disk.<br />';
}
}
} else {
echo '<span style="color: #f00;">' . ($file_is_db ? 'Database' : 'Report') . ' not sent! Please check your mail settings.</span><br />';
}
echo 'Sent? ' . $sent;
Where is the problem? :( Maybe from 18 june PHP is upgraded and changed something? I'm not expert.
Thank you in advance

The problem is a recent update in PHP. Under this update you can no longer have multiple (\n) new lines in the header. Thus you need to remove the newlines in a few places.
Find the following lines and remove the trailing \n (leave just one):
$body = 'Database backup file:' . "\n" . ' - ' . $file . "\n\n";
$headers .= 'Content-Transfer-Encoding: base64' . "\n\n";
However you also need to remove the \n in the following line as well:
$headers .= chunk_split(base64_encode(implode('', file($file)))) . "\n";
change it to:
$headers .= chunk_split(base64_encode(implode('', file($file)))) . "";
It should work after these changes.
Hope this helps you out mate.

Related

Laravel: Calling Artisan command from controller not working properly

I'm trying to backup my db when I hit a spesific route.
first I made artisan command: php artisan backupDB
here is the function:
echo "Generating backup file..\n";
$filename = "backup-" . Carbon::now()->format('Y-m-d-H-i-s') . ".sql";
$command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " > " . storage_path() . "/app/backup/" . $filename;
$returnVar = NULL;
$output = NULL;
exec($command, $output, $returnVar);
echo "Backup file path: ".storage_path() . "/app/backup/".$filename;
I run it in cli and it is working fine.
then when I tried to run it via controller:
use Illuminate\Support\Facades\Artisan;
public function backupDB{
Artisan::call("backupDB");
}
when I hit the route it prints the echo, but nothing else happened. just that.
So, I found a solution. wrap the command inside a job or just put the code inside that job, and then start the queue. By doing that the code works fine when I try to hit the route.
php artisan make:job backupDB
it will create a file inside app>jobs folder
then put the code inside handle() function.
next just call the job inside your controller or route
use App\Jobs\backupDB;
public function backup()
{
$process = new pull();
dispatch($process);
}
and you need to setup the queue, here is the docs
You don't need to reinvent the wheel. Just use this extension.
PS: I can't explain the problem you are facing, because you didn't provide any debug info: logs, backtrace, or smth like that.
PS2: Be careful with exec() functions usage

Cannot add line break to file

I am going to make function that writes all activity logs on my webpage. I'm using Codeigniter 3x as the framework. Here is the code.
public function writeLog($loc, $user, $activity) {
$this->load->helper('file');
date_default_timezone_set('Asia/Jakarta');
$files = FCPATH . '/assets/logs/logs-' . date('Y-m-d') . ".log";
$data = date("H:i:s") . " | User: " . $user . " from IP " . $loc . " do activity " . $activity . "\n";
write_file($files, $data, "a");
}
When I'm about to call the function by writeLog("192.168.1.3", "blackjack", "login"), it should be written like this:
15:20:22 | blackjack from IP 192.168.1.3 do activity login
But when I call that function again by writeLog("192.168.1.3", "blackjack", "logout"), the result like this:
15:20:22 | blackjack from IP 192.168.1.3 do activity login15:20:39 | blackjack from IP 192.168.1.3 do activity logout
I have inserted "\n" in the end of the $data, but new line isn't created.
Please help me
Different Operating systems use different characters for line endings. On unix-like systems the line ending is \n (called LF) while windows use \r\n (called CRLF). Most windows programs can display \n but not all.
Best way to go about is to use the PHP_EOL constant and let the server decide what line endings to use.
Extra curiosa: Mac OSX uses LF now days but older mac's used only CR.

write csv file to server with codeigniter csv_from_result

Trying to save a csv file on my server with codeigniter.
my syntax is:
$this->load->dbutil();
$query = $this->db->query("SELECT id,customeraccount,band FROM Customer_Bands");
$delimiter = ',';
$newline = "<br>";
echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
if ( ! write_file(base_url().'/application/authorizations/test.csv', $this->dbutil->csv_from_result($query, $delimiter, $newline)))
{
echo 'Unable to write the file';
}
else
{
echo 'File written!';
}
This is rough right now but want to get functionality working first.
Two issues.
Firstly the echo $this->dbutil->csv_from_result($query, $delimiter, $newline); outputs to my view correctly but each field is inside "", how can I remove these?
Secondly, the file is not writing to my server.
base_url() is my codeigniter root folder then inside applications I have created a folder named authorizations so base_url()/application/authorizations
This currently fails and echos 'Unable to write the file'
Thanks in advance

fopen with dynamic filename is not working

I need to make a web site on which people can upload data files that after some treatment will be plotted using the jpgraph. The file is analyzed with a bash script called analfile.sh. The bash script is like this:
#!/bin/bash
file=$1
fecha=`date +%s`
mv $1 $fecha.dat
echo $fecha.dat
So, it gives back another file which name is like: 1321290921.dat. That is the file that I need to plot.
This my current php code:
$target_path = "/home/myhome";
$target_path = $target_path . basename( $_FILES['rdata']['name']);
$target_file = basename( $_FILES['rdata']['name']);
if(move_uploaded_file($_FILES['rdata']['tmp_name'], $target_path)) {
echo "The file ". $target_file. " has been uploaded";
chdir('/home/myhome');
$filetoplot=shell_exec('./analfile.sh'.' '.$target_file);
} else{
echo "There was an error uploading the file, please <a href=\"index.html\">try again!
</a>";
}
//$filetoplot="1321290921.dat"
echo "<br>The file to be opened is ". $filetoplot. "<br>";
if ($file_handle = fopen("$filetoplot", 'r')) {
while ( $line_of_text = fgets($file_handle) ) {
$parts = explode('.', $line_of_text);
echo $line_of_text ;
print $parts[0] . $parts[1]. $parts[2]. "<br>";
}
fclose($file_handle);
}
I have permissions to read and write on the target directory. I find strange that if I uncomment the line $filetoplot="1321290921.dat" then the script works perfectly. I guess I am doing something stupid since this is my first code in php but after some hours googling I was not able to find a solution.
Any help will be appreciated.
First thing I'm seeing is that you don't append trailing slash (/) to your home path, so that the path would be like /home/myhomefoo rather than /home/myhome/foo.
You should also move $target_file earlier, and reuse that within $target_path. There's no reason to do the same thing twice.
If that doesn't help, we'll see what goes next.

MediaWiki cannot get path

My MediaWiki cannot get the path of my live web server. Here is the code:
$script = $_SERVER['SCRIPT_NAME'];
$path = pathinfo( $script, PATHINFO_DIRNAME ) . '/';
$path = str_replace( '//', '/', $path );
$ext = pathinfo( $script, PATHINFO_EXTENSION );
echo "Please enter";
The localhostwebserver displays the echo as: Please enter,
but the live server displays the echo as Please enter
How is that possible? Need help!
If you need the url of the current page in php, I can recommend this script. I have used it successfully for years.

Resources