A complete SMTP class in PHP (resolved when mail () encounters the mail server to verify the problem)

xiaoxiao2021-03-06  52

SMTP.php

Class

SMTP

{

/ * Public variables * /

VAR

$ SMTP_PORT

VAR

$ TIME_OMT

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 buy in fsockopen () #

$ THIS

->

AUTH

=

$ Auth

;

// auth

$ THIS

->

User

=

$ USER

;

$ THIS

->

PASS

=

$ Pass

;

#

$ THIS

->

Host_name

=

"Localhost"

;

// is buy in Helo Command

$ THIS

->

LOG_FILE

=

""

;

$ THIS

->

Sock

=

False

}

/ * Main function * /

FUNCTION

Sendmail

(

$ TO

,

$ from

,

$ SUBJECT

=

""

,

$ Body

=

""

,

$ MailType

,

$ CC

=

""

,

$ BCC

=

""

,

$ addition_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

=

$ addition_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

(

$ TO

)); if

$ CC

! =

""

) {

$ TO

=

Array_mege

(

$ TO

,

EXPLODE

(

","

,

$ THIS

->

Strip_comment

(

$ CC

))));}

$ BCC

! =

""

) {

$ TO

=

Array_mege

(

$ 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

}

#aut

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

.

"/ 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 relays 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 (! @

FILE_EXISTS

(

$ THIS

->

LOG_FILE

) ||!

$ fp

= @

Fopen

(

$ THIS

->

LOG_FILE

,

"a"

))) {

$ 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

/ * This is a test program !!! Please follow the instructions to set the following parameters, the following is set as an example of Tom.com. * /

REQUIRE

"sm.php"

);

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

$ SMTPSERVER

=

"smtp.tom.com"

;

// SMTP server

$ SMTPSERVERPORT

=

25

;

// SMTP server port

$ SMTPUSERMAIL

=

"someone@tom.com"

;

// SMTP server user mailbox

$ SMTPEMAILTO

=

"someone@hotmail.com"

;

// Who sent?

$ SMTPUSER

=

"someone"

;

/ / SMTP server user account

$ SMTPASS

=

"somePass"

;

// SMTP server user password

$ MAILSUBJECT

=

Test Subject

;

// Mail Topic $ Mailbody

=

"

this is a test mail "

;

//content of email

$ MailType

=

"Html"

;

// mail format (html / txt), txt is text mail ################################################################################################################################################################################################################################### ######

$ SMTP

= New

SMTP

(

$ SMTPSERVER

,

$ SMTPSERVERPORT

,

True

,

$ SMTPUSER

,

$ SMTPASS

);

// This TRUE inside is to represent the use of authentication, otherwise it is not used as authentication.

$ SMTP

->

Debug

=

True

;

/ / Whether to display the sending debugging information

$ SMTP

->

Sendmail

(

$ SMTPEMAILTO

,

$ SMTPUSERMAIL

,

$ MAILSUBJECT

,

$ Mailbody

,

$ MailType

);

?>

转载请注明原文地址:https://www.9cbs.com/read-115531.html

New Post(0)