# Trouble with mail in script



## frosty85 (Jul 18, 2009)

Hi guys,
I'm writing a script that will fetch the external ip of my router and email me when it changes...

I know a few people have done this before, I've seen a few different ones, mainly use html pages from the router, but I need to log in to mine to get the the page, so I'm using www.findmyip.com...

Well my problem is when the script emails the new ip, it comes as a blank email and the message is as an attachment...

I've tried the below from the command line, which worked but not when run from within the script


```
echo "New ip is" | mail -s "New IP" [EMAIL="[email protected]"][email protected][/EMAIL]
```
here's the code as is now

```
#!/bin/bash
# check external ip address
# check if different from one stored
# email if new
#
 
# email settings
EMAIL="[email protected]~~~~.~~~~"
SUBJECT="Notification of new IP address"
 
# get last ip
LAST_IP=$(cat /var/wan_ip)
 
# get page
# www.findmyip.com
cd /tmp
wget -q [URL="http://www.findmyip.com"]www.findmyip.com[/URL] 
 
# get ip line
tail -30 index.html | head -1 > /tmp/ip
CURRENT_IP=$(cat /tmp/ip)
 
# tidy up
rm index.html
 
if [ "$CURRENT_IP" != "$LAST_IP" ]
then
 
  echo $(date) " IP address is now" "$CURRENT_IP" >> /root/wan_ip.log
  rm -f /var/wan_ip
  echo "$CURRENT_IP" > /var/wan_ip
 
  MESSAGE="Your new IP address is: $CURRENT_IP"
  echo $MESSAGE > /tmp/wan_ip_message
 
# echo "$MESSAGE" | mail -s "$SUBJECT" $EMAIL
  mail -q /tmp/wan_ip_message -s "$SUBJECT" $EMAIL 
  rm /tmp/wan_ip_message
fi
 
# tidy again
rm /tmp/ip
 
exit
```
as you can see i've tried few different ways


----------



## frosty85 (Jul 18, 2009)

if it helps im using mailx


----------



## lotuseclat79 (Sep 12, 2003)

Hi frosty85,

Have you tried the following so you can trace the script?
#!/bin/bash -x

-- Tom


----------



## frosty85 (Jul 18, 2009)

tried that,
when it comes to mail line, writes message then waits for [Ctrl-D] after pressing it, sent email... still with message as an attachment rather than in the body


----------



## lotuseclat79 (Sep 12, 2003)

Hi frosty,

What you need to be doing is issuing the mail command with the message as an input file to the mail or mailx command:

I used to send email in-line as a file from the command line with:
$ mail <input "title" email-address (or whatever the parameters allowed by the mail command are from the man page)

Try modifying your mail command to something like that to see if it sends it in-line.

-- Tom


----------



## frosty85 (Jul 18, 2009)

i noticed i had left '-q' option on mail which starts message with a file, header like a signiture..


```
mail -q /tmp/wan_ip_message -s "$SUBJECT" $EMAIL
```
changed it to 

```
mail -s "$SUBJECT" $EMAIL < /tmp/wan_ip_message
```
no joy, then

```
mail -s "$SUBJECT" $EMAIL "$MESSAGE"
```
which gave:
contains invaild character '\015'

need to [Ctrl-C] out... so basically the part of the message "is: " was throwing out the whole mail bit...

that was wrong, tried without the ':', same thing so [Ctrl-D] to send the message, this time tried emailling to 'every word'@server in the to part of the message...

thanks for your help on this


----------



## lotuseclat79 (Sep 12, 2003)

Hi frosty85,

Here is a sample script with inline data for the ed command:
#deblank - - remove leading blanks
foreach i ($argv)
ed - $i << 'EOF'
1,$s/^[spacetab]*//
w
q
'EOF'
end

Note the use of << 'EOF'
inline data
'EOF'

Perhaps your script needs a similar treatment when using the mail or mailx command to signify the end-of-input (which seems like why you need to do a Ctrl-D)?

-- Tom


----------



## Squashman (Apr 4, 2003)

I guess I don't understand why you just don't simplify this situation and use Dynamic DNS?


----------



## Squashman (Apr 4, 2003)

There is a couple of good scripts on this page and they also suggest using Whatismyip's automation page instead.
http://ubuntuforums.org/archive/index.php/t-526176.html

```
#!/bin/bash

IPFILE=/etc/ipaddress

CURRENT_IP=$(wget -q -O - http://www.whatismyip.com/automation/n09230945.asp)

MAIL_FROM="From: [email protected]"

if [ -f $IPFILE ]; then
KNOWN_IP=$(cat $IPFILE)
else
KNOWN_IP=
fi

if [ "$CURRENT_IP" != "$KNOWN_IP" ]; then
echo $CURRENT_IP > $IPFILE

MAIL_SUBJECT="IP address for domain.org changed"
MAIL_BODY="The IP address for domain.org has been changed to $CURRENT_IP. Please change the nameserver records accordingly."

echo $MAIL_BODY | mail -a "$MAIL_FROM" -s "$MAIL_SUBJECT" -c [email protected] [email protected]
```
With this link you just get the IP address.
http://whatismyip.com/automation/n09230945.asp


----------



## frosty85 (Jul 18, 2009)

Well in-line mail, nearly worked, sent mail correctly but my var $CURRENT_IP wrote as that rather than my ip address...

no DNS as this is a home internet connection, and knowing the ip is all i need at the end of the day, not web hosting or anything...
thanks for the address for ip, might come in handy..
but as the script goes, i tried to send the message that way, echo | mail etc but didn't work as expected


----------



## Squashman (Apr 4, 2003)

frosty85 said:


> no DNS as this is a home internet connection, and knowing the ip is all i need at the end of the day, not web hosting or anything...


I think your logic is flawed but you do whatever you want. I ran a personal linux server at home for many years and used NO-IP.com for dynamic dns. It was a no brainer. This falls under the K.I.S.S. rule!



frosty85 said:


> Well in-line mail, nearly worked, sent mail correctly


If the email sent then your IP address should be in the headers. Read the Headers.


----------



## frosty85 (Jul 18, 2009)

the idea was to be able to check for this email via my window mobile phone, outlook on there doesn't show headers nor does the web based email, so was trying for simple email..

but looking at it, i might investigate no-ip


----------

