LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-28-2012, 12:09 PM   #1
secondhandman
Member
 
Registered: May 2012
Posts: 60

Rep: Reputation: Disabled
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!!
 
Old 05-28-2012, 12:19 PM   #2
secondhandman
Member
 
Registered: May 2012
Posts: 60

Original Poster
Rep: Reputation: Disabled
Come to think of it.. it's the routers ip address that changes.. Hmmm..
 
Old 05-28-2012, 02:43 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
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>!!'
 
1 members found this post helpful.
Old 05-28-2012, 02:44 PM   #4
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Quote:
Originally Posted by arizonagroovejet View Post
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.
 
1 members found this post helpful.
Old 05-28-2012, 08:43 PM   #5
secondhandman
Member
 
Registered: May 2012
Posts: 60

Original Poster
Rep: Reputation: Disabled
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.

Last edited by secondhandman; 05-28-2012 at 10:24 PM.
 
Old 05-29-2012, 12:41 AM   #6
secondhandman
Member
 
Registered: May 2012
Posts: 60

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP - Mail.php and Mail/mime.php issues LVsFINEST Linux - Server 1 02-08-2009 06:44 PM
php mail script csv UMG:Chicken_Sop Programming 3 02-13-2008 07:24 PM
LXer: Title: PHP/MySQL Classifieds Script AddAsset1.php Script Insertion LXer Syndicated Linux News 0 07-02-2006 07:21 PM
What is wrong with my mail script?(PHP) Alexander.s Programming 2 05-16-2005 05:18 AM
php mail script return path richard22 Linux - Software 1 08-22-2003 05:10 AM


All times are GMT -5. The time now is 12:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration