LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Sending mail with php mailer (https://www.linuxquestions.org/questions/linux-software-2/sending-mail-with-php-mailer-948435/)

call_krushna 06-04-2012 07:54 AM

Sending mail with php mailer
 
Hi Team,

I have created back up script which creates log file.I need to send mail with the content of log as body message .Below are the details

log file will generated in /mnt/with today's date as file name
log=/mnt/`date +%F`.log

#vi mail.php

<head>
<title>PHPMailer</title>
</head>
<body>
<?php

require("class.phpmailer.php");

$To = 'xyz@gmail.com';
$From = 'xyz@gmail.com';
$FromName = 'xyz';
$Subject = 'Weekly backup status';
$today = date('Y-m-d');

$Message = ;

$HTML = true;

$Mail = new PHPMailer();

$Mail->IsSMTP(); // send via SMTP
$Mail->Host = "Smtp.gmail.com";
$Mail->SMTPAuth = true; // enable SMTP authentication
$Mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent
$Mail->Hostname = "Smtp.gmail.com"; // sets the SMTP server
$Mail->Port = 465; // set the SMTP port for the GMAIL server
$Mail->Username = "xyz@gmail.com "; // SMTP account username
$Mail->SMTPSecure = "ssl"; // SMTP account username
$Mail->Password = 'xyz1234'; // SMTP account password


//$Mail->Host = $Host; //SMTP server

$Mail->SMTPAuth=true;
$Mail->From = $From;
$Mail->FromName = $FromName;
$Mail->AddAddress($To);
$Mail->AddReplyTo($From);

$Mail->WordWrap = 50; // set word wrap
$Mail->IsHTML($HTML);

$Mail->Subject = $Subject;
$Mail->Body = $Message;
echo "<pre>";
print_r($Mail);
if($Mail->Send())
{
echo "Message Sent";
}
else
{
echo "Message Not Sent<br/>";
echo "Mailer Error: " . $Mail->ErrorInfo;
}

?>
</body>
</html>

############################# File ends here ################
What should be the parameter for $message in the above file to send the content of log file i.e. /mnt/`date +%F`.log in a mail .

Any help is appreciable.

Thanks in advance

cliffordw 06-05-2012 04:19 AM

Hi there,

Try this PHP code:
Code:

$Logfile = '/mnt/' . date("Y-m-d") . '.log';
$Message = file_get_contents($Logfile);

Regards,

Clifford

call_krushna 06-06-2012 02:02 AM

Quote:

Originally Posted by cliffordw (Post 4695835)
Hi there,

Try this PHP code:
Code:

$Logfile = '/mnt/' . date("Y-m-d") . '.log';
$Message = file_get_contents($Logfile);

Regards,

Clifford

Thanks for replying, I am busy with some other high priority task, I will check the code and update you

call_krushna 06-06-2012 10:40 PM

Quote:

Originally Posted by call_krushna (Post 4696588)
Thanks for replying, I am busy with some other high priority task, I will check the code and update you

Hi cliffordw,

I had to modify the script as per request .Now my log file is located as below .


TAR=/bin/tar
DIR=`date +%m%d%Y`
DES=/vol-112124
MYDIR=$DES/$DIR
LOG=$MYDIR/`date +%F`.log

Can you tell , what should be the path of $message

I tried below way, but not working.

$Logfile = '$MYDIR' . date("Y-m-d") . '.log';

cliffordw 06-07-2012 03:22 AM

Hi there,

I assume your commands above to set the variables are in a shell script. Are you calling the PHP code from the same script after setting these?

If so, you need to add this to the script after setting LOG:
Code:

export LOG
Then you can try this in your PHP code:
Code:

$LogFile = getenv('LOG');
If not, the variables you have set are probably not available to the PHP code, so you have to set them again in your PHP code, as follows:
Code:

$DIR = date("mdY");
$DES = '/vol-112124';
$MYDIR = $DES . '/' . $DIR;
$Logfile = $MYDIR . '/' . date("Y-m-d") . '.log';

Hope this helps!

call_krushna 06-07-2012 03:48 AM

Quote:

Originally Posted by cliffordw (Post 4697633)
Hi there,

I assume your commands above to set the variables are in a shell script. Are you calling the PHP code from the same script after setting these?

If so, you need to add this to the script after setting LOG:
Code:

export LOG
Then you can try this in your PHP code:
Code:

$LogFile = getenv('LOG');
If not, the variables you have set are probably not available to the PHP code, so you have to set them again in your PHP code, as follows:
Code:

$DIR = date("mdY");
$DES = '/vol-112124';
$MYDIR = $DES . '/' . $DIR;
$Logfile = $MYDIR . '/' . date("Y-m-d") . '.log';


Hope this helps!

Thanks a ton . It worked like champ .Thanks again :)

call_krushna 06-07-2012 05:56 AM

Quote:

Originally Posted by call_krushna (Post 4697647)
Thanks a ton . It worked like champ .Thanks again :)



Hi cliffordw,

Can I send mail to multiple email addresses ?If so , can you tell me the chanages need to make in mail.php .


All times are GMT -5. The time now is 06:50 PM.