- $ Smtpserver = "*****";
- $ Smtpserverport = 25;
- $ Smtpuser = "******";
- $ Smtppass = "*******";
- $ Smtp = new smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass); // Here, true indicates that authentication is used; otherwise, authentication is not used.
- $ Smtp-> debug = false;
- // $ Emailtype = "HTML ";
- For ($ I = 0; $ I <5; $ I ++ ){
- $ Smtp-> sendmail ("*****", "*******", "Hello world! "," This is only a test! ");
- }
- Echo "sent in total$ IEmail! ";
- ?>
The following describes the implementation of a specific class.
Class smtp {
- /* Public Variables */
- Var $ smtp_port;
- Var $ time_out;
- Var $ host_name;
- Var $ log_file;
- Var $ relay_host;
- Var $ debug;
- Var $ auth;
- Var $ user;
- Var $ pass;
- /* Private Variables */
- Var $ sock;
- /* Constractor */
- Function smtp ($ relay_host = "", $ smtp_port = 25, $ auth = false, $ user, $ pass)
- {
- $ This-> debug = false;
- $ This-> smtp_port = $ smtp_port;
- $ This-> relay_host = $ relay_host;
- $ This-> time_out = 30; // is used in fsockopen ()
- $ This-> auth = $ auth; // auth
- $ This-> user = $ user;
- $ This-> pass = $ pass;
- $ This-> host_name = "localhost"; // is used in HELO command
- $ This-> log_file = "";
- $ This-> sock = false;
- }
- /* Main Function */
- Function sendmail ($ to, $ from, $ subject = "", $ body = "", $ mailtype = "", $ cc = "", $ bcc = "", $ additional_headers = "")
- {
- $ Mail_from = $ this-> get_address ($ this-> strip_comment ($ from ));
- $ Body = ereg_replace ("(^ | () (.)", "1.3", $ body );
- $ Header. = "maid: 1.0 ";
- If ($ mailtype = "HTML "){
- $ Header. = "Content-Type: text/html ";
- }
- $ Header. = "To:". $ ."";
- If ($ cc! = ""){
- $ Header. = "Cc:". $ cc ."";
- }
- $ Header. = "From: $ from <". $ from. "> ;";
- $ Header. = "Subject:". $ subject ."";
- $ Header. = $ additional_headers;
- $ Header. = "Date:". date ("r ")."";
- $ Header. = "X-Mailer: By Redhat (PHP/". phpversion ().")";
- List ($ msec, $ sec) = explode ("", microtime ());
- $ Header. = "Message-ID: <". date ("YmdHis", $ sec ). ". ". ($ msec * 1000000 ). ". ". $ mail_from. "> ;";
- $ TO = explode (",", $ this-> strip_comment ($ ));
- If ($ cc! = ""){
- $ TO = array_merge ($ TO, explode (",", $ this-> strip_comment ($ cc )));
- }
- If ($ bcc! = ""){
- $ TO = array_merge ($ TO, explode (",", $ this-> strip_comment ($ bcc )));
- }
- $ Sent = true;
- Foreach ($ TO as $ rcpt_to ){
- $ Rcpt_to = $ this-> get_address ($ rcpt_to );
- If (! $ This-> smtp_sockopen ($ rcpt_to )){
- $ This-> log_write ("Error: Cannot send email to". $ rcpt_to ."");
- $ Sent = false;
- Continue;
- }
- If ($ this-> smtp_send ($ this-> host_name, $ mail_from, $ rcpt_to, $ header, $ body )){
- $ This-> log_write ("E-mail has been sent to <". $ rcpt_to. "> ;");
- } Else {
- $ This-> log_write ("Error: Cannot send email to <". $ rcpt_to. "> ;");
- $ Sent = false;
- }
- Fclose ($ this-> sock );
- $ This-> log_write ("Disconnected from remote host ");
- }
- Return $ sent;
- }
/* Private Functions */
- Function smtp_send ($ helo, $ from, $ to, $ header, $ body = "")
- {
- If (! $ This-> smtp_putcmd ("HELO", $ helo )){
- Return $ this-> smtp_error ("sending HELO command ");
- }
- // Auth
- If ($ this-> auth ){
- If (! $ This-> smtp_putcmd ("auth login", base64_encode ($ this-> user ))){
- Return $ this-> smtp_error ("sending HELO command ");
- }
- If (! $ This-> smtp_putcmd ("", base64_encode ($ this-> pass ))){
- Return $ this-> smtp_error ("sending HELO command ");
- }
- }
- If (! $ This-> smtp_putcmd ("MAIL", "FROM: <". $ from. "> ;")){
- Return $ this-> smtp_error ("sending mail from command ");
- }
- If (! $ This-> smtp_putcmd ("RCPT", "TO: <". $ to. "> ;")){
- Return $ this-> smtp_error ("sending rcpt to command ");
- }
- If (! $ This-> smtp_putcmd ("DATA ")){
- Return $ this-> smtp_error ("sending DATA command ");
- }
- If (! $ This-> smtp_message ($ header, $ body )){
- Return $ this-> smtp_error ("sending message ");
- }
- If (! $ This-> smtp_eom ()){
- Return $ this-> smtp_error ("sending ; ;. ; ; [EOM] ");
- }
- If (! $ This-> smtp_putcmd ("QUIT ")){
- Return $ this-> smtp_error ("sending QUIT command ");
- }
- Return true;
- }
- Function smtp_sockopen ($ address)
- {
- If ($ this-> relay_host = ""){
- Return $ this-> smtp_sockopen_mx ($ address );
- } Else {
- Return $ this-> smtp_sockopen_relay ();
- }
- }
- Function smtp_sockopen_relay ()
- {
- $ This-> log_write ("Trying to". $ this-> relay_host. ":". $ this-> smtp_port ."");
- $ This-> sock = @ fsockopen ($ this-> relay_host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
- If (! ($ This-> sock & $ this-> smtp_ OK ())){
- $ This-> log_write ("Error: Cannot connenct to relay host". $ this-> relay_host ."");
- $ This-> log_write ("Error:". $ errstr. "(". $ errno .")");
- Return false;
- }
- $ This-> log_write ("Connected to relay host". $ this-> relay_host ."");
- Return true ;;
- }
- Function smtp_sockopen_mx ($ address)
- {
- $ Domain = ereg_replace ("^. + @ ([^ @] +) $", "1", $ address );
- If (! @ Getmxrr ($ domain, $ MXHOSTS )){
- $ This-> log_write ("Error: Cannot resolve MX" ". $ domain .""");
- Return false;
- }
- Foreach ($ MXHOSTS as $ host ){
- $ This-> log_write ("Trying to". $ host. ":". $ this-> smtp_port ."");
- $ This-> sock = @ fsockopen ($ host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
- If (! ($ This-> sock & $ this-> smtp_ OK ())){
- $ This-> log_write ("Warning: Cannot connect to mx host". $ host ."");
- $ This-> log_write ("Error:". $ errstr. "(". $ errno .")");
- Continue;
- }
- $ This-> log_write ("Connected to mx host". $ host ."");
- Return true;
- }
- $ This-> log_write ("Error: Cannot connect to any mx hosts (". implode (",", $ MXHOSTS ).")");
- Return false;
- }
- Function smtp_message ($ header, $ body)
- {
- Fputs ($ this-> sock, $ header. "". $ body );
- $ This-> smtp_debug ("> ;". str_replace ("","". ">;", $ header. "> ;". $ body. "> ;"));
- Return true;
- }
- Function smtp_eom ()
- {
- Fputs ($ this-> sock ,".");
- $ This-> smtp_debug (". [EOM]");
- Return $ this-> smtp_ OK ();
- }
- Function smtp_ OK ()
- {
- $ Response = str_replace ("", "", fgets ($ this-> sock, 512 ));
- $ This-> smtp_debug ($ response ."");
- If (! Ereg ("^ [23]", $ response )){
- Fputs ($ this-> sock, "QUIT ");
- Fgets ($ this-> sock, 512 );
- $ This-> log_write ("Error: Remote host returned" ". $ response .""");
- Return false;
- }
- Return true;
- }
- Function smtp_putcmd ($ cmd, $ arg = "")
- {
- If ($ arg! = ""){
- If ($ cmd = "") $ cmd = $ arg;
- Else $ cmd = $ cmd. "". $ arg;
- }
- Fputs ($ this-> sock, $ cmd ."");
- $ This-> smtp_debug (">;". $ cmd ."");
- Return $ this-> smtp_ OK ();
- }
- Function smtp_error ($ string)
- {
- $ This-> log_write ("Error: Error occurred while". $ string .".");
- Return false;
- }
- Function log_write ($ message)
- {
- $ This-> smtp_debug ($ message );
- If ($ this-> log_file = ""){
- Return true;
- }
- $ Message = date ("M d H: I: s"). get_current_user (). "[". getmypid (). "]:". $ message;
- If (! @ File_exists ($ this-> log_file) |! ($ Fp = @ fopen ($ this-> log_file, ""))){
- $ This-> smtp_debug ("Warning: Cannot open log file" ". $ this-> log_file .""");
- Return false ;;
- }
- Flock ($ fp, LOCK_EX );
- Fputs ($ fp, $ message );
- Fclose ($ fp );
- Return true;
- }
- Function strip_comment ($ address)
- {
- $ Comment = "([^ ()] *)";
- While (ereg ($ comment, $ address )){
- $ Address = ereg_replace ($ comment, "", $ address );
- }
- Return $ address;
- }
- Function get_address ($ address)
- {
- $ Address = ereg_replace ("([]) +", "", $ address );
- $ Address = ereg_replace ("^. * <(. +)>;. * $", "1", $ address );
- Return $ address;
- }
- Function smtp_debug ($ message)
- {
- If ($ this-> debug ){
- Echo $ message .";";
- }
- }
- }
- ?>
|