Email possesses an important position in the network. At present, the mail servers running in the enterprise have their own independent management systems to create, password modifications, mailbox expansion, deletion of mailboxes, etc. in their own independent management systems. When the mailbox user reaches a certain amount, the daily maintenance work of the mail administrator is extremely heavy, which requires automation processing of some administrative tasks (such as account applications); at the same time, part of the task (such as password modification) is used by multiple group administrators. To be responsible, the email administrator will be freed from the cumbersome maintenance work to actually play the mail server.
Selection of mail servers and development environments
At present, many mail servers do not provide development interfaces, so the primary issue of the development of the mail management system within the enterprise is to find an open mail system capable of combining development languages.
Since the stability requirements are high, large-scale email systems generally use UNIX as the server's operating system. For example, Hotmail is used by FreeBSD and Solaris systems.
LINUX comes with a defect in the system structure, it is difficult to withstand access to large users. Qmail is a famous free software in the GNU, a new generation UNIX mail system that supports MAILDIR storage. It saves each message as a separate file in the user's personal mail directory. QMAIL supports virtual domain and virtual users (Virtual users). Current domestic popular free email systems use qmail as basic server software, providing multi-level directory to support larger users.
From the perspective of development, the development model of PHP Linux has been adopted by many developers. Use PHP to call the control program provided by QMAIL to implement various management works of the mail system. In order to achieve effective control of a large number of users, database management can also be introduced. This article will introduce a development example of PHP Oracle QMail Linux.
Overall design
The entire system is divided into email management and mail to send and receive two parts. When you apply for an email, you will first fill in some necessary information and then the system will automatically create an email and allow the user to use it immediately. When the user forgets the password, you can use the common practice of the current Internet to modify the mailbox password by answering user-defined issues. For safety reasons, after the user answers the wrong problem three times, the system locks the "Repair Mailbox Password" function of the account for 30 minutes.
Mail management is divided into the following three levels in accordance with different needs:
◆ Ordinary users can send emails, modify their mailbox passwords, modify custom issues and answers in the browser, and make a query of the mailbox.
◆ Group administrators can modify all accounts of all accounts in this group, and unlock the "Repair Mailbox Password" function of the undergraduate room account in addition to the function of ordinary users.
◆ Mailbox administrator can modify all mailbox port letters, delete mailboxes, modify the mailbox capacity, check the application of the mailbox, and release all accounts "Repair Mailbox Password" function lock.
The transmission and reception of the message uses the standard POP3 function provided by PHP. You can get a message list, check the details of the email, send messages with multiple attachments, and provide email deletion, reply, and so on.
The database is mainly composed of several tables:
◆ Email_info All email information, such as account, person name, department, contact number, custom problem, custom problem, lock tag, etc.
◆ Email_register_info The information filled in the user, such as an account, password, application time, department, and application, the current step (mainly to prevent users from maliciously skip some steps).
◆ email_change_log records the change of the user's email address, and checks when necessary. Mainly records the application time, delete time, email account, and information on the mailbox operation, etc., IP address.
Technology difficult analysis
PHP calls QMAIL control program
In the Linux environment, each program has the concept of the corresponding user and group. When the PHP program is executed in the server, the default user is Nobody without the execution of the QMAIL control program. There is a sudo command in Linux, allowing other users to perform some commands as root. Modify the / etc / sudoers file, add the following sections: noBody all = / var / vpopmail / bin / vadduser (increasing users)
Nobody all = / var / vpopmail / bin / vdeluser (deleted users)
Nobody all = / var / vpopmail / bin / vpasswd (modified password)
NoDy all = / var / vpopmail / bin / vchkpw (check if the user password is correct)
Nobody all = / var / vpopmail / bin / vsetuserquota (Maximum capacity of modification)
Nobody all = / var / vpopmail / bin / vmoduser (modify user information)
Root all = (all) all
When calling a QMAIL control program, you need to manually enter a part of the information. If automation is to be automated, you need to use the redirection technology in Linux to store the information pre-entered in advance to the file, and transfer to the shell script using the redirection technology.
PHP provides functions to perform external commands for system (), exec (). The System () function performs a given command, output, and return results. The exec () function is similar to system (), but does not output the result. Using program control must use the exec () function, and determine if the execution is performed correctly, if an error is required, you need to advertise to the mail administrator. The following code takes "increasing user" function as an example:
Require ("/ Home / httpd / phplib / qmail.inc");
$ qmail_date = "v1";
$ qmail_email = new register_email;
$ SQL = "SELECT PW_NAME FROM". $ qmail_date;
$ qmail_email-> query ($ sql);
While ($ qmail_email-> next_record ())
{
$ qmail_user = $ qmail_email-> f ("pw_name");
$ Passwd = "12345";
$ STR_COMMAND = "sudo / var / vpopmail / bin / vedduser". $ qmail_user .;
$ STR_COMMAND = $ STR_COMMAND. "@ mail.com". $ passwd. "-q 10000000 home / n";
@exec ($ STR_COMMAND, $ STR, & $ RESULT);
IF ($ Result! = 0)
{
Echo "Command String IS:". $ STR_COMMAND. ", Command is failed !!!
$ Error_INFO = "Failed to execute the vadduser command";
$ address = "from: computer@mail.com/nreply-to: computer@mail.com/n";
Mail ("Webmaster@mail.com", $ ERROR_INFO, $ Message, $ address;
}
}
$ qmail_email-> free (); other functions such as modifying passwords, modifying mailbox capacity, deleting mailbox, etc., very similar to program implementation, not to explain.
Upload and download of attachments in e-mail
The PHP itself provides upload function, but according to the actual situation, some configuration and error can be performed accordingly. Users can expand the maximum capacity of upload files by modifying the php.ini file. At the same time, the size of the individual attachments can be limited to 5MB by increasing the method of [I] in the Form on the web page. If the network speed is slow, you need to set the Timeout variable of the web page, otherwise it will time out because the time is too long. The handler part of the server side is as follows:
If (! file_exists ($ mail_att)) // determine if the file is successful
{// error handling and display
Break;
}
COPY ($ mail_att, $ mail_att. "); // re-rename the attachment and copy to the specified location.
For ($ I = 1; $ i <= $ num_attach; $ i )
{
$ file_name = "send_att". $ i; // Get the original name of the upload file
$ file_tmp = "send_att_tmp". $ i; // Get the temporary file name of the upload file in the server
$ file_size = "send_att_size". $ i; // Get the size of the upload file
$ file_mime = "send_att_mime". $ i; // Get the type of uploaded file
}
When providing attachment downloads, there is no common practice here, that is, the URL of the file is given to the user. Because this practice is not safe, users may download directly to the file directly. Instead, the read file is available from the browser that cannot be accessed by the browser to provide a similar security issue. The core code of the program is as follows:
IF (file_exists ($ attach_filepos) // determine if the file exists
{
$ SIZE = FileSize ($ attach_filepos); // Get the size of the file
// Provide the user's original file name to the user download.
Header ("Content-Disposition: attachment; filename =". $ attach_filename;
Header ("Content-Length:". $ size);
Header ("Content-Type: Application / X-Zip-Compressed");
Readfile ($ attach_filepos); // reads the download file and is available to the user.
}
Conclude
The system is technically perfect to combine PHP with Oracle database to implement the management of QMAIL users and the maintenance of daily mailbox servers. At present, the system has been used within our company for more than a year, and the effect is very good. On the one hand, it reduces the daily maintenance of the mail administrator; on the other hand, users who are not familiar with the mail configured online send and receive emails. This system also has functions of authentication, group management, and email, Chinese character coding, etc., due to the limited space limit, it is not an explanation.