LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   PHP Mail Script questions! (https://www.linuxquestions.org/questions/linux-newbie-8/php-mail-script-questions-947234/)

secondhandman 05-28-2012 11:09 AM

PHP Mail Script questions!
 
I am trying to create a script that can send an email to my address whenever the ip address of the server changes.

I have tried a few things without luck. Right now my script looks like this:

<?php
$to = 'me@example.com';
$subject = 'ip address'
$output = 'ifconfig eth0 | grep "inet addr"');
$body = 'The current ip address is: '<pre>$output</pre>;
?>

Not sure what to correct. I have tried a number of things, but I keep getting syntax errors.

First time playing with php btw!

Thanks!!

secondhandman 05-28-2012 11:19 AM

Come to think of it.. it's the routers ip address that changes.. Hmmm..

arizonagroovejet 05-28-2012 01:43 PM

Well the code you post isn't going to send an email so presumably that's either not all the code, or you're calling this from a cronjob and because the cronjob has output, that gets email you by the server. (Though if that's the case then you're not getting the an email whenever the IP address changes, you're just getting it whenever the cronjob runs.) Or you're doing something else I haven't thought of.

You say you get errors, might help to say what they are. You're missing a semi-colon though and the third line has a ) but no ( and even if it had a ( it wouldn't actually do anything.

Any reason you're trying to do this in PHP rather than a shell script?

If it's the router IP that keeps changing you need to ask something what your IP address is. E.g.
Code:

$ curl -s http://whatsmyip.net/  |grep -i '<title>' | cut -d : -f 2 | sed 's!</title>!!'

arizonagroovejet 05-28-2012 01:44 PM

Quote:

Originally Posted by arizonagroovejet (Post 4689571)
If it's the router IP that keeps changing you need to ask something what your IP address is. E.g.

Or you need to figure out how to query your router. If it has a web interface you could use curl to grab the relevant page and parse it.

secondhandman 05-28-2012 07:43 PM

Thank you for your reply!

I'll try to be more descriptive. I'm using a Ubuntu Server with LAMP and installed the follow to send mail using php:

apt-get install php-pear
pear install mail
pear install Net_SMTP
pear install Auth_SASL
pear install mail_mime
apt-get install postfix

This is the script I'm using now to report the host address (no real reason for me to do it!). The following script works without a problem:

<?php
$to = "me@example.com";
$subject = "Router ip address";
$body = exec('ifconfig eth0 | grep "inet addr"');
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>


Thanks for your curl command! I have it working now. The code for reporting my public ip address is as follows:

<?php
$to = "me@example.com";
$subject = "Routers ip address";
$body = exec("curl -s http://whatsmyip.net/ |grep -i '<title>' | cut -d : -f$
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

I would like this script to only send a message when the ip address changes if possible. I don't know much about cron jobs yet and have never set it up before.


Would there be another way to do get the public ip address? Maybe using SNMP? Just an idea.
We don't have the router were we're deploying yet, and I'm not allowed to configure it.

secondhandman 05-28-2012 11:41 PM

How could I change the script to only send an email when the ip address changes? I'm using Scheduled Tasks from the Ubuntu Software Center in place of cron jobs. It's a simple GUI tool that gets the job done, so I'm not worried about cron anymore.


All times are GMT -5. The time now is 10:00 PM.