Results 1 to 10 of 10

Thread: [Howto] Esmtp Email, PHP, Reminder, send_logfile

  1. #1
    Join Date
    Jul 2007
    Location
    Austria
    Posts
    1,336

    Post [Howto] Esmtp Email, PHP, Reminder, send_logfile

    How to send emails and use php-mail and now perl-mail from your asus:
    precondition: installation according wengi's howto. 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.
    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:
    Code:
    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 with a perl test page.

    Have fun
    Last edited by newbiefan; 24-06-2011 at 22:09. Reason: howto perl-mail added
    Alle HowTo's, all howto's

    RT-N16 1.9.2.7-rtn-r3121, Samba, VSFTP, Lightthpd, PHP, Perl, MySQL, Serendipity, Aria2web, HDD 640GB
    RT-N66U, 16GB MicroSD/ 2 Partitions, 2,5" HDD 1TB, running with Merlin's FW and Entware, 16 Mbit A1,
    Netgear DGND 3700V2, QNAP TS119PII 4 TB, QNAP TS209 2 TB Raid1, Backup Synology DS107+ 1 TB, HP CP1515n

  2. #2
    Join Date
    Jul 2007
    Location
    Austria
    Posts
    1,336

    Send daily your logfile

    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
    Code:
    #! /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)

    Code:
    59 23 * * * root /opt/bin/sendlog
    Thats it!
    To control the function, enter
    Code:
    sendlog
    you get an email from now on of your logfile daily at 23:59
    have fun
    Last edited by newbiefan; 04-06-2011 at 12:21.
    Alle HowTo's, all howto's

    RT-N16 1.9.2.7-rtn-r3121, Samba, VSFTP, Lightthpd, PHP, Perl, MySQL, Serendipity, Aria2web, HDD 640GB
    RT-N66U, 16GB MicroSD/ 2 Partitions, 2,5" HDD 1TB, running with Merlin's FW and Entware, 16 Mbit A1,
    Netgear DGND 3700V2, QNAP TS119PII 4 TB, QNAP TS209 2 TB Raid1, Backup Synology DS107+ 1 TB, HP CP1515n

  3. #3
    Join Date
    Jul 2007
    Location
    Austria
    Posts
    1,336

    A simple to use reminder system

    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:
    Code:
    #! /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
    Code:
     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)
    Code:
    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
    Last edited by newbiefan; 05-02-2012 at 07:34.
    Alle HowTo's, all howto's

    RT-N16 1.9.2.7-rtn-r3121, Samba, VSFTP, Lightthpd, PHP, Perl, MySQL, Serendipity, Aria2web, HDD 640GB
    RT-N66U, 16GB MicroSD/ 2 Partitions, 2,5" HDD 1TB, running with Merlin's FW and Entware, 16 Mbit A1,
    Netgear DGND 3700V2, QNAP TS119PII 4 TB, QNAP TS209 2 TB Raid1, Backup Synology DS107+ 1 TB, HP CP1515n

  4. #4
    Join Date
    Jul 2007
    Location
    Austria
    Posts
    1,336

    esmtp with php configuration

    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:
    Code:
    ; 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.

    Code:
    <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
    Last edited by newbiefan; 04-06-2011 at 12:17.
    Alle HowTo's, all howto's

    RT-N16 1.9.2.7-rtn-r3121, Samba, VSFTP, Lightthpd, PHP, Perl, MySQL, Serendipity, Aria2web, HDD 640GB
    RT-N66U, 16GB MicroSD/ 2 Partitions, 2,5" HDD 1TB, running with Merlin's FW and Entware, 16 Mbit A1,
    Netgear DGND 3700V2, QNAP TS119PII 4 TB, QNAP TS209 2 TB Raid1, Backup Synology DS107+ 1 TB, HP CP1515n

  5. #5
    Join Date
    Jul 2007
    Location
    Austria
    Posts
    1,336

    PHP 404-error-page with email function

    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.

    Code:
    <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
    Code:
    ## 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:
    Code:
    http://ip.of.your.asus/notexistingpage
    You should see now your 404-error-page and get an email.

    have fun
    Last edited by newbiefan; 04-06-2011 at 12:18.
    Alle HowTo's, all howto's

    RT-N16 1.9.2.7-rtn-r3121, Samba, VSFTP, Lightthpd, PHP, Perl, MySQL, Serendipity, Aria2web, HDD 640GB
    RT-N66U, 16GB MicroSD/ 2 Partitions, 2,5" HDD 1TB, running with Merlin's FW and Entware, 16 Mbit A1,
    Netgear DGND 3700V2, QNAP TS119PII 4 TB, QNAP TS209 2 TB Raid1, Backup Synology DS107+ 1 TB, HP CP1515n

  6. #6
    Join Date
    Dec 2007
    Location
    The Netherlands - Eindhoven
    Posts
    1,767
    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

    Good job on the php scripts for a non php-coder
    Last edited by wpte; 04-06-2011 at 19:59.

  7. #7
    Join Date
    Jul 2007
    Location
    Austria
    Posts
    1,336
    Quote Originally Posted by wpte View Post
    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

    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
    Alle HowTo's, all howto's

    RT-N16 1.9.2.7-rtn-r3121, Samba, VSFTP, Lightthpd, PHP, Perl, MySQL, Serendipity, Aria2web, HDD 640GB
    RT-N66U, 16GB MicroSD/ 2 Partitions, 2,5" HDD 1TB, running with Merlin's FW and Entware, 16 Mbit A1,
    Netgear DGND 3700V2, QNAP TS119PII 4 TB, QNAP TS209 2 TB Raid1, Backup Synology DS107+ 1 TB, HP CP1515n

  8. #8
    Join Date
    Jul 2007
    Location
    Austria
    Posts
    1,336

    howto use perl-mail with lighttpd

    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:
    Code:
    ipkg list_installed |grep perl
    should give you the following output:
    Code:
    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:
    Code:
                                    "mod_cgi",
    and after section index-file.names insert this:
    Code:
    #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:
    Code:
    #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 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.
    Code:
    #!/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
    Attached Files Attached Files
    Last edited by newbiefan; 15-01-2012 at 08:06.
    Alle HowTo's, all howto's

    RT-N16 1.9.2.7-rtn-r3121, Samba, VSFTP, Lightthpd, PHP, Perl, MySQL, Serendipity, Aria2web, HDD 640GB
    RT-N66U, 16GB MicroSD/ 2 Partitions, 2,5" HDD 1TB, running with Merlin's FW and Entware, 16 Mbit A1,
    Netgear DGND 3700V2, QNAP TS119PII 4 TB, QNAP TS209 2 TB Raid1, Backup Synology DS107+ 1 TB, HP CP1515n

  9. #9

    Send daily your logfile

    Quote Originally Posted by newbiefan View Post
    Let us send as example daily your logfile:
    Hi, my email said that "unknown date", so I changed that date line into this:
    Code:
    today_long=$(date -R)     #set the format for date
    Last edited by newbiefan; 15-01-2012 at 15:12.

  10. #10
    Join Date
    Jul 2007
    Location
    Austria
    Posts
    1,336
    Quote Originally Posted by jeremees View Post
    Hi, my email said that "unknown date", so I changed that date line into this:
    Code:
    today_long=$(date -R)     #set the format for date
    Well, I bet you have not installed coreutils according wengis howto.
    So perform a:
    Code:
    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
    Alle HowTo's, all howto's

    RT-N16 1.9.2.7-rtn-r3121, Samba, VSFTP, Lightthpd, PHP, Perl, MySQL, Serendipity, Aria2web, HDD 640GB
    RT-N66U, 16GB MicroSD/ 2 Partitions, 2,5" HDD 1TB, running with Merlin's FW and Entware, 16 Mbit A1,
    Netgear DGND 3700V2, QNAP TS119PII 4 TB, QNAP TS209 2 TB Raid1, Backup Synology DS107+ 1 TB, HP CP1515n

Similar Threads

  1. Îòïðàâêà IP àäðåñà WAN èíòåðôåéñà ïî SMS è eMail
    By ncat in forum Russian Discussion - РУССКИЙ (RU)
    Replies: 30
    Last Post: 20-03-2013, 16:14
  2. Powiadomienia na email rtorrent+nail
    By jabol.91 in forum Polish Discussion - Polski (PL)
    Replies: 2
    Last Post: 15-11-2011, 12:48
  3. Email from router
    By Googol in forum WL-500g Q&A
    Replies: 33
    Last Post: 26-08-2010, 07:32
  4. [How-To] Send email with php function
    By cichy in forum WL-500gP Tutorials
    Replies: 3
    Last Post: 11-09-2009, 16:01
  5. esmtp remove Message-Id in the body of the mail
    By boobax in forum WL-500g Q&A
    Replies: 3
    Last Post: 11-02-2008, 07:49

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •