ASMailer – Send Email in AS 3 without server-side script
The ASMailer class sends emails using an SMTP server. ASMailer sends mail without the need of a server side language like PHP or JSP.
The ASMailer class sends emails using an SMTP server. ASMailer sends mail without the need of a server side language like PHP or JSP. This project is in alpha state. Currently sends using SMTP servers that do not require authentication or SSL connection. Currently only test on default port 25.
Usage:
import com.almerblank.fl.net.mail.Email;
import com.almerblank.fl.net.mail.ASMailer;
import com.almerblank.fl.net.mail.events.MailEvent;
import com.almerblank.fl.net.mail.events.MailErrorEvent;
private var mail:ASMailer;
private function init():void
{
mail = new ASMailer( ‘mail.yourserver.com’ );
mail.addEventListener( MailEvent.SMTP_CONNECTION_READY, _connReady );
mail.addEventListener( MailEvent.SMTP_STATUS_EVENT, _handleStatus );
mail.addEventListener( MailEvent.SMTP_EMAILING_COMPLETE, _emailsComplete );
mail.addEventListener( MailEvent.SMTP_DISCONNECTED, _disconn );
mail.addEventListener( MailErrorEvent.SMTP_SERVER_ERROR, _smtpErrorHandler );
mail.addEventListener( MailErrorEvent.SMTP_IOERROR, _smtpIOError );
mail.addEventListener( MailErrorEvent.SMTP_SECURITY_ERROR, _smtpSecError );
}
private function _disconn( event:MailEvent ):void
{
trace( ‘disconnected…’ );
}
private function _emailsComplete( event:MailEvent ):void
{
trace( ‘_emailsComplete()’ );
}
private function _smtpIOError( event:MailErrorEvent ):void
{
trace( ‘event.message = ‘ + event.message );
}
private function _smtpSecError( event:MailErrorEvent ):void
{
trace( ‘event.message = ‘ + event.message );
}
private function _handleStatus( event:MailEvent ):void
{
tf_status.text += event.status;
tf_status.text += ‘\n’;
tf_status.verticalScrollPosition = tf_status.maxVerticalScrollPosition;
}
private function _smtpErrorHandler( event:MailErrorEvent ):void
{
trace( ‘event.code = ‘ + event.code );
trace( ‘event.message = ‘ + event.message );
trace( ‘event.failedOnCommand = ‘ + event.failedOnCommand );
}
private function _connReady( event:MailEvent ):void
{
sendEmails()
}
private function sendEmails():void
{
var lipsum1:String = ‘…text…’;
var lipsum2:String = ‘…text…’;
// one email send to one person
var em:Email = new Email( ‘user@laziomatica.com’, [ 'toUser@laziomatica.com' ], ‘message 1 subject’, lipsum1 );
// one email to two people
var em2:Email = new Email( ‘user@laziomatica.com’, [ 'toUser@laziomatica.com', 'toSecondUser@3life.it' ], ‘message 2 subject’, lipsum2 );
// send multiple in an array
mail.sendMail( [ em, em2 ] );
// send one in an array
//mail.sendMail( [ em ] );
}
This code should work in Flash and Flex. The init() method would start the process.
Another MXML Mailer Application:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”init()”>
<mx:Script>
<![CDATA[
import com.almerblank.fl.net.mail.Email;
import com.almerblank.fl.net.mail.ASMailer;
import com.almerblank.fl.net.mail.events.MailEvent;
import com.almerblank.fl.net.mail.events.MailErrorEvent;
private var mail:ASMailer;
private function init():void
{
mail = new ASMailer( '10.109.70.11' );
mail.addEventListener( MailEvent.SMTP_CONNECTION_READY, _connReady );
mail.addEventListener( MailEvent.SMTP_STATUS_EVENT, _handleStatus );
mail.addEventListener( MailEvent.SMTP_EMAILING_COMPLETE, _emailsComplete );
mail.addEventListener( MailEvent.SMTP_DISCONNECTED, _disconn );
mail.addEventListener( MailErrorEvent.SMTP_SERVER_ERROR, _smtpErrorHandler );
mail.addEventListener( MailErrorEvent.SMTP_IOERROR, _smtpIOError );
mail.addEventListener( MailErrorEvent.SMTP_SECURITY_ERROR, _smtpSecError );
}
private function _disconn( event:MailEvent ):void
{
trace( 'disconnected...' );
}
private function _emailsComplete( event:MailEvent ):void
{
trace( '_emailsComplete()' );
}
private function _smtpIOError( event:MailErrorEvent ):void
{
trace( 'event.message = ' + event.message );
}
private function _smtpSecError( event:MailErrorEvent ):void
{
trace( 'event.message = ' + event.message );
}
private function _handleStatus( event:MailEvent ):void
{
tf_status.text += event.status;
tf_status.text += '\n';
tf_status.verticalScrollPosition = tf_status.maxVerticalScrollPosition;
}
private function _smtpErrorHandler( event:MailErrorEvent ):void
{
trace( 'event.code = ' + event.code );
trace( 'event.message = ' + event.message );
trace( 'event.failedOnCommand = ' + event.failedOnCommand );
}
private function _connReady( event:MailEvent ):void
{
sendEmails()
}
private function sendEmails():void
{
var lipsum1:String = '...text...';
var lipsum2:String = '...text...';
// one email send to one person
var em:Email = new Email( 'laziomatica@gmail.com', [ 'ideasviluppo@gmail.com' ], ‘message 1 subject’, lipsum1 );
// one email to two people
var em2:Email = new Email( ‘cert@laziomatica.com’, [ 'ideasviluppo@gmail.com', 'info@3life.it' ], ‘message 2 subject’, lipsum2 );
// send multiple in an array
mail.sendMail( [ em, em2 ] );
// send one in an array
//mail.sendMail( [ em ] );
}
]]>
</mx:Script>
<mx:TextArea x=”0″ y=”0″ width=”309″ height=”289″ id=”tf_status”/>
</mx:WindowedApplication>
