Send email on script

Re: Send email on script

Postby fvdw » Thu Aug 23, 2012 9:49 pm

seems the problem is not with sendmail.
For whatever reason the nwsp2 seems not be able to connect with smtp servers behind the router. Inside LAN it can connect to an smtp server but not to one on the internet.
its not a router issue I tried to contact the mail servers via my windows pc using telnet (in a command window) and was able to connect to them and send an e-mail. So the router is not the problem.
I tried also telnet on the nwsp2 and could connect to smtp servers in the LAN but not on the internet...
I can ping to the mail servers using the nwsp2 so it is able to go through the router and contact the server but it connot connect to port 25 or 587 (I tried also 465 and that worked partly on one server, the connection was established but i could not give commands. probably this is because port 465 is smtps and I need to use an ssl connection instead of telnet (plain text)
I used openssl client and was able to connect to that server abd sent an e-mail from the nwsp2.
So it seems to be finding the proper authentication method
Anyhow some progress is made..
fvdw
Site Admin - expert
 
Posts: 13471
Joined: Tue Apr 12, 2011 2:30 pm
Location: Netherlands

Re: Send email on script

Postby Tumppi73 » Sat Aug 25, 2012 1:22 pm

Hi

I am able to send email from via an SMTP server that requires no authentication nor is SSLed using a PHP mailer script.

http://code.google.com/a/apache-extras.org/p/phpmailer

Code: Select all
require_once('class.phpmailer.php');

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

$contents="<body style=\"margin: 10px;\">
         <div style=\"width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;\">
            <H1>Torrent ".@$tr_torrent_name." completed</H1>
               <ul>
                  <li> in ".@$tr_time_localtime." ".date('c',intval(@$tr_time_localtime))."
                  <li> using ".@$tr_app_version."
                  <li>at ".date('c')."
                  <li> in directory ".@$tr_torrent_dir."
                  <li> hash=".@$tr_torrent_hash."
                  <li> id=".@$tr_torrent_id."
               </ul>
            </div>
         </body>";


try
   {
   $mail->Host       = "smtp.XXX.XX; // SMTP server
   $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
   $mail->AddAddress('YY@gmail.com', 'AA BB');
   $mail->SetFrom('XXXX.XXXXXX@XXX.XX', 'CCCCC DDDD');
   $mail->AddReplyTo('XXXX.XXXXXX@XXX.XX', 'CCCCC DDDD');
   $mail->Subject = 'Torrent '.$tr_name.' completed';
   $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
   $mail->MsgHTML($contents);
   $mail->Send();
   echo "Message Sent OK</p>\n";
   }
catch (phpmailerException $e)
   {
   echo $e->errorMessage(); //Pretty error messages from PHPMailer
   }
catch (Exception $e)
   {
   echo $e->getMessage(); //Boring error messages from anything else!
   }


As there is no SSL support it does not support e.g. connecting to Gmail.

I am also working on a script to get a bit more data from the torrent server via RPC to be attached to the mail (e.g. files included, their sizes), and the function to remove the torrent from Transmission and move the completed files either to "music" or "movies" folder depending on what the files inside are.

That is almost done, was a it tricky as rpc requires session id capture, http basic authentication and JSON posting and there is no support for CURL. Managed to do it with file_get_contents when played around with HTTP heades manually...
Tumppi73
 
Posts: 6
Joined: Thu Aug 16, 2012 11:51 am

Re: Send email on script

Postby Tumppi73 » Sat Aug 25, 2012 1:23 pm

Oh, and the actual script that is called when torrent is completed, is simply

#!/bin/sh
##

php /share/1000/public/php-scripts/torrent_completed.php tr_app_version="$TR_APP_VERSION" tr_time_localtime="$TR_TIME_LOCALTIME" tr_torrent_dir="$TR_TORRENT_DIR" tr_torrent_hash="$TR_TORRENT_HASH" tr_torrent_id="$TR_TORRENT_ID" tr_torrent_name="$TR_TORRENT_NAME"
echo "COMPLETED " > output.txt
Tumppi73
 
Posts: 6
Joined: Thu Aug 16, 2012 11:51 am

Re: Send email on script

Postby fvdw » Sat Aug 25, 2012 4:13 pm

nice work :-D well done


In the mean I manged to get sendmail working to send e-mails to smtp mail server with authentication.
I recompiled sendmail latest version 8.14.5 and included sasl2 support needed for authentication.
Then senddmail must be configured properly to work as smtp client. So no need to run sendmail server on the nas with all possible dangers. What you will need is access to a smtp server that allows you to sent e-mails. Most of the time the one of your ISP will do just fine.

Code: Select all
Aug 25 16:49:21 (none) mail.info sendmail[27599]: q7PFn0jJ027599: to=fvdw@xxxx.yy, ctladdr=root (0/0), delay=00:00:21, xdelay=00:00:20, mailer=relay, pri=30119, relay=mail.xxxxxxxxx.nl. [88.xxx.xxx.xxx], dsn=2.0.0, stat=Sent (Ok: queued as 7EE87394303)

(I replaced some numbers because of privacy reasons


But I can also I can sent from command line e-mails using an ssl connection and using openssl as client, so without using sendmail

basically you need this command
Code: Select all
 openssl s_client -crlf -connect smtp-server-name:465

"smtp-server-name" must be repalced by the actual name of the smtp server you want to connect to (for example mail.example.com)

For smtp servers that require authenciation you can then use AUTH command and supply the necessary data.

However it is not so convient as AUTH strings needs to be genrated based on username and password. Also the fact that you need to know the name of the mail sever is a set back as we usually don't know that.


Later more info
fvdw
Site Admin - expert
 
Posts: 13471
Joined: Tue Apr 12, 2011 2:30 pm
Location: Netherlands

Re: Send email on script

Postby fvdw » Mon Aug 27, 2012 3:41 pm

the problem with sendmail will be that the configuration file probably will need o be generated for each system as not all ISP's allow sending e-mails via port 25 and you are forced to use the smtp server of your ISP. That normally needs a username and password. Also there will need to be defined a firle with authorisation info that sendmail will need to be able to ccess the ISP smtp server. The reason why ISP's do this is fighting spam and running the risk that the domain of the isp is classified as spam and rejected by other mail servers.. Setting up on the nas an own e-mail server is also to much for the little brothers as nwsp1 and spd8020. So probably it will become an implementation that sendmail is used as smtp client sending mail via an external smtp server. The advantage of this setup is that there is no sendmail server running on the system eating resources but also opening security holes.
fvdw
Site Admin - expert
 
Posts: 13471
Joined: Tue Apr 12, 2011 2:30 pm
Location: Netherlands

Re: Send email on script

Postby fvdw » Mon Aug 27, 2012 5:45 pm

in the meantime i also tried to compile the openssl module for our php version and I succeeded.

I attached it for you. It goes in /usr/lib/php/extensions
of course you also need to add a line in php.ini to load when booting the nas.

php526-openssl-module-fvdw-sl-27-aug12.zip


Here a screen shot that is loaded...and enabled

openssl.JPG
You do not have the required permissions to view the files attached to this post.
fvdw
Site Admin - expert
 
Posts: 13471
Joined: Tue Apr 12, 2011 2:30 pm
Location: Netherlands

Re: Send email on script

Postby fvdw » Mon Aug 27, 2012 7:21 pm

In addition I did a test with phpmailer on a server using authenciation, I used the gmail avance4d test script and only changed it to use my isp's mail server and it worked ok

The script used
Code: Select all
<html>
<head>
<title>PHPMailer - SMTP (myisp) advanced test</title>
</head>
<body>

<?php
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

try {
  $mail->Host       = "mail.myisp.yy"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
  $mail->Host       = "mail.myisp.yy";      // sets myisp as the SMTP server
  $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
  $mail->Username   = "myname@myisp.yy";  // myisp username
  $mail->Password   = "mypassword";            // myisp password
  $mail->AddReplyTo('info@xxxxx.yy', 'First Last');
  $mail->AddAddress('fvdw@xxxxx.yy', 'Frans vdw');
  $mail->SetFrom('fvdw@myisp.yy', f vdw');
  $mail->AddReplyTo('fvdwmyisp@myisp.yy', 'f vdwmyisp');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}
?>

</body>
</html>


running the script
Code: Select all
root@fvdwsl-nwsp2:/share/1000/fvdw/PHPMailer_5.2.1/examples # php test1.php
<html>
<head>
<title>PHPMailer - SMTP (myisp) advanced test</title>
</head>
<body>

SMTP -> FROM SERVER:220 myisp isp-aos-01.myisp.intern ESMTP

<br />SMTP -> FROM SERVER: 250-isp-aos-01.myisp.intern
250-PIPELINING
250-SIZE 16000000
250-ETRN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

<br />SMTP -> FROM SERVER:250 2.1.0 Ok

<br />SMTP -> FROM SERVER:250 2.1.5 Ok

<br />SMTP -> FROM SERVER:354 End data with <CR><LF>.<CR><LF>

<br />SMTP -> FROM SERVER:250 2.0.0 Ok: queued as 497103DC129

<br />Message Sent OK</p>


:punk so the openssl module works fine !!

Using phpmailer is much easier then using sendmail or lets say much easier then setting up sendmail
fvdw
Site Admin - expert
 
Posts: 13471
Joined: Tue Apr 12, 2011 2:30 pm
Location: Netherlands

Previous

Return to Development

Who is online

Users browsing this forum: No registered users and 8 guests