PHP mail activation verification

Source: Internet
Author: User
Tags ereg
Step 1 of PHP mail activation verification: connect to the database in php: dbconnection. php & nbsp; & lt ;? Php $ server = & quot; (local) & quot; $ _ uid = & quot; adweb & quot; $ _ pwd = & quot; loveyou & quot ;; $ _ database = PHP sends email activation verification

Step 1: connect to the database using php: dbconnection. php

 

$ Server = "(local )";
$ _ Uid = "adweb ";
$ _ Pwd = "loveyou ";
$ _ Database = "addev ";


 Function sqlsrv_connect ($ server, $ uid, $ pwd, $ db){
    
    $ Connect_id = mssql_connect ($ server, $ uid, $ pwd );
           // Echo $ connect_id;
   If ($ connect_id ){
               If (mssql_select_db ($ db, $ connect_id ))
                   Return $ connect_id;
    Else
     EchoMssql_get_last_message ();
           }
   Else
    EchoMssql_get_last_message ();
 }

 Function sqlsrv_query ($ SQL, $ conn){
   Returnmssql_query ($ SQL, $ conn );
 }

Function sqlsrv_num_rows ($ n ){
 
 Return mssql_num_rows ($ n );

}
Function sqlsrv_fetch_array ($ n ){

  Returnmssql_fetch_array ($ n );
}
// Call a function to connect to the database
$ Conn = sqlsrv_connect ($ server, $ _ uid, $ _ pwd, $ _ database);

?>

 

Step 2: email. class. php

 

Class smtp
{

Var $ smtp_port;
Var $ time_out;
Var $ host_name;
Var $ log_file;
Var $ relay_host;
Var $ debug;
Var $ auth;
Var $ user;
Var $ pass;


Var $ sock;


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 HELOcommand
$ This-> log_file = "";

$ This-> sock = FALSE;
}


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 remotehost \ n ");
}
Echo"
";
Echo $ header;
Return $ sent;
}

Function smtp_send ($ helo, $ from, $ to, $ header, $ body = "")
{
If (! $ This-> smtp_putcmd ("HELO", $ helo )){
Return $ this-> smtp_error ("sending HELOcommand ");
}
# Auth
If ($ this-> auth ){
If (! $ This-> smtp_putcmd ("auth login", base64_encode ($ this-> user ))){
Return $ this-> smtp_error ("sending HELOcommand ");
}

If (! $ This-> smtp_putcmd ("", base64_encode ($ this-> pass ))){
Return $ this-> smtp_error ("sending HELOcommand ");
}
}
#
If (! $ This-> smtp_putcmd ("MAIL", "FROM: <". $ from. "> ")){
Return $ this-> smtp_error ("sending MAIL FROMcommand ");
}

If (! $ This-> smtp_putcmd ("RCPT", "TO: <". $ to. "> ")){
Return $ this-> smtp_error ("sending RCPT TOcommand ");
}

If (! $ This-> smtp_putcmd ("DATA ")){
Return $ this-> smtp_error ("sending DATAcommand ");
}

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 QUITcommand ");
}

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 relayhost". $ 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 mxhosts (". 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, ""))){
$ 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 ."
";
}
}

Function get_attach_type ($ image_tag ){//

$ Filedata = array ();

$ Img_file_con = fopen ($ image_tag, "r ");
Unset ($ image_data );
While ($ tem_buffer = AddSlashes (fread ($ img_file_con, filesize ($ image_tag ))))
$ Image_data. = $ tem_buffer;
Fclose ($ img_file_con );

$ Filedata ['context'] = $ image_data;
$ Filedata ['filename'] = basename ($ image_tag );
$ Extension = substr ($ image_tag, strrpos ($ image_tag, "."), strlen ($ image_tag)-strrpos ($ image_tag ,"."));
Switch ($ extension ){
Case ". gif ":
$ Filedata ['type'] = "image/gif ";
Break;
Case ". gz ":
$ Filedata ['type'] = "application/x-gzip ";
Break;
Case ". htm ":
$ Filedata ['type'] = "text/html ";
Break;
Case ". html ":
$ Filedata ['type'] = "text/html ";
Break;
Case ". jpg ":
$ Filedata ['type'] = "image/jpeg ";
Break;
Case ". tar ":
$ Filedata ['type'] = "application/x-tar ";
Break;
Case ". txt ":
$ Filedata ['type'] = "text/plain ";
Break;
Case ". zip ":
$ Filedata ['type'] = "application/zip ";
Break;
Default:
$ Filedata ['type'] = "application/octet-stream ";
Break;
}


Return $ filedata;
}

}
?>

Step 3: activate class jihuo. php

Require ('dbconnection. php ');

 Echo $ _ GET ['code'];
 Echo "activated ";
 Echo "";
If ($ _ GET [code]) {
 $ Result = sqlsrv_query ('Select * from actcodeWhere code = "'. $ _ GET [code].'" ', $ conn ); // Check whether the activated account exists in the database
 If (sqlsrv_num_rows ($ result) = 1 ){
    // $ _ ROW = sqlsrv_fetch_array ($ result );
  // $ I = sqlsrv_query ('updateaduserSet status = 5 whereemail = "'. $ _ ROW [email].'" ', $ conn );// Modify the status value of this email user to 1 at registration.
  // If ($ I ){
   // Sqlsrv_query ('DeleteFrom actcode wherecode = "'. $ _ GET [code].'" ', $ conn );
   Echo 'email activation-log on to the homepage ';
   //}
 }
 Else {
  Echo 'verification completed ';
 }

}
?>

 

Step 4: Send email sendemail. php

 // Send an activation email
Require_once ('email. class. php ');
Require ('dbconnection. php ');

//###################################### ####
$ Smtpserver = "smtp.163.com"; // SMTP server 163/126/sohu/qq/xinlang
$ Smtpserverport = 25; // SMTP server port
$ Smtpusermail = "zxwzxw_1@163.com"; // user email address of the SMTP server
$ Smtpuser = "zxwzxw_1"; // User account of the SMTP server
$ Smtppass = "doyoumissme"; // user password of the SMTP server
$ Mailsubject = "hello, glad to meet you! "; // Email subject
$ Mailtype = "HTML"; // The email format (HTML/TXT). TXT is a text email.
######################################## ##
$ Smtp = newsmtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass); // Here, true indicates that authentication is used; otherwise, authentication is not used.
$ Smtp-> debug = false; // whether to display the sent debugging information
$ Toemail = "zxwzxw_12@163.com ";// Recipient's email
$ Actcodes = md5 ($ toemail. mt_rand (111111,999999 ));
$ Mmsg = 'You have registered successfully. please click the following link to activate your account: click activity ';

 $ Hh = sqlsrv_query ('Insert into actcode (code, email) values ("'. $ actcodes.'", "'. $ toemail.'") ', $ conn );// Insert the mailbox number and verification at registration into the data table
If ($ hh ){
   $ Smtp-> sendmail ($ toemail, $ smtpusermail, $ mailsubject, $ mmsg, $ mailtype ); // Send an email
}
Else {
 Echo 'failed to send! ';
 Exit;
}
   

?>

 

Step 5: Final: Sender mailbox activation verification .txt

 

1. create a database: addevTable ACTcodeField code varchar (50) Emailvarchar (50)

2. connection data: dbconnection. php

$ Server = "(local )";
$ _ Uid = "adweb ";
$ _ Pwd = "loveyou ";
$ _ Database = "addev ";


 Function sqlsrv_connect ($ server, $ uid, $ pwd, $ db){
    
    $ Connect_id = mssql_connect ($ server, $ uid, $ pwd );
           // Echo $ connect_id;
   If ($ connect_id ){
               If (mssql_select_db ($ db, $ connect_id ))
                   Return $ connect_id;
    Else
     EchoMssql_get_last_message ();
           }
   Else
    EchoMssql_get_last_message ();
 }


// Call a function to connect to the database
$ Conn = sqlsrv_connect ($ server, $ _ uid, $ _ pwd, $ _ database);

?>


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.