DSM-G600, DNS-3xx and NSA-220 Hack Forum

Unfortunately no one can be told what fun_plug is - you have to see it for yourself.

You are not logged in.

Announcement

#1 2007-10-23 19:46:20

kbe
New member
Registered: 2007-04-09
Posts: 3

Script for sending e-mail alerts (Sendmail replacenent)

As I know DNS-323 doesn't have sendmail for sending e-mails (beside chroot'ed Debian).
So I wrote this script as sendmail replacement for my NAS.
This script periodically searches (via cron) new files in Exchange directory and then alerts me.
It's rather raw and quick but working for me.
Please feel free to correct it or post your own script of this kind if you like.

Code:

<?
$search_path = "/mnt/HD_a2/Exchange";
$bin_prefix = "/mnt/HD_a2/fun_plug.d/bin";
$search_time_interval = 30;

function email_me($MESSAGE) {

$fromName = "DNS-323 Linux Box";
$fromEmail = "user@server.com";
$fromMailer = "PHP SMTP socket script";
$smtp = "smtp.server.com";
$smtp_port = 25;
$charset = "ISO-8859-1";
$subject = "DNS-323 Upload Alert";


$connect = fsockopen ($smtp, $smtp_port, $errno, $errstr, 5);
$rcv = fgets($connect, 512);
fputs($connect, "EHLO server.com\r\n");
$rcv = fgets($connect, 512);
$rcv = fgets($connect, 512); 
fputs($connect, "AUTH LOGIN\r\n"); 
$rcv = fgets($connect, 512);
fputs($connect, base64_encode("Login") ."\r\n"); //Your login
$rcv = fgets($connect, 512);
fputs($connect, base64_encode("Password") . "\r\n");  //Your password
$rcv = fgets($connect, 512);
fputs($connect, "MAIL FROM: user@server.com\r\n");
$rcv = fgets($connect, 512);
fputs($connect, "RCPT TO:user@server.com\r\n");
$rcv = fgets($connect, 512);
fputs($connect, "DATA\r\n");
$rcv = fgets($connect, 512);

fputs($connect, "Subject: $subject\r\n");
fputs($connect, "From: $fromName <$fromEmail>\r\n");
fputs($connect, "To: $toRcpt\r\n");
fputs($connect, "X-Sender: <$fromEmail>\r\n");
fputs($connect, "Return-Path: <$fromEmail>\r\n");
fputs($connect, "Errors-To: <$fromEmail>\r\n");
fputs($connect, "Message-Id: <".md5(uniqid(rand())).".".preg_replace("/[^a-z0-9]/i", "", $fromName)."@$smtp>\r\n");
fputs($connect, "X-Mailer: PHP - $fromMailer\r\n");
fputs($connect, "X-Priority: 3\r\n");
fputs($connect, "Date: ".date("r")."\r\n");
fputs($connect, "Content-Type: text/plain; charset=$charset\r\n");
fputs($connect, $MESSAGE);

fputs($connect, "\r\n.\r\n");
$rcv = fgets($connect, 512);

fputs($connect, "RSET\r\n");
$rcv = fgets($connect, 512);
fputs($connect, "QUIT\r\n");
fclose($connect);

}
$search_result = shell_exec("$bin_prefix/find $search_path  -mmin -$search_time_interval -type f");
$search_result = str_replace($search_path, "", $search_result);
$search_result_lines = explode("\n", $search_result);

foreach($search_result_lines as $theline) {  
    ltrim($theline);
    }
    
$search_result = implode("\r\n", $search_result_lines);
$search_result_count = shell_exec("$bin_prefix/find $search_path  -mmin -$search_time_interval -type f | $bin_prefix/wc -l");

if ($search_result_count > 0) { 
    email_me($search_result);
    echo "Mail sent!\n"; 
    } else {
    echo "No new files!\n";
}

?>

Last edited by kbe (2007-10-23 19:57:35)

Offline

 

#2 2007-11-02 20:02:36

blbrown
Member
Registered: 2007-11-02
Posts: 88
Website

Re: Script for sending e-mail alerts (Sendmail replacenent)

Last night I figured out an alternative to using PHP (and sendmail or another binary that's not-yet available on the DNS-323) for sending email through an SMTP server.  My solution is just a bash script that uses telnet in order to send an e-mail.  I have successfully used it to send an e-mail automatically from a script.

This is the early stages, so you might want to modify it, but the idea is here and works:

Code:

#!/bin/sh

# What email address do you want status emails sent to?
EMAILADDRESSES="<user@comany.com>"

# What email address do you want status emails to be listed as being from?
# NOTE: This field must be surrounded by quotes "
FROMADDRESS="dns323@mydomain.com"

# What's the SMTP server used to send email?
SMTPS=smtp.usfamily.net

# What's the Base-64 encoded username used to log into the SMTP server?
# (Use http://gtools.org/tool/base64-encode-decode/ or
#  http://legacy.dillfrog.com/tools/base-64_encode/ to encode your username.)
#user@usfamily.net
SMTPU="dXNlckB1c2ZhbWlseS5uZXQ="

# What's the Base-64 encoded password used to log into the SMTP server?
# (Use http://gtools.org/tool/base64-encode-decode/ or
#  http://legacy.dillfrog.com/tools/base-64_encode/ to encode your password.)
SMTPP="bm9zdWNodGhpbmc="

# What's the port number used to connect to the SMTP server?
SMTPR=25

# Set up defaults
RTDIR=/mnt/HD_a2/fun_plug.d
EMLRUNFILE="${RTDIR}/email-body.txt"
TEMPEMAILFILE="${RTDIR}/temporary-email-file.txt"


if ping "${SMTPS}" -c 3
then
    {
    echo "HELO ${FROMADDRESS##*@}"
    sleep 2
    echo "AUTH LOGIN"
    sleep 2
    echo "${SMTPU}"
    sleep 2
    echo "${SMTPP}"
    sleep 2
    echo "MAIL FROM: <${FROMADDRESS}>"
    sleep 2
    echo "RCPT TO: ${EMAILADDRESSES}"
    sleep 10
    echo "DATA"
    sleep 2
    echo "Subject: DNS-323 BackupNetClone Status"
    sleep 2
    echo "From: DNS-323 Automated Backup <${FROMADDRESS}>"
    sleep 2
    echo "To: ${EMAILADDRESSES}"
    sleep 2
    echo "X-Sender: ${FROMADDRESS}"
    sleep 2
    echo "Date: `date`"
    sleep 2
    echo ""
    sleep 2
    cat "${EMLRUNFILE}"
    sleep 10
    echo ""
    sleep 2
    echo "." 
    sleep 2
    echo "QUIT"
    } | telnet "${SMTPS}" "${SMTPR}" >>"${TEMPEMAILFILE}" 2>&1
fi

SERVERBUSYTEXT=`grep -e "^451 " "${TEMPEMAILFILE}"`

cat "${TEMPEMAILFILE}"

This script is part of a much larger project for automating rsync.  I know others have posted rsync solutions here before, but I think my solution combines the best of all options.  I've attached the readme file in case anyone is curious.  The project is currently at 95% complete.  When it's done, I'll post it in the appropriate topic(s).

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2010 PunBB