A complete SMTP class of PHP (solve the problem when the mail server needs verification)

Source: Internet
Author: User
Tags ereg

SMTP. php

<? PHP
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 ("(^ | (\ r \ n) (\.)", "\ 1. \ 3", $ body );

$ Header. = "mime-version: 1.0 \ r \ n ";

If ($ mailtype = "html "){

$ Header. = "Content-Type: text/html \ r \ n ";

}

$ Header. = "to:". $ to. "\ r \ n ";

If ($ CC! = ""){

$ Header. = "cc:". $ cc. "\ r \ n ";

}

$ Header. = "from: $ from <". $ from. "> \ r \ n ";

$ Header. = "Subject:". $ subject. "\ r \ n ";

$ Header. = $ additional_headers;

$ Header. = "Date:". date ("R"). "\ r \ n ";

$ Header. = "X-mailer: by RedHat (PHP/". phpversion (). ") \ r \ n ";

List ($ msec, $ Sec) = explode ("", microtime ());

$ Header. = "message-ID: <". date ("ymdhis", $ sec ). ". ". ($ msec * 1000000 ). ". ". $ mail_from. "> \ r \ n ";

$ 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. "\ n ");

$ 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. "> \ n ");

} Else {

$ This-> log_write ("error: cannot send email to <". $ rcpt_to. "> \ n ");

$ Sent = false;

}

Fclose ($ this-> sock );

$ This-> log_write ("disconnected from remote host \ n ");

}

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 <CR> <LF>. <CR> <LF> [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. "\ n ");

$ 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. "\ n ");

$ This-> log_write ("error:". $ errstr. "(". $ errno. ") \ n ");

Return false;

}

$ This-> log_write ("connected to Relay host". $ this-> relay_host. "\ n ");

Return true ;;

}

Function smtp_sockopen_mx ($ address)

{

$ Domain = ereg_replace ("^. + @ ([^ @] +) $", "\ 1", $ address );

If (! @ Getmxrr ($ domain, $ mxhosts )){

$ This-> log_write ("error: cannot resolve MX \" ". $ domain." \ "\ n ");

Return false;

}

Foreach ($ mxhosts as $ host ){

$ This-> log_write ("trying to". $ host. ":". $ this-> smtp_port. "\ n ");

$ 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. "\ n ");

$ This-> log_write ("error:". $ errstr. "(". $ errno. ") \ n ");

Continue;

}

$ This-> log_write ("connected to MX host". $ host. "\ n ");

Return true;

}

$ This-> log_write ("error: cannot connect to any MX hosts (". implode (",", $ mxhosts). ") \ n ");

Return false;

}

Function smtp_message ($ header, $ body)

{

Fputs ($ this-> sock, $ header. "\ r \ n". $ body );

$ This-> smtp_debug ("> ". str_replace ("\ r \ n", "\ n ". ">", $ header. "\ n> ". $ body. "\ n> "));

Return true;

}

Function smtp_eom ()

{

Fputs ($ this-> sock, "\ r \ n. \ r \ n ");

$ This-> smtp_debug (". [Eom] \ n ");

Return $ this-> smtp_ OK ();

}

Function smtp_ OK ()

{

$ Response = str_replace ("\ r \ n", "", fgets ($ this-> sock, 512 ));

$ This-> smtp_debug ($ response. "\ n ");

If (! Ereg ("^ [23]", $ response )){

Fputs ($ this-> sock, "Quit \ r \ n ");

Fgets ($ this-> sock, 512 );

$ This-> log_write ("error: remote host returned \" ". $ response." \ "\ n ");

Return false;

}

Return true;

}

Function smtp_putcmd ($ cmd, $ Arg = "")

{

If ($ Arg! = ""){

If ($ cmd = "") $ cmd = $ ARG;

Else $ cmd = $ cmd. "". $ ARG;

}

Fputs ($ this-> sock, $ cmd. "\ r \ n ");

$ This-> smtp_debug (">". $ cmd. "\ n ");

Return $ this-> smtp_ OK ();

}

Function smtp_error ($ string)

{

$ This-> log_write ("error: error occurred while". $ string. ". \ n ");

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 (LOG_FILE ">! @ File_exists ($ this-> LOG_FILE) |! ($ Fp = @ fopen ($ this-> LOG_FILE, ""))){

$ This-> smtp_debug ("Warning: cannot open log file \" ". $ this-> LOG_FILE." \ "\ n ");

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 ("([\ t \ r \ n]) +", "", $ address );

$ Address = ereg_replace ("^. * <(. +)>. * $", "\ 1", $ address );

Return $ address;

}

Function smtp_debug ($ message)

{

If ($ this-> Debug ){

Echo $ message;

}

}

}

?>

Test. php

<? PHP

/*

This is a testProgram!!!

Set the following parameters according to the instructions. The following uses the user of tom.com as an example.

*/

Require ("SM. php ");

######################################## ##

$ Smtpserver = "smtp.tom.com"; // SMTP Server

$ Smtpserverport = 25; // SMTP server port

$ Smtpusermail = "someone@tom.com"; // user email address of the SMTP server

$ Smtpemailto = "jack@knowsky.com"; // to whom to send

$ Smtpuser = "someone"; // User Account of the SMTP server

$ Smtppass = "someonepass"; // user password of the SMTP server

$ Mailsubject = "test subject"; // Email Subject

$ Mailbody = "

$ Mailtype = "html"; // The email format (html/txt). txt is a text email.

######################################## ##

$ SMTP = new SMTP ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass); // here, true indicates that authentication is used; otherwise, authentication is not used.

$ SMTP-> DEBUG = true; // whether to display the sent debugging information

$ SMTP-> Sendmail ($ smtpemailto, $ smtpusermail, $ mailsubject, $ mailbody, $ mailtype );

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.