PDA

Bekijk de volledige versie : [Howto] Esmtp Email, PHP, Reminder, send_logfile



newbiefan
04-06-2011, 10:57
How to send emails and use php-mail and now perl-mail from your asus:
precondition: installation according wengi's howto (http://www.wl500g.info/showthread.php?t=10307). Further an extra email account with any provider. The provider must have a regular mailserver. Please ensure, that all account datas are available.
If you want to use emails with php and/or perl, you need a wengi installation and install your webserver, mysql and php according wpte's howto (http://www.wl500g.info/showthread.php?t=20306).
WinSCP, Putty, mc or nano or vi is called editor
Preamble: in this howto we use esmtp. I never had success with sendmail. But esmtp has a aquivalent sendmail syntax, so we can use it for everything.
It is very easy to install and easy to use, even for multiple accounts.
For more informations just google....

INSTALLATION:


ipkg update
ipkg install esmtp
touch /opt/etc/esmtprc
vi /opt/etc/esmtprc #switch to insert mode

identity accountname@testdomain.com
hostname smtpserver.testdomain.com:25
username "accountname@testdomain.com"
password "accountpassword"
#save the file

cd /tmp/local/root
touch email
vi email #switch to insert mode
date: 29.05.2011
to: anyemailname@domain.com
subject: Test-email
from: accountname@testdomain.com

This is a testmail from the asus router
#save the file

esmtp -t < email


The identity is your new account which you must have.
The hostname must be a valid smtp server of your/any provider.
The username is your accountname (usually your email-name)
Password is your password of you emailaccount.
The used emailaddress (anyemail@domain.com) in file email must be a valid address.

Check that you have received your email
Consider, this way you can send for instance your logfile daily with the help of cron.
Or you can install a simple to use reminder system.
Further, you can use your esmtp-mail as default mailsystem with PHP.

Just to show you, how you can use your mailsystem you will find some script-code for your shell below as well as how to configure PHP and php tests.
Further, you will find now a short introduction of using perl-mail (http://www.wl500g.info/showpost.php?p=233892&postcount=8) with a perl test page.

Have fun

newbiefan
04-06-2011, 11:26
Let us send as example daily your logfile:
Just set the vars (at the top) of script to your account and a recipient.
and store the file to /opt/bin/sendlog - do not forget to set the rights to 755


#! /bin/sh
#Mail your logfile to any recipient with esmtp
#written by newbiefan 2011 for user of asus routers
#contact: hirau@gmx.at
#Please read the entire howto in order to understand everything
#Further. add a line to your crontab

tempfile=/opt/var/log/tmp_email.log #Location of your last email, can be at any writeable place
today_long=$(date +"%d.%m.%Y") #set the format for date
recipient="anyemailname@domain.com"
subject="Your daily logfile from your asus"
accountname="accountname@testdomain.com" #Email address which esmtp is using
loglocation=/opt/var/log/syslog.log #This is the full path of your logfile, consider that you can use any textfile
#can be for instance your logfile of your webserver a.s.o.


###Prepare your email
echo "date: $today_long" > $tempfile
echo "to: $recipient" >> $tempfile
echo "subject: $subject" >> $tempfile
echo "from: $accountname" >> $tempfile
echo >> $tempfile
echo "AUTOMAILER: THIS IS YOUR LOGFILE" >> $tempfile
cat $loglocation >> $tempfile
echo >> $tempfile
echo "Email is automatically generated, please do not answer!" >> $tempfile
echo "With kind regards, your router" >> $tempfile
/opt/bin/esmtp -t < $tempfile # send email


Then add a line to your crontab: (If you are not root, use admin instead)



59 23 * * * root /opt/bin/sendlog


Thats it!
To control the function, enter


sendlog


you get an email from now on of your logfile daily at 23:59
have fun

newbiefan
04-06-2011, 11:45
A simple reminder system by mail:
Think about how often you forgot mother's/father's/sister's birthday or your wedding day.
Just set the var accountname to your email account of your asus
Save the script to /opt/bin/reminder and set the rights:

#! /bin/sh
#Mail a reminder to any recipient with esmtp
#written by newbiefan 2011 for user of asus routers
#contact: hirau@gmx.at
#Please read the entire howto in order to understand everything

#use name of your file, usually reminder.txt - you can store the file anywhere
bfname=/opt/etc/reminder.txt
accountname="youremail@yourprovider.com"

#use your loglocation, usually logs are stored in /opt/var/log
loglocation=/opt/var/log/reminder.log
#if you do not need any log-file, uncomment
#loglocation=/dev/null

#we count all lines
countlines=`/opt/bin/wc -l $bfname | /opt/bin/cut -d " " -f1`

today_long=$(/opt/bin/date +"%d.%m.%Y")
today=$(/opt/bin/date +"%d.%m")

#lets start with line 1
i=0

while [ $i -ne $countlines ] ; do #start the loop over each line
let i=$i+1
#grab data from file
born=`/usr/bin/head -n$i $bfname|/usr/bin/tail -n1|/opt/bin/cut --delimiter=: -f1`
reminddate=`/usr/bin/head -n$i $bfname|/usr/bin/tail -n1|/opt/bin/cut --delimiter=: -f2`
recipient=`/usr/bin/head -n$i $bfname|/usr/bin/tail -n1|/opt/bin/cut --delimiter=: -f3`
subject=`/usr/bin/head -n$i $bfname|/usr/bin/tail -n1|/opt/bin/cut --delimiter=: -f4`
text_to_send=`/usr/bin/head -n$i $bfname|/usr/bin/tail -n1|/opt/bin/cut --delimiter=: -f5`

if [ "$today" = "$reminddate" ] ; then #yes, create the email
echo "date: $today_long" > /tmp/remindmail
echo "to: $recipient" >> /tmp/remindmail
echo "subject: $subject" >> /tmp/remindmail
echo "from: $accountname" >> /tmp/remindmail
echo >> /tmp/remindmail
echo "AUTOMAILER: REMINDER FOR DATE $born" >> /tmp/remindmail
echo $text_to_send >> /tmp/remindmail
echo "Email is automatically generated, please do not answer!" >> /tmp/remindmail
/opt/bin/esmtp -t < /tmp/remindmail # send email
onlytime=`date +"%T"`
echo "Reminder sent $today_long at $onlytime to $recipient with subject: $subject" >> $loglocation
fi

done #end of lines in file




set the rights with chmod 755 /opt/bin/reminder
Further you need a textfile with the data.
Make a file reminder.txt and add your entries. The syntax of a line is following:
date_to_remind:when_to_remind:recipients_email:you rsubject:Text_to send

Consider always to make no spaces and to use as delimiter a : (colon)

The format of date_to_remind must be: dd.mm.yyyy
The format of when_to_remind must be: dd.mm
Recipients email must be a valid email address
Your subject can be any short text
Text_to_send can be any text (should not be too long)
Just add your lines (as much as you want) to you file reminder.txt
Store your reminder.txt to /opt/etc/reminder.txt


touch /opt/etc/reminder.txt
vi /opt/etc/reminder.txt #goto insert mode

31.03.1960:28.03:hirau@gmx.at:Birthday reminder:Please remember Helmut's birthday
28.06.1936:05.06:hirau@gmx.at:Birthday reminder:Attention, Mama's birthday is coming soon!
28.06.1936:24.06:hirau@gmx.at:Second birthday reminder:Attention, Mama's birthday in 4 days!
28.06.1936:24.06:sistersaccount@gmx.at:Birthday reminder:Attention, Mama's birthday in 4 days!
#save the file


Now only one thing remains, add to your crontab: (If you are not root, use admin instead)


0 1 * * * root /opt/bin/reminder

No worry any more to forget anything important!

If any problem occurs when using cron, just use the full path of each command (e.g.: /usr/bin/head a.s.o.)
Thats it - have fun.

EDIT:In order to save your valuable time, I've added the full path to some command, hence it will work propper with cron

newbiefan
04-06-2011, 11:54
Howto configure php to use esmtp:
This is simple and fast, just open with any editor (winscp) your /opt/etc/php.ini file
and change line 588:


; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = /opt/bin/esmtp -t -i


In order to test your php-mail, store the file below to (for instance) /opt/www/cgi-bin/mailnow.php
Please change your Asus-Email account and the recipient in the file below.



<HTML>
<HEAD>
<title> Mailnow </title>
</HEAD>
<BODY>

<?php

$from_header = "From: your@mailaccount.com";
$to = "recipient@domain.com";
$subject = "Your first Testmail";
$message = "This is your first Email from your Webserver using PHP!";

mail( $to, $subject, $message, $from_header )

?>

<p align="center">

<h1>Test-email</h1><br>
is sent to your account. Everything seems to work proper</br>

<p>


</BODY></HTML>


Now call your page with http://ip.of.your.asus/cgi-bin/mailnow.php
When you can see a page with Test-email, everything is ok.
And I'm really sorry - I'm not good in PHP-coding - but it works.

Have fun

newbiefan
04-06-2011, 12:01
Or you can use a own 404 error page withh mailfunction for your webserver:
Save the file into the root-directory of your webserver (/opt/share/www/error-page404.php
I found the PHP code elsewhere in the internet and can not find it again, so credit to unknown.....
Change the accountname and recipient.



<HTML>
<HEAD>
<title> 404 Error Page</title>
</HEAD>
<BODY>
<p align="center">

<h1>Error 404</h1><br>Page Not Found

<p>
<?php

$ip = getenv ("REMOTE_ADDR");

$requri = getenv ("REQUEST_URI");
$servname = getenv ("SERVER_NAME");
$combine = $ip . " tried to load a missing" . " page on webserver" . $requri ;


$httpref = getenv ("HTTP_REFERER");
$httpagent = getenv ("HTTP_USER_AGENT");

$today = date("D M j Y g:i:s a T");

$note = "We apologize for any inconvenience, Email is sent to administrator" ;

$message = "$today \n
<br>
$combine <br> \n
User Agent = $httpagent \n
<h2> $note </h2>\n
<br> $httpref ";

$message2 = "$today \n
$combine \n
User Agent = $httpagent \n
$note \n
$httpref ";

echo $message;

$from_header = "From: accountname@testdomain.com\r\n";
$to = "recipient@domain.com";
$subject = "404 Error";
$today = date("D M j Y g:i:s a T");
$ip = getenv ("REMOTE_ADDR");
$requri = getenv ("REQUEST_URI");
$servname = getenv ("SERVER_NAME");
$pageload = $ip . " tried to load http://" . $servname . $requri ;
$httpagent = getenv ("HTTP_USER_AGENT");
$httpref = getenv ("HTTP_REFERER");
$message = "$today \n\n$pageload \n\nUser Agent = $httpagent \n\n$httpref ";
mail( $to, $subject, $message, $from_header );

?>

</BODY></HTML>


Then you have to change your lighttpd.conf in /opt/etc/lighttpd.conf and change the line


## error-handler for status 404
#server.error-handler-404 = "/error-handler.html"
server.error-handler-404 = "/error-page404.php" #use the name of your error page


after restart of lighttpd you get an email when a missing page is called
Test it with your browser:


http://ip.of.your.asus/notexistingpage

You should see now your 404-error-page and get an email.

have fun

wpte
04-06-2011, 19:57
Looks good newbiefan:)
The initial setup of esmpt I did have the error "Error parsing headers" tho I'm not sure why.
The PHP setup works great tho:D

Good job on the php scripts for a non php-coder :)

newbiefan
04-06-2011, 21:07
Looks good newbiefan:)
The initial setup of esmpt I did have the error "Error parsing headers" tho I'm not sure why.
The PHP setup works great tho:D

Good job on the php scripts for a non php-coder :)

Well, I think "Error parsing headers" was my mistake - just enter an emty line between header and body (text message).
Thanks for trying it and pointing me in the right direction with the error.
I changed already the file email of the initial setup.


newbiefan

newbiefan
24-06-2011, 21:50
Well, provided you have installed perl and some other perl-modules you can use perl-mail and mysql.
First, ensure you have installed this from optware repo:

ipkg list_installed |grep perl
should give you the following output:

perl - 5.8.8-23 - Practical Extraction and Report Language.
perl-carp-clan - 5.3-1 - This module reports errors from the perspective of the caller of a clan of modules, similar to Carp.pm itself.
perl-date-calc - 5.4-2 - Gregorian calendar date calculations.
perl-date-manip - 5.48-1 - date manipulation routines
perl-dbd-mysql - 4.006-1 - DBD-mysql - The Perl Database Driver for MySQL.
perl-dbi - 1.607-1 - DBI - The Perl Database Interface by Tim Bunce.
Then you are fine, now lets adjust your lighttpd.conf
Activate the mod cgi:

"mod_cgi",
and after section index-file.names insert this:

#tell lighty howto handle cgi
$HTTP["url"] =~ "/cgi-bin/" {
cgi.assign = ( ".pl" => "/opt/bin/perl",
".cgi" => "/opt/bin/bash",
".sh" => "/bin/sh" )
}
In case you do not need to assign bash and shell, just set extension .cgi to perl. Then your cgi.assign should look like this:

#tell lighty howto handle cgi
$HTTP["url"] =~ "/cgi-bin/" {
cgi.assign = ( ".pl" => "/opt/bin/perl",
".cgi" => "/opt/bin/perl" )
}
With the first cgi.assign example you can install this webinterface for your optware (http://www.wl500g.info/attachment.php?attachmentid=8176&d=1308949143) installations. Please consider anytime the security risks. If you do not know what you are doing including related consequences, please install just the second example of cgi.assign.

Well now the perl-testpage, store it to folder cgi-bin/mailnow.pl of your webservers document root.
Just set the vars to and from to your needs.

#!/opt/bin/perl
print "Content-type: text/html\n\n";

$title='Perl Mail Test for Asusrouters';
$to='recipient@domain.net';
$from= 'yourasusaccount@yourdomain.com';
$subject='Perl Mail Test';

open(MAIL, "|/opt/bin/esmtp -t");

## Create Header, Message and send everything, consider after subject \n\n - it's an emty line
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Message
print MAIL "This is your first Perl Message from your Asus or any other machine\n";
print MAIL "When you are able to read this message, your Perl including cgi-bin works!\n";
print MAIL "This example is taken from: http://www.cyberciti.biz/faq/sending-mail-with-perl-mail-script/\n";
print MAIL "and adapted by user newbiefan for asusrouter.\n";

close(MAIL);

print "<html><head><title>$title</title></head>\n<body>\n\n";

## HTML content sent, let us know we have sent an email
print "
<h1>$title</h1>

An Email has been sent from $from to $to

</body></html>";

Consider to set the rights to 755.
Have fun

jeremees
15-01-2012, 10:36
Let us send as example daily your logfile:

Hi, my email said that "unknown date", so I changed that date line into this:

today_long=$(date -R) #set the format for date

newbiefan
15-01-2012, 15:12
Hi, my email said that "unknown date", so I changed that date line into this:

today_long=$(date -R) #set the format for date

Well, I bet you have not installed coreutils according wengis howto.
So perform a:

ipkg install coreutils
and you are fine......
But as long as your email-account allows such a date-format, it doesn't matter......
Have fun