Php Tutorial fsockopen email sending instance code
<?
// Send an OK email.
Include "smtp. class. php ";
// $ Smtps tutorial erver = "smtp.163.com"; // address of your smtp server
$ Smtpserver = "smtp.163.com ";
$ Port = 25; // The smtp server port, usually 25
$ Smtpuser = "your mailbox @ 163.com"; // The user name of the smtp server you log on
$ Smtppwd = "your email password"; // your smtp server password
$ Mailtype = "txt"; // mail type. Optional values: txt or html. txt indicates a plain text email. html indicates an html email.
$ Sender = "your mailbox @ 163.com ";
// Sender, which is generally the same as the username ($ smtpuser) for logging on to the smtp server. Otherwise, sending may fail due to the setting of the smtp server.
$ Smtp = new smtp ($ smtpserver, $ port, true, $ smtpuser, $ smtppwd, $ sender );
$ Smtp-> debug = true; // whether to enable debugging. It is only used for testing the program. Please comment out this line during official use.
$ To = "the email address of the person you want to send"; // recipient
$ Subject = "Hello ";
$ Body = "The content you sent ";
$ Send = $ smtp-> sendmail ($ to, $ sender, $ subject, $ body, $ mailtype );
If ($ send = 1 ){
Echo "email sent successfully ";
} Else {
Echo "email sending failed <br/> ";
// Echo "reason:". $ this-> smtp-> logs;
}
?>
Smtp. class. 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 ("(^ | (rn) (.)", "1.3", $ body );
$ Header. = "mime-version: 1.0rn ";
If ($ mailtype = "html "){
$ Header. = "content-type: text/htmlrn ";
}
$ Header. = "to:". $ to. "rn ";
If ($ cc! = ""){
$ Header. = "cc:". $ cc. "rn ";
}
$ Header. = "from: $ from <". $ from. ">; rn ";
$ Header. = "subject:". $ subject. "rn ";
$ Header. = $ additional_headers;
$ Header. = "date:". date ("r"). "rn ";
$ Header. = "x-mailer: by redhat (php/". phpversion (). ") rn ";
List ($ msec, $ sec) = explode ("", microtime ());
$ Header. = "message-id: <". date ("ymdhis", $ sec ). ". ". ($ msec * 1000000 ). ". ". $ mail_from. ">; rn ";
$ 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 hostn ");
}
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. "rn". $ body );
$ This-> smtp_debug ("> ;". str_replace ("rn", "n ". ">;", $ header. "n> ;". $ body. "n> ;"));
Return true;
}
Function smtp_eom ()
{
Fputs ($ this-> sock, "rn. rn ");
$ This-> smtp_debug (". [eom] n ");
Return $ this-> smtp_ OK ();
}
Function smtp_ OK ()
{
$ Response = str_replace ("rn", "", fgets ($ this-> sock, 512 ));
$ This-> smtp_debug ($ response. "n ");
If (! Ereg ("^ [23]", $ response )){
Fputs ($ this-> sock, "quitrn ");
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. "rn ");
$ 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, ""))){
$ 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 ("([trn]) +", "", $ address );
$ Address = ereg_replace ("^. * <(. +)>;. * $", "1", $ address );
Return $ address;
}
Function smtp_debug ($ message)
{
If ($ this-> debug ){
Echo $ message;
}
}
}
Function sendmail ($ smtpserver, $ smtpuser, $ smtppass, $ smtpemailto, $ smtpusermail, $ mailsubject, $ mailbody ){
$ Smtp = new smtp ($ smtpserver, 25, true, $ smtpuser, $ smtppass );
// $ Smtp-> debug = true;
$ Smtp-> sendmail ($ smtpemailto, $ smtpusermail, $ mailsubject, $ mailbody, "html ");
}
// Such
// Sendmail ("smtp.126.com", "test@126.com", "password", "1034555083@qq.com", "test@126.com", "title", "body ");
?>
For more information about the fsockopen function, see
Http://www.111cn.net/phper/php-function/33796.htm