I'm having a hard time getting the following code to run:
$name = 'Kevin';
$company = 'nonw';
$website = 'none';
$email = 'my#email.com';
$phone = 'none';
$message = 'Just a test message.';
$message = "<ul>
<li><strong>From:</strong> $name</li>
<li><strong>Company:</strong> $company</li>
<li><strong>Website:</strong> $website</li>
<li><strong>Email:</strong> $email</li>
<li><strong>Phone:</strong> $phone</li>
</ul>
<p>$message</p>";
$subject = '[' . $sitename . '] New contact message from ' . $name . ' ( ' . $email . ' )';
$headers = 'From: ' . $sitename . ' <' . $to . '>\r\nReply-To: ' . $email . '\r\n';
$s = mail($subject, $message, $email, $to);
if($s){
echo '<div class="msg">Your message has been sent. Thank you for contacting us, someone will be in touch soon.</div>';
}else{
echo '<div class="err">There was an error sending your message. Please try again later.</div>';
print_r(error_get_last());
}
I have this in my php.ini:
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me#example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "/usr/sbin/sendmail -t -i -f my#email.com"
Have also tried with:
sendmail_path = "/usr/sbin/sendmail -t -i"
I have verified that the server is setup correctly to send email by running this directly on the server echo "test" | mail -s "test mail" my#email.com and I received the email.
What can be going on, or more to the point... what am I missing?
sorry guys. I had it spelt wrong in mailconfig.ini
changing "/urs/sbin/sendmail -t -i -f my#email.com" to "/usr/sbin/sendmail -t -i -f my#email.com" did the trick
Related
I tried to create a new log file if there is a phpmailer error. But the file is not created. I check my directory (777).
Do you have an idea ?
Thank you
if (DEBUG_EMAIL == 'true'){
$this->phpMail->SMTPDebug = $this->debug_level; //2
$this->phpMail->Debugoutput = function($str, $level) {
$filename = CLICSHOPPING::BASE_DIR . 'Work/Log/phpmail_error-' . date('Ymd') . '.log';
$data = date('Y-m-d H:i:s') . "\t" . "\t$level\t$str\n";
$flags = FILE_APPEND | LOCK_EX;
file_put_contents($filename, $data, $flags);
};
// $this->phpMail->SMTPDebug = SMTP::DEBUG_SERVER;
}
Double check all your values, don't assume they are what you think they are. For example what is in CLICSHOPPING::BASE_DIR? Also, it's usual for directory env vars to not have a trailing slash, so you may be missing one when you append Work/... to it. Try this:
$filename = CLICSHOPPING::BASE_DIR . '/Work/Log/phpmail_error-' . date('Ymd') . '.log';
$data = date('Y-m-d H:i:s') . "\t" . "\t$level\t$str\n";
my($stdout, $stderr, $exit)
so i am trying to send command-lines to a remote server using a code but I don't know if i am doing it right, here is the code
#!/usr/bin/perl
use warnings;
use Net::SSH::Perl;
use strict;
#my($stdout, $stderr, $exit) = $ssh->cmd("ls -l /home/$usr")
my $host = "$host";
my $usr = "$usr";
my $pwd = "$pwd";
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($usr,$pwd);
$ssh->cmd("script /home/$usr/textput.txt/");
$ssh->cmd("ls -l /$usr/jrivas/");
$ssh->cmd("exit");
When i run the code it seems to work fine until i go check if it made the script in the server but it wasn't there so i am imagining is the way i am sending the commands.
UPDATE
I've managed to connect to my server and make the .txt file but i havent been able to automatically save it to my out pc instead of the server
here is my code so far
#!/usr/bin/perl
use Net::SSH2;
use Net::SSH::Expect;
use Net::SSH::Perl;
use Net::SSH;
#my($stdout, $stderr, $exit) = $ssh->cmd("ls -l /home/$usr")
my $host = "host";
my $usr = "root";
my $pwd = "pwd";
#my $ssh = Net::SSH::Perl->new($host);
#$ssh->login($usr,$pwd);
my $ssh = Net::SSH::Expect->new (
host => "$host",
password=> "$pwd",
user => "$usr",
raw_pty => 1
);
my $login_output = $ssh->login();
if ($login_output !~ /Welcome/) {
die "Login has failed. Login output was $login_output";
}
my $script = $ssh->exec("script /home/jrivas/script_output.txt");
my $script = $ssh->exec("ls -l /home/jrivas");
my $script = $ssh->exec("ls -a /home/jrivas");
my($out, $err, $exit) = $ssh->cmd("cat script_output.txt");
my $script = $ssh->exec("exit");
#$ssh->cmd("script /home/jrivas/textputq.txt/");
#$ssh->cmd("ls -l /home/jrivas");
#$ssh->cmd("exit");
So next thing is to get the script_output.txt to save in my computer automatically
Thanks to #zdim for helping me :)
How can I create a daily task to run a command which outputs the results into a file that gets emailed?
I would like to run the following command:
find ./ -type f -size 510c -name "*.php" -mtime -3
in the following location:
/var/www/vhosts/
I would like to add this a cron job so I get the contents emailed ONLY if the file is not empty.
What's the best way to accomplish this?
It may be possible write a one-liner to do this, but if not, you can write a script. Below is a perl script that should do the job:
use warnings;
use strict;
my($cmd, $r);
my($to, $from, $subject, $message);
$cmd="find /var/www/vhosts/ -type f -size 510c -name \"*.php\" -mtime -3";
$r=`$cmd`;
if(length($r)>0) {
$to = 'to#to.com';
$from = 'from#from.com';
$subject = 'This is the subject';
$message = $r;
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;
close(MAIL);
}
Just save this to a file (e.g. script.pl), then just add a line to your crontab like so:
x x x x x perl /path/to/script.pl
I have a linux server running, which I back up every day.
The backup script works like a charm, but I would like some feedback on completion with or without errors.
#!/bin/bash
# Hier gaan we zoeken naar alle mappen en onderliggende mappen in de LinuxKJ server.
find /var/www -maxdepth 1 -mindepth 1 -print0 | while read -d $'\0' foldername
do
# Hier gaan we alle mappen in zipjes comprimeren en overzetten naar een andere locatie. Overigens laat hij _Inactief eruit.
fatsoenlijkPad=$(echo $foldername)
tar --exclude='/var/www/_Inactief' -zcvpf /mediabak/winboxbak/"${fatsoenlijkPad##*/}".tar.gz "$fatsoenlijkPad"
# Hier gaan we kijken of de functie hierboven een succes return (Succes = 0) (Fout = 1,2,3,4, etc)
if ( $? == 0 ) then
Mailtext=$(echo "Backup succesvol.")
else
Mailtext=$(echo "Backup failed.")
fi
done
# Hier gaan we mailen wat de functie heeft gereturned
mail -s "Linux backup" "example#example.com"
$Mailtext
./backupscript.sh: line 9: 141: command not found
Can anyone help me to fix this issue?
In bash square brackets are used. Hence change
if ( $? == 0 ) then
to
if [ $? == 0 ]; then
Edit: Change
mail -s "Linux backup" "example#example.com"
$Mailtext
to
echo $Mailtext | mail -s "Linux backup" example#example.com
To verify that your are able to send and receive a mail, try to send a mail with dummy text as below.
echo "Testing Mail" | mail -s "Linux backup" example#example.com
As commented, your variable assignement for Mailtext are inefficient (it works, but it has no sense to use an echo command to assign a text value).
As for your email sending, your mail command invocation should be :
echo $Mailtext | mail -s "Linux backup" "example#example.com"
This script runs when logged in but I cannot get it to work under a cron job, any ideas?
This script is used to monitor a wep page for changes if it detects any graphic/visual differences it has to send an email.
I've read other posts about such problems, and tried to implement some of the suggestions, but I still get errors and even if there are changes I don't get the email. (FYI: The email has been changed for confidentiality purposes)
#!/usr/local/cpanel/3rdparty/bin/perl
`SHELL=/bin/bash`;
`PATH=/usr/local/jdk/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin`;
`cd /root/pagechange`;
`rm -f null`;
`wkhtmltoimage --no-images --height 3000 --javascript-delay 7500 http://www.google.com /root/pagechange/sys.jpg`;
`$dif=/usr/bin/compare -metric AE /root/pagechange/sys.jpg /root/pagechange/sys1.jpg null: 2>&1`;
print "1";
if ( $dif == 0 ) {
print "They're equal\n";
} else {
$to = 'me#domain.com';
$from = 'you#domain.com';
$subject = 'Page changes detected ';
$message = "Get to work";
print "2";
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;
print "3";
close(MAIL);
print "Email Sent Successfully\n";
}
print "4";
`cp /root/pagechange/sys.jpg /root/pagechange/sys1.jpg`;
print "5";
#`rm /root/pagechange/index.html`;
exit
Here's what that might look like in Perl. I only modified the most obvious problems:
#!/usr/bin/perl
use strict;
use warnings;
# Set the environment with the %ENV hash
# environment variables set in a subshell will not persist
$ENV{SHELL} = '/bin/bash'; # although you don't need this
$ENV{PATH} = ...;
# change directory
chdir '/root/pagechange' or die "Could not change directory: $!";
# remove a file with unlink
unlink 'null';
# list argument form of system
# this prevents arguments from being treated as special by the shell
# use full path to executable so you know which one you use
system '/path/to/wkhtmltoimage', qw(
--no-images --height 3000 --javascript-delay 7500
http://www.google.com /root/pagechange/sys.jpg
);
# save the result of the backticks to get the program output
my $dif = `/usr/bin/compare -metric AE /root/pagechange/sys.jpg /root/pagechange/sys1.jpg null: 2>&1`;
print "1";
if ( $dif == 0 ) {
print "They're equal\n";
}
else {
my $to = 'me#domain.com';
my $from = 'you#domain.com';
my $subject = 'Page changes detected';
my $message = "Get to work";
print "2";
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;
print "3";
if( close(MAIL) ){
print "Email Sent Successfully\n";
}
else { # close puts the error in $? instead of $! (until 5.22!)
my $error = $? >> 8;
print "Problem sending mail: $error";
}
}
print "4";
# list argument form of system, again
system '/bin/cp', qw(/root/pagechange/sys.jpg /root/pagechange/sys1.jpg);
print "5";
# unlink '/root/pagechange/index.html';
Besides anything else, it has to be:
$dif=`/usr/bin/compare -metric AE /root/pagechange/sys.jpg /root/pagechange/sys1.jpg null: 2>&1`;
Your not assigning anything to $dif as variable within your script in your code. And whatever you do in the shell your way, it stays in the shell.
edit: That alone surely will not solve your problem, check the other comments