LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 11:09 AM   #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, 11:19 AM   #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, 01:43 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
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, 01:44 PM   #4
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
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, 07: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 09:24 PM.
 
Old 05-28-2012, 11:41 PM   #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



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 05:44 PM
php mail script csv UMG:Chicken_Soüp Programming 3 02-13-2008 06:24 PM
LXer: Title: PHP/MySQL Classifieds Script AddAsset1.php Script Insertion LXer Syndicated Linux News 0 07-02-2006 06:21 PM
What is wrong with my mail script?(PHP) Alexander.s Programming 2 05-16-2005 04:18 AM
php mail script return path richard22 Linux - Software 1 08-22-2003 04:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:31 AM.

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
Open Source Consulting | Domain Registration