26 Eylül 2007 Çarşamba

PHP SMTP Authentication: send an email through an authenticated SMTP server

Author: Matthew R. Miller, http://www.bytemycode.com/snippets/snippet/223/


<?php
function mymail($to,$subject,$message,$headers)
{
// set as global variable
global $GLOBAL;
// get From address
if ( preg_match("/From:.*?[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+.*/", $headers, $froms) ) {
preg_match("/[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+/", $froms[0], $fromarr);
$from = $fromarr[0];
}
// Open an SMTP connection
$cp = fsockopen ($GLOBAL["SMTP_SERVER"], $GLOBAL["SMTP_PORT"], &$errno, &$errstr, 1);
if (!$cp)
return "Failed to even make a connection";
$res=fgets($cp,256);
if(substr($res,0,3) != "220") return "Failed to connect";
// Say hello...
fputs($cp, "HELO ".$GLOBAL["SMTP_SERVER"]."\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "Failed to Introduce";
// perform authentication
fputs($cp, "auth login\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "334") return "Failed to Initiate Authentication";
fputs($cp, base64_encode($GLOBAL["SMTP_USERNAME"])."\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "334") return "Failed to Provide Username for Authentication";
fputs($cp, base64_encode($GLOBAL["SMTP_PASSWORD"])."\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "235") return "Failed to Authenticate";
// Mail from...
fputs($cp, "MAIL FROM: <$from>\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "MAIL FROM failed";
// Rcpt to...
fputs($cp, "RCPT TO: <$to>\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "RCPT TO failed";
// Data...
fputs($cp, "DATA\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "354") return "DATA failed";
// Send To:, From:, Subject:, other headers, blank line, message, and finish
// with a period on its own line (for end of message)
fputs($cp, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "Message Body Failed";
// ...And time to quit...
fputs($cp,"QUIT\r\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "221") return "QUIT failed";
return true;
}
?>


Extracts all email adresses from a given string, and returns them as array

Author: Jonas John, http://codedump.jonasjohn.de/

<?php
/*
** Function: extract_emails (PHP)
** Desc: This function extracts all E-Mail adresses from a given string, and returns them as array.
** Author: Jonas John
*/

function extract_emails($str)
{
// This regular expression extracts all emails from
// a string:
$regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
preg_match_all($regexp, $str, $m);
return isset($m[0]) ? $m[0] : array();
}
?>


Email address validator

Author: Tobias Ratschiller & izzy, http://www.zend.com/tips/tips.php?id=224&single=1

<?php

/*
Original script by Tobias Ratschiller.
* top level domain must have at least 2 chars
* there can be more than one dot in the address
*/

function is_email($address) {
$rc1 = (ereg('^[-!#$%&'*+./0-9=?A-Z^_`a-z{}~]+'.
'@'.
'[-!#$%&'*+\/0-9=?A-Z^_`a-z{}~]+.'.
'[-!#$%&'*+\./0-9=?A-Z^_`a-z{}~]+$',
$address));
$rc2 = (preg_match('/.+.ww+$/',$address));
return ($rc1 && $rc2);
}
?>


Emailer Class

author: edolecki, http://www.bigbold.com/snippets/tag/php/6

<?php
import mx.events.EventDispatcher;
import mx.utils.Delegate;
class Emailer {
// required for EventDispatcher:
public var addEventListener:Function;
public var removeEventListener:Function;
private var dispatchEvent:Function;

// use to communicate with php script
private var _lv:LoadVars;
// holds address of sender
private var _sentFrom:String;
// constructor
public function Emailer() {
EventDispatcher.initialize(this);
_lv = new LoadVars();
}
//
private function dataReceived(dataxfer_ok:Boolean):Void {
// if some problem with loadVars transfer, pass back error=2
if (!dataxfer_ok) dispatchEvent({target:this, type:'mailSent', errorFlag:2});
// otherwise pass back error code returned from script
else dispatchEvent({target:this, type:'mailSent', errorFlag:Number(_lv["faultCode"])});
}
// Use loadvars object to send data (set to call dataReceived when script returns data)
public function sendEmail(sub:String, fn:String, fe:String, msg:String, rep:String):Void {
// if user already sent from this address, show error msg
if (_sentFrom == fe) dataReceived(false);
// otherwise set up and send
else {
_sentFrom = fe;
// specify function to handle results, make scope = Emailer
_lv.onLoad = Delegate.create(this, dataReceived);
// set up properties of lv to items to be POSTed
_lv.subject = sub;
_lv.name = fn;
_lv.email = fe;
_lv.message = msg;
_lv.reply = rep;
// call script
_lv.sendAndLoad("sendemail.php", _lv, "POST");
}
}
}
?>


6 Eylül 2007 Perşembe

Asp Report Maker



  • Detail and Summary Report
  • Crosstab Report
  • Simple Charts
  • Advanced Security
  • Export to HTML/Excel/Word
  • Custom View
  • Customizable Template
  • Database Synchronization

demo